Assembly 将双字号打印为字符串

Assembly 将双字号打印为字符串,assembly,dos,tasm,Assembly,Dos,Tasm,我在si:bx中有两个单词编号。 如何将其作为字符串写入数组?最简单的方法是将数字转换为十六进制。每组4位变成一个十六进制数字 ; this is pseudocode mov di, [addr_of_buffer] ; use the string instructions cld ; always remember to clear the direction flag ; convert bx mov cx, 4 ; 4 hexits

我在si:bx中有两个单词编号。
如何将其作为字符串写入数组?

最简单的方法是将数字转换为十六进制。每组4位变成一个十六进制数字

; this is pseudocode
mov di, [addr_of_buffer]  ; use the string instructions
cld                       ; always remember to clear the direction flag

; convert bx
mov cx, 4    ; 4 hexits per 16 bit register
hexLoopTop:
mov al, bl   ; load the low 8 bits of bx
and al, 0x0F ; clear the upper 4 bits of al
cmp al, 10
jl decimal
add al, 65   ; ASCII A character
jmp doneHexit:
decimal:
add al, 48   ; ASCII 0 character
doneHexit:
stosb        ; write al to [di] then increment di (since d flag is clear)
ror bx       ; rotate bits of bx down, happens 4 times so bx is restored
loop hexLoopTop

; repeat for si, note that there is no need to reinit di
现在如果你想要十进制输出,你需要一个更复杂的算法。最明显的方法是通过重复划分。请注意,如果您有32位CPU(386或更高版本),您可以使用16位模式的32位寄存器,这使得这更容易

将si:bx移动到eax以启动。要获取下一个数字,您需要执行一条cdq指令,将eax扩展到edx:eax,div乘以10,edx中的剩余部分就是下一个数字。eax中的商要么是0(在这种情况下,您完成了),要么是下一轮循环的输入。这将首先为您提供最有效的数字,因此您需要在完成后反转这些数字。您的缓冲区需要至少是2^32的基数10日志,向上取整为10。调整此算法以处理有符号整数并不难