这需要在纯汇编中完成(即没有库或对 C 的调用)。
我理解问题的本质:需要将整数除以 10,将一位数余数转换为 ASCII,输出,然后用商重复该过程。
但由于某种原因,它不起作用。我在 x86 上使用 NASM。
这是我到目前为止所拥有的(不输出任何内容,但也不抛出任何汇编器错误):
; integer to output is stored in eax
mov ecx, 10 ; for base 10
loop:
div ecx ;EAX contains the quotient, EDX the remainder
; Do something to EDX to convert it to ASCII, not sure if this is correct
add edx, '0'
push eax ;We'll be playing with EAX to output EDX, save EAX to the stack
mov eax, 4 ; sys_write
mov ebx, 1 ; to STDOUT
mov ecx, edx
mov edx, 1
int 0x80
pop eax ;restore EAX
cmp eax, 0 ;If EAX is 0, our job is done
jnz loop
有很多与此类似的问题(即,this https://stackoverflow.com/questions/4117422/more-efficient-way-to-output-an-integer-in-pure-assembly and this https://stackoverflow.com/questions/6892805/how-to-get-integer-input-with-assembly),但我在实施过程中迷失了方向。这个问题 https://stackoverflow.com/questions/4244624/print-integer-to-console-in-x86-assembly(对于 DOS)也很有帮助,但我仍然很困惑。
我一定在这里遗漏了一些东西。想法?