这需要在纯装配中完成(即没有库或对C的调用)。在Linux上输出装配整数
我明白问题的实质:需要将整数除以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了一些问题和this),但我迷失在执行中。 This question(对于DOS)也很有帮助,但我仍然感到困惑。
我必须在这里失去一些东西。思考?
我没有看到这个代码中的任何地方常量'10',这将是必要的打印出十进制数字。 (你需要用'10'重复'div'和'mod',存储数字,反转并输出它们,或者先从'big end'开始,然后除以'100,000,000',然后'10,000,000'等等 - 但这可能不会比存储和反转方法复杂。) – sarnold
@sarnold忘记在顶部包括一行。编辑我的问题添加它。 –
优秀!感谢更新。 – sarnold