遍历 NASM 汇编代码中的数组 [英] Iterating through an array in NASM assembly code

查看:88
本文介绍了遍历 NASM 汇编代码中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我目前正在学习汇编语言课程,我们正在尝试根据输入创建和排序数组.我可以很好地获得排序逻辑,但是我在尝试遍历数组时遇到了很多麻烦.

So I'm currently taking an assembly language class and we are trying to create and sort an array from input. I can get the sorting logic just fine, but I'm having a great deal of trouble trying to iterate through the array.

为了进一步说明以下代码的作用,我们应该从命令行读取整数.数字 0 分隔用户输入的结尾,然后调用排序方法.

For further clarification of what the following code does, we are supposed to read in integers from the command line. The number 0 delimits the end of user input, and then the sorting method is called.

这很好,是数组迭代不起作用.

That works fine, it's the array iteration that is not working.

这是我正在做的示例

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

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

When I go to print the array in a later function, no matter what element I choose, it's always some crazy large number.

在上面的代码片段中我的逻辑关闭了吗?你有什么改进建议?

Is my logic off in the above code snippet? What improvements would you suggest?

推荐答案

由于 asm 使用字节索引,但每个整数为 4 个字节,因此您需要按 4 进行缩放.因此,您需要像 mov [ebx+ecx*4], eax 这样的东西.或者,在每次迭代中将 ecx 增加 4.

You need to scale by 4 since asm uses byte indexing but each integer is 4 bytes. Thus, you need something like mov [ebx+ecx*4], eax. Alternatively, increment ecx by 4 in each iteration.

这篇关于遍历 NASM 汇编代码中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆