我正在尝试做一个非常简单的汇编练习:将位于连续内存单元中的 N 个数字相加。这是我的实际代码:
global _start section .data array: DD 300,10,20,30,40,50 ; Allocate the numbers arrayLen: EQU $-array ; Get the quantity of numbers section .text _start: mov ecx, 0 ; The counter mov ebx, 0 ; The Accumulator loop: add ebx, DWORD [array+ecx] ; get the i-th word and add it to the accumulator add ecx, 4 cmp ecx, arrayLen jne loop mov eax, 1 int 0x80
程序已正确编译并执行,但返回错误值。结果应该是 450,但我得到的是 194。
我注意到 194 与 450 是相同的比特流,但省略了第 9 位。 调试我的程序时,我认为由于某种原因,我无法理解,当我阅读
[array+ecx]
虽然我指定了关键字 DWORD,但它只读取 8 位。
有人可以帮助我吗? 提前致谢
最佳答案
程序正确地对数组求和。问题在于返回结果。
[答案已更正,感谢 Jester。]
您将返回值传递给sys_exit()
(这就是mov eax, 1; int 0x80
所做的)。
sys_exit()
只留下 the lower 8 bits返回值(其他位用于某些标志)。
这就是第 9 位丢失的地方。
Shell 观察已截断的返回值并将其打印出来。
关于assembly - 从 Assembly x86 NASM 内存中读取 16 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27887915/