汇编编号转为 ascii

标签 assembly intel

我正在使用英特尔上的 at&t 语法在汇编中处理程序。

我迷路了,如何将寄存器中的整数转换为 ascii 数?

假设我想转换数字 10,我会将数字 10 放在寄存器 %eax 中。
如果我只是将数字 48 添加到 %eax,则 ascii 符号将是:

我想在 1 上加 48,然后在数字 10 中的 0 上加 48。
我怎样才能做到这一点?

示例代码:

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

关于汇编编号转为 ascii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9113772/

相关文章:

c - 如何在没有Windows头文件的情况下使用NtCurrentTeb()?

string - 查找输入字符串中的子字符串

将汇编代码中的c调用函数也转换为汇编代码

x86 - 如何使用微程序修改 Intel CPU 的指令集架构?

linux - 当另一个进程共享相同的HT内核时,为什么一个进程的执行时间更短

x86 - 英特尔x86处理器的整数除法算法

c++ - 微软计算器使用的库

assembly - 64位汇编介绍

python - 模块未找到错误: No module named 'keras' in AI DevCloud Intel

c# - 英特尔架构上的双读原子吗?