首页 文章

在Assembly x86 NASM中从内存中读取16位

提问于
浏览
2

我正在尝试在汇编中进行一个非常简单的练习:将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位 .

有人能帮我吗?提前致谢

1 回答

  • 3

    该程序正确地汇总了数组 . 问题在于返回结果 .

    [答案更正了,多亏了杰斯特 . ]

    您将返回值传递给 sys_exit() (这是 mov eax, 1; int 0x80 所做的) . sys_exit() 仅返回the lower 8 bits的返回值(其他位用于某些标志) .

    这就是第9位丢失的地方 .

    Shell观察到已截断的返回值并将其打印出来 .

相关问题