[英]How to convert Word To Ascii String In 8086 Assembly?
这里我有一个汇编代码,可以将单词值转换为字符串
; 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) 询问。
output 一个接一个的字符,就像使用 DOS 显示数字中的示例一样,或者从缓冲区的末尾一次存储所有字符和 output 字符。 在这种情况下,不要忘记适当地终止缓冲区。
请注意,在您当前的代码中,您忘记在第一次除法之前将DX
归零!
转换一个单词最多可以生成5 个字符,这就是为什么我在缓冲区中寻找偏移量 +5,其中终止符紧跟在最低有效数字之后。
add si, 5
mov byte [si], '$'
mov bx, 10
.loop:
xor dx, dx
div bx
add dl, 48 ; Add 48 to DL to Convert Digit to Ascii
dec si
mov [si], dl ; Write digit to string
test ax, ax
jnz .loop
这里SI
指向字符串的开头。 您可以通过以下方式打印:
mov dx, si
mov ah, 09h ; DOS.PrintString
int 21h
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.