在NASM汇编代码中遍历数组

3
所以我正在上汇编语言课程,我们正在尝试从输入创建和排序数组。我可以很好地掌握排序逻辑,但是我在尝试遍历数组时遇到了很大的困难。
为了进一步说明以下代码的作用,我们应该从命令行中读取整数。数字0表示用户输入的结束,然后调用排序方法。
这个方面还算顺利,但是数组迭代却不起作用。
下面是我所做的示例。
main:
        mov ebx, array  ;move the variable array to ebx
        mov ecx, 0          ;set ecx to zero
        jmp input            ;jump to input
input:
        mov edx, 0          ;move 0 to edx
        call ReadInt        ; read in an integer from input to eax
        cmp eax, edx     ; compare the integer to 0
        je sort                ;jump if it equals zero to the sort method
        mov [ebx+ecx], eax    ;moves eax to the ecxth element of the array stored in ebx
        add ecx, 1                    ;increment ecx by 1
        jmp input                        ; jump back to input

当我在后面的函数中打印数组时,无论我选择哪个元素,它总是一些疯狂的大数字。

我的逻辑在上面的代码片段中是错误的吗?你有什么改进建议吗?


尝试不使用 mov edx, 0,而是直接使用 cmp eax, 0 - owacoder
1个回答

3
由于asm使用字节索引,而每个整数占用4个字节,因此您需要按4倍缩放。因此,您需要类似于 mov [ebx+ecx*4], eax 的内容。或者在每次迭代中将 ecx 增加 4

这解决了我的问题!非常感谢。 - BlazerBrown

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接