如何将变量转换为要打印的 DECIMAL 字符串?

发布于 2024-12-11 09:34:17 字数 459 浏览 0 评论 0原文

我计划将 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 技术交流群。

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

发布评论

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

评论(1

尬尬 2024-12-18 09:34:17

将数字转换为可打印格式时,通常最容易从最后一位数字开始。

考虑将 123 转换为“123”,我们如何获得最后一位数字?它是除以 10(底数)后的余数。因此 123 % 10 给出了 3,而 123 / 10 = 12 则方便地给出了在下一次迭代中使用的正确数字。在 x86 上,“DIV”指令足以为我们提供商和余数(分别在 axdx 中)。剩下的就是在字符串中存储可打印字符。

将所有这些放在一起,您最终会得到如下内容(使用 nasm 语法):

; ConvertNumber
;   Input:
;     ax = Number to be converted
;     bx = Base
;   
;   Output:
;     si = Start of NUL-terminated buffer
;          containing the converted number
;          in ASCII represention.

ConvertNumber:
    push ax            ; Save modified registers
    push bx
    push dx
    mov si, bufferend  ; Start at the end
.convert:
    xor dx, dx         ; Clear dx for division
    div bx             ; Divide by base
    add dl, '0'        ; Convert to printable char
    cmp dl, '9'        ; Hex digit?
    jbe .store         ; No. Store it
    add dl, 'A'-'0'-10 ; Adjust hex digit
.store:
    dec si             ; Move back one position
    mov [si], dl       ; Store converted digit
    and ax, ax         ; Division result 0?
    jnz .convert       ; No. Still digits to convert
    pop dx             ; Restore modified registers
    pop bx
    pop ax
    ret

这需要一个工作缓冲区(当基数 = 2 时为 16,并且为 NUL 终止符提供一个额外字节):

buffer: times 16 db 0
bufferend:
    db 0

添加对有符号数字的支持保留为供读者练习。 此处 与适用于 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 and dx 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):

; ConvertNumber
;   Input:
;     ax = Number to be converted
;     bx = Base
;   
;   Output:
;     si = Start of NUL-terminated buffer
;          containing the converted number
;          in ASCII represention.

ConvertNumber:
    push ax            ; Save modified registers
    push bx
    push dx
    mov si, bufferend  ; Start at the end
.convert:
    xor dx, dx         ; Clear dx for division
    div bx             ; Divide by base
    add dl, '0'        ; Convert to printable char
    cmp dl, '9'        ; Hex digit?
    jbe .store         ; No. Store it
    add dl, 'A'-'0'-10 ; Adjust hex digit
.store:
    dec si             ; Move back one position
    mov [si], dl       ; Store converted digit
    and ax, ax         ; Division result 0?
    jnz .convert       ; No. Still digits to convert
    pop dx             ; Restore modified registers
    pop bx
    pop ax
    ret

This requires a working buffer (16 for the case when base = 2 and an extra byte for the NUL terminator):

buffer: times 16 db 0
bufferend:
    db 0

Adding support for signed numbers is left as an exercise for the reader. Here is roughly the same routine adapted for 64-bit assembly.

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