在x86汇编中打印十六进制值

问题描述 投票:5回答:2

我需要制定一个例程,将内存地址转换为字节字符串。然后,该字符串将作为一个函数的输入,该函数将打印以空值结尾的字符串(我已经可以制作出)。例如,如果我的地址为0x1bf9,则需要在屏幕上打印文本“ 1bf9”。这本书还没有进入32位模式,但是有点暗示我们也需要它。这是我到目前为止的内容:

TABLE:
db "0123456789ABCDEF", 0

STRING:
db 0

hex_to_char:
    lea bx, TABLE
    mov ax, dx

    mov ah, al ;make al and ah equal so we can isolate each half of the byte
    shr ah, 4 ;ah now has the high nibble
    and al, 0x0F ;al now has the low nibble
    xlat ;lookup al's contents in our table
    xchg ah, al ;flip around the bytes so now we can get the higher nibble 
    xlat ;look up what we just flipped
    inc STRING
    mov [STRING], ah ;append the new character to a string of bytes
    inc STRING
    mov [STRING], al ;append the new character to the string of bytes

    ret
assembly printing x86 hex
2个回答
5
投票

这会尝试增加文字标签,这是不正确的。同样,您的STRING内存位置仅分配一个字节(字符),而不分配更大的数字来容纳您想要的字符串的大小。

STRING:
    db 0

    inc STRING   ;THIS WON'T WORK
    mov [STRING], ah ;append the new character to a string of bytes
    inc STRING   ;THIS WON'T WORK
    mov [STRING], al ;append the new character to the string of bytes

中性注释:用于xlat的字符表不需要以零结尾。

另外,我建议您保存并恢复一些寄存器,作为asm编程的好习惯。这样,调用函数就不必担心寄存器在其“后面”被更改。最终,您可能想要这样的东西:

TABLE:
    db "0123456789ABCDEF", 0

hex_to_char:
    push bx

    mov   bx, TABLE
    mov   ax, dx

    mov   ah, al            ;make al and ah equal so we can isolate each half of the byte
    shr   ah, 4             ;ah now has the high nibble
    and   al, 0x0F          ;al now has the low nibble
    xlat                    ;lookup al's contents in our table
    xchg  ah, al            ;flip around the bytes so now we can get the higher nibble 
    xlat                    ;look up what we just flipped

    mov   bx, STRING
    xchg  ah, al
    mov   [bx], ax          ;append the new character to the string of bytes

    pop bx
    ret

    section .bss

STRING:
    resb  50                ; reserve 50 bytes for the string

编辑:根据彼得·科德斯的意见进行一些可取的调整。


0
投票

[请在此页面上查看我的答案,以将EAX中的32位值转换为8个十六进制ASCII字节:Printing out a number in assembly language?

© www.soinside.com 2019 - 2024. All rights reserved.