我是 ISA x86 64 位的新编程人员,所以我尝试使用十六进制转换在汇编中打印数字 1 到 10,但我得到了无限循环,因为 8 位寄存器 $cl 不能增量。我一直在使用 gdb 来分析代码,但我找不到运行代码的方法,并且在此过程中没有无限循环。我希望你们都能帮助我,并且记住并且是新手,所以请耐心等待。
section .data
number db 0; Buffer
nl db 10; \n
section .text
global _start
_start:
; syscall information
mov rax, 1
mov rdi, 1
mov rsi, number
mov rdi, 1
mov byte[number], "0" ; Inicializando the buffer in "0" = 46 Dec
.loop:
add byte[number], cl ; cl contains the data of the number in ASCII format
mov rdx, 1 ; lenght of bytes
syscall
inc cl; ASCII data
cmp cl, 0x39; 0x39 = 57 Dec = 9
jle .loop; lower equal jump
; \n
mov rsi, nl
mov rdx, 1
syscall
; Exit
mov rax, 60
xor rdi, rdi
syscall
我一直在使用gdb来分析代码,即使我尝试更改8位寄存器而不是使用cl,但没有任何作用。
syscall
摧毁 rcx
,其中包括 cl
。选择不同的寄存器。 rax
用于返回值,因此也需要重新加载,尽管在本例中它恰好可以工作。另外,你总是添加到 number
,这不会达到你想要的效果。
一个可能的解决方案,只需进行最小的更改:
section .data
number db 0; Buffer
nl db 10; \n
section .text
global _start
_start:
; syscall information
mov rdi, 1
mov rsi, number
mov rdi, 1
mov bl, '0'
.loop:
mov [number], bl ; bl contains the data of the number in ASCII format
mov eax, 1 ; need to reload syscall number
mov rdx, 1 ; lenght of bytes
syscall
inc bl; ASCII data
cmp bl, 0x39; 0x39 = 57 Dec = 9
jle .loop; lower equal jump
; \n
mov rsi, nl
mov rdx, 1
syscall
; Exit
mov rax, 60
xor rdi, rdi
syscall