assembly - 从 Assembly x86 NASM 内存中读取 16 位

标签 assembly x86 sum nasm 32-bit

我正在尝试做一个非常简单的汇编练习:将位于连续内存单元中的 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/

相关文章:

gcc - 退出系统调用的正确常量是什么?

assembly - BIOS int 13中的 "invalid command (error code 0x01)"是什么意思

performance - ADD 1 真的比 INC 快吗? x86

c++ - 使用 Microsoft Detours 时出现访问冲突

mysql - 表中选定行之间的差异之和

linux - 可变长度表的 Bash 列总和

assembly - 为什么 x86 16 位寻址模式没有比例因子,而 32 位版本有?

linux - linux 3.13 中定义的 syscall num 在哪个文件中?

assembly - leal(%eax,%eax) 是做什么的?

algorithm - 在 3 个数组中的每一个中找到 3 个总和为给定值的元素