在.data中添加另一个单词正在与x86中的值混乱

发布于 2025-02-12 21:53:21 字数 812 浏览 1 评论 0原文

我创建了一个简单的代码来求和(目前是前20个数字)。一切都很好,甚至我要打印一个数字的功能。有一个问题:我不能使用我在.data上分配的两个单词。

section .data 
    total dw 0
    index dw 1

如果我将索引添加到.data,则的值[Total]从0到65536。 S仍然是1,我对为什么会发生这种情况有任何线索。

我在_sum20中删除了循环和说明,以缩短粘贴代码(我对其进行了测试)。

section .data 
    total dw 0
    index dw 1

_start:
    call _sum20         
    call _printInt     
    end                 

_sum20:
    mov rax, [total] ; rax = 65536
    ret 

在上面的代码中,[Total]

section .data 
    total dw 0


_start:
    call _sum20         
    call _printInt     
    end                 

_sum20:
    mov rax, [total] ; rax = 0
    ret 

在此处具有65536的值,[Total]的值为0

I created a simple code to make a summation (currently the first 20 numbers). Everything goes fine, even the functions I made to print a number. There's a single problem: I can't use both words I allocated at .data.

section .data 
    total dw 0
    index dw 1

If I add index to .data, the value of [total] goes from 0 to 65536. index's is still 1, and I don't have any clue of why this is happening.

I removed the loops and instructions in _sum20 to shorten the pasted code (I tested it).

section .data 
    total dw 0
    index dw 1

_start:
    call _sum20         
    call _printInt     
    end                 

_sum20:
    mov rax, [total] ; rax = 65536
    ret 

In the code above, [total] has a value of 65536

section .data 
    total dw 0


_start:
    call _sum20         
    call _printInt     
    end                 

_sum20:
    mov rax, [total] ; rax = 0
    ret 

Here, [total]'s value is 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素食主义者 2025-02-19 21:53:21

问题很简单:您定义了totaldata是单词,即2个字节数量,然后使用Quad Word(即8个字节操作)访问了变量。这种超大的内存访问不仅会导致一个变量,还会导致某些无关内存受到影响。

要解决此问题,要么更改totaldata使用dq而不是dw指令或更改代码以使用16位数据大小访问这些变量。例如,在您的情况下,您可以这样做:

movzx rax, word [total]

The problem is simple: you defined total and data to be words, i.e. 2 byte quantities but then accessed the variables using quad word, i.e. 8 byte operations. This oversized memory access causes not just one variable, but also some unrelated memory after it to be affected.

To fix this problem, either change total and data to be quad word variables using the dq instead of dw directive, or change your code to access these variables with a 16 bit data size. For example, in your case you could do:

movzx rax, word [total]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文