gpt4 book ai didi

assembly - 在 x86 程序集中打印十六进制值

转载 作者:行者123 更新时间:2023-12-03 16:17:34 25 4
gpt4 key购买 nike

我需要制作一个将内存地址转换为字节串的例程。然后,该字符串将成为打印以空字符结尾的字符串(我已经能够创建)的函数的输入。例如,如果我有一个地址 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

最佳答案

这试图增加一个文字标签,这是不正确的。此外,您的 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

编辑:根据 Peter Cordes 的输入进行了一些理想的调整。

关于assembly - 在 x86 程序集中打印十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18879672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com