我正在使用 intel 上的 at&t 语法来处理汇编程序。
我迷路了,如何将寄存器中的整数转换为 ASCII 数字?
假设我想转换数字 10,我会将数字 10 放入寄存器 %eax 中。
如果我只是将数字 48 添加到 %eax,ascii 符号将是:
我想将 48 加到 1 上,然后将 48 加到数字 10 中的 0 上。
我怎样才能做到这一点?
示例代码:
mov $10, %eax
#Cut the number in some way.
add $48, %eax
要将数字转换为 ASCII,您需要将数字除以 10 并使用余数作为结果。然后添加 ASCII“0”并存储结果数字。然后对商重复相同的操作,直到商达到零。
然而,这会以相反的顺序给出数字,从最低有效数字开始。您可以颠倒顺序,例如使用堆栈。将每个数字推入堆栈,然后弹出它们并存储到字符串缓冲区中。
像这样的东西(未测试):
.DATA
strResult db 16 dup (0) ; string buffer to store results
.CODE
mov eax, number ; number to be converted
mov ecx, 10 ; divisor
xor bx, bx ; count digits
divide:
xor edx, edx ; high part = 0
div ecx ; eax = edx:eax/ecx, edx = remainder
push dx ; DL is a digit in range [0..9]
inc bx ; count digits
test eax, eax ; EAX is 0?
jnz divide ; no, continue
; POP digits from stack in reverse order
mov cx, bx ; number of digits
lea si, strResult ; DS:SI points to string buffer
next_digit:
pop ax
add al, '0' ; convert to ASCII
mov [si], al ; write it to the buffer
inc si
loop next_digit
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)