【问题标题】:How to convert normal bytes to ascii in assembly?如何在汇编中将普通字节转换为 ascii?
【发布时间】:2014-05-06 17:43:48
【问题描述】:

我正在尝试为我的实模式操作系统编写一个转储内存的函数。我希望字节 0x0a 显示为“0A”,0xf3 显示为“F3”等等。我编写的代码使用了一个包含“0123456789ABCDEF”的表,并使用单个半字节作为偏移量来查找正确的字符。首先它保存了al 中的值。接下来,它将al 向右移动 4,移除较低的半字节并将较高的半字节向下移动。然后它将表加载到di,并清零ah。接下来,它将ax 添加到di 以找到偏移量。然后它将di 处的值移动到al 中,并打印出来。然后它遵循相同的步骤,只是它使用低半字节而不是高半字节。但是,当我运行它时,它只会重复打印出“33”,而不是实际的十六进制数字。这是我的代码:

memdump:            
mov si, 0x7000      ;load si
loop:               
    mov al, [si]        ;put the value at si into al
    call printhex       ;call the hex printer
    inc si          ;increment si
    cmp si, 0x7CFF      ;are we done?
    je done         ;yes, return
    jmp loop        ;loop
printhex:           
    mov al, bl      ;save al
    shr al, 4       ;remove the lower nibble and move the higher nibble down
    mov di,hexbuffer    ;put the hexadecimal buffer in di
    xor ah, ah      ;remove everything from ah
    add di, ax      ;add the address to the buffer
    mov al, [di]        ;move the ascii char into al
    mov ah, 0x0E        ;teletype printing for int 0x10
    int 0x10        ;print the character
    mov bl, al      ;reload al
    shl al, 4       ;remove the high nibble
    shr al, 4       ;move the new high nibble down
    mov di, hexbuffer   ;reload the buffer
    xor ah, ah      ;zero out ah
    add di, ax      ;add the offset
    mov al, [di]        ;transfer the ascii char
    mov ah, 0x0E        ;teletype printing for int 0x10
    int 0x10        ;print the char
    mov al, ' '     ;now print a space
    int 0x10        ;print the space
    ret         ;return
done:               
    ret         ;return to kernel
    hexbuffer db '0123456789ABCDEF'

它有什么问题?提前谢谢你。

【问题讨论】:

标签: assembly x86 hex ascii real-mode


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    一个问题是mov al, bl; save almov bl, al ; reload al 的操作数颠倒了。

    如果您已经在编写所谓的操作系统,那么您应该熟悉调试器,这样您就可以修复自己的错误。

    【讨论】:

    • 这么简单的错误!愚蠢的我!我可能应该有调试器,我从没想过。我想我的操作系统有点业余爱好。
    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 2016-07-31
    相关资源
    最近更新 更多