繁体   English   中英

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

[英]Read 16 bits from memory in Assembly x86 NASM

我试图在组装中做一个非常简单的练习:对位于连续内存单元中的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位。

有人能帮我吗? 提前致谢

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

[答案得到纠正,多亏了杰斯特。]

您将返回值传递给sys_exit() (这是mov eax, 1; int 0x80所做的事情)。 sys_exit()仅保留返回值的低8位 (其他位用于某些标志)。

那是第9位丢失的地方。

Shell观察已经截断的返回值并打印出来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM