assembly - 如何在 8086 汇编中将 Word 转换为 Ascii 字符串?

标签 assembly x86-16

这里我有一个将单词值转换为字符串的汇编代码

; si -> Offset Address of String
; ax -> Number to be converted
itoa:
push ax ; Backup AX To Stack
push bx ; Backup BX To Stack
push dx ; Backup DX To Stack
push si ; Backup SI To Stack
mov bx,10 ; Set BX to 10 for Dividing the AX by 10
.loop:
    div bx ; Divide AX With BX For Getting The Digit And Removing First Digit Of Number
    add dl,48 ; Add 48 to DL to Convert Digit to Ascii
    mov [cs:si],dl ; Write digit to string
    xor dl,dl ; Set DX to 0
    inc si ; Increase SI
    cmp ax,0 ; Compare AX with 0
    jne .loop ; If ax not equals 0 then jump back to start of loop
pop si ; Get Backup Value of SI From Stack
pop dx ; Get Backup Value of DX From Stack
pop bx ; Get Backup Value of BX From Stack
pop ax ; Get Backup Value of AX From Stack
ret ; Return from Subroutine

当我运行此代码时,它输出 AX 中给出的数字的反转,这对我来说是个问题。 我该如何解决这个问题?

最佳答案

我是这样做的

utoa:
   mov  cx,10
nextdiv:
   xor  dx,dx
   div  cx
   push dx
   or   ax,ax
   jz   nextdigit
   call nextdiv
nextdigit:
   pop  ax
   add  al,30h
   mov  [si],al
   inc  si
   ret

它在堆栈上递归,因此最多使用 10 个字,每个数字加上返回值。 我称其为 utoa,因为它是未签名的,如果您想要签名版本 (itoa),请询问。

关于assembly - 如何在 8086 汇编中将 Word 转换为 Ascii 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65553846/

相关文章:

assembly - INT 0x13/AH = 0x02 适用于软盘镜像,但不适用于在闪存驱动器上刻录

performance - 使用 SIMD/AVX/SSE 进行树遍历

assembly - 当带有指令的内存被另一个内核更改时,CPU 流水线会发生什么情况?

assembly - 如何使用windbg到达入口点

gcc - 约束 "Rah"和 "Ral"在扩展内联汇编中意味着什么?

.net - 你能帮我处理 Delphi .net 的 8086 项目吗?

assembly - 8086 实模式下有哪些中断可用?

程序集 - 读取虚拟磁盘的下一个扇区

c - x86 32 位汇编中快速排序的优化

assembly - nasm 64 位推送 qword?