在.data中添加另一个单词正在与x86中的值混乱
我创建了一个简单的代码来求和(目前是前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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题很简单:您定义了
total
和data
是单词,即2个字节数量,然后使用Quad Word(即8个字节操作)访问了变量。这种超大的内存访问不仅会导致一个变量,还会导致某些无关内存受到影响。要解决此问题,要么更改
total
和data
使用dq
而不是dw
指令或更改代码以使用16位数据大小访问这些变量。例如,在您的情况下,您可以这样做:The problem is simple: you defined
total
anddata
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
anddata
to be quad word variables using thedq
instead ofdw
directive, or change your code to access these variables with a 16 bit data size. For example, in your case you could do: