我一直在尝试学习一些汇编并测试数组,发现当我尝试打印索引点处的值时没有发生任何事情,经过进一步实验后,似乎即使我使用了许多示例中所示的数组通过互联网,它根本不起作用
这是代码:
section .text
global _start
_start:
mov eax, num ; eax now contains 5
mov ebx, [array+8] ; ebx now contains 8
cmp eax, ebx ; compares eax to ebx
jge skip ; should not happen because eax is smaller than ebx
call printdigit
skip:
call printn
call _exit
printdigit:
mov eax, 0x30
add [num], eax
mov ecx, num
mov edx, 1 ;length
mov ebx, 1 ;write to stdout
mov eax, 4 ;write call number
int 0x80
ret
printn:
mov eax, 0x0A
push eax
mov eax, 4
mov ebx, 1
mov ecx, esp
mov edx, 1
int 0x80
add esp, 4
ret
_exit:
mov eax, 1
mov ebx, 0
int 0x80
section .data
num dw 5
array dw 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
我用来编译代码的命令
nasm -f elf Bubblesort.asm
ld -m elf_i386 -s -o Bubblesort Bubblesort.o
./Bubblesort
我正在运行什么:
ubuntu 22.04.3 桌面 amd64,(在虚拟机上,但我认为应该不重要)
我想要的输出应该是
5
实际输出
我希望仅当 num 小于数组中索引的值时才调用 printdigit
我几乎可以肯定这不是计算机问题而是代码问题,但我不确定在哪里
我现在做了以下更改
_start:
mov eax, [num]
mov ebx, [array+8]
cmp eax, ebx
jae skip
section .data
num dd 5
array dd 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
其余代码正常
当数组索引到大于 num 的值时,现在打印我想要的值,但是当它不大于 num 时,它仍然打印该值