我需要在纯汇编中完成这个任务(即没有使用库或调用C函数)。
我理解问题的本质:需要将整数除以10,将一位数字余数转换为ASCII,输出并重复这个过程直到商为0。
但出于某种原因,它就是不起作用。 我正在使用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
有很多类似的问题(比如这个和这个), 但我在实现时遇到了困难。对于DOS系统,这个问题也是有帮助的,但我仍然感到困惑。
我一定是漏掉了什么。有什么想法吗?
10
,这对于打印十进制数字是必要的。(您需要重复使用div
和mod
与10
,存储数字,反转它们并输出。或者从“大端”开始,先除以100,000,000
,然后除以10,000,000
等等——但这可能不比存储和反转方法简单。) - sarnoldint 0x80
系统调用。 - Peter Cordes