将双字数字打印到字符串

发布于 2024-08-25 00:09:26 字数 41 浏览 2 评论 0原文

我在 si:bx 中有双字编号。 我怎样才能将它作为字符串写入数组?

i have double word number in si:bx.
How can i write it to array as string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2024-09-01 00:09:26

最简单的方法是将数字转换为十六进制。每组 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,除以 10,edx 中的余数就是下一个数字。 eax 中的商要么是 0(此时您已完成),要么是下一轮循环的输入。这将首先为您提供最高有效的数字,因此完成后您需要反转数字。您的缓冲区至少需要是 2^32 的以 10 为底的对数,向上舍入为 10。调整此算法以使用有符号整数并不难。

The simplest way is to convert the number to hex. Each group of 4 bits becomes a hex digit.

; 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

Now if you want decimal output, you'll need a rather more complicated algorithm. The obvious way to do that is by repeated division. Note that if you have a 32bit CPU (386 or later) you can use the 32bit registers from 16 bit mode, which makes this easier.

Move si:bx into eax to start. To get the next digit you execute a cdq instruction to extend eax into edx:eax, div by 10, and the remainder in edx is the next digit. The quotient in eax is either 0 in which case you're done, or the input for the next round of the loop. This will give you the most significant digit first, so you'll need to reverse the digits when you're done. Your buffer will need to be at least the base 10 log of 2^32 rounded up which is 10. It is not too hard to adjust this algorithm to work with signed integers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文