如何将变量转换为要打印的 DECIMAL 字符串?
我计划将 X 变量转换为十进制。我在使用涡轮汇编器时遇到了困难,你能帮忙吗?
code segment ;inicio de un segmento unico
assume cs:code,ds:code,ss:code
org 100h ;localidad de inicio del contador
main proc ;procedimiento principal
mov ax,cs
mov ds,ax ; INICIO
mov ax, x
mov ah,4ch ;comienzo del fin de programa
int 21h ;fin del programa
main endp
x dw 0A92FH
code ends ; fin del segmento de codigo
end main ;fin del ensamble
多谢
I plan to convert the X variable to decimal. I'm having a hard time using turbo assembler, can you give a hand?
code segment ;inicio de un segmento unico
assume cs:code,ds:code,ss:code
org 100h ;localidad de inicio del contador
main proc ;procedimiento principal
mov ax,cs
mov ds,ax ; INICIO
mov ax, x
mov ah,4ch ;comienzo del fin de programa
int 21h ;fin del programa
main endp
x dw 0A92FH
code ends ; fin del segmento de codigo
end main ;fin del ensamble
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将数字转换为可打印格式时,通常最容易从最后一位数字开始。
考虑将 123 转换为“123”,我们如何获得最后一位数字?它是除以 10(底数)后的余数。因此 123 % 10 给出了 3,而 123 / 10 = 12 则方便地给出了在下一次迭代中使用的正确数字。在 x86 上,“DIV”指令足以为我们提供商和余数(分别在
ax
和dx
中)。剩下的就是在字符串中存储可打印字符。将所有这些放在一起,您最终会得到如下内容(使用 nasm 语法):
这需要一个工作缓冲区(当基数 = 2 时为 16,并且为 NUL 终止符提供一个额外字节):
添加对有符号数字的支持保留为供读者练习。 此处 与适用于 64 位汇编的例程大致相同。
When converting numbers to a printable format it's often easiest to start with the last digit.
Consider converting 123 to "123", how would we get the last digit? It's the remained when dividing by 10 (the base). So 123 % 10 gives us 3 and 123 / 10 = 12 conveniently gives us the correct number to work with in the next iteration. On x86 the "DIV" instruction is nice enough to give us both the quotient and remainder (in
ax
anddx
respectively). All that remains is to store printable characters in the string.Putting all this together you end up with something like the following (using nasm syntax):
This requires a working buffer (16 for the case when base = 2 and an extra byte for the NUL terminator):
Adding support for signed numbers is left as an exercise for the reader. Here is roughly the same routine adapted for 64-bit assembly.