如何在汇编中将普通字节转换为ascii?

时间:2014-03-27 15:57:33

标签: assembly x86 hex ascii real-mode

我正在尝试为我的实模式操作系统编写一个将转储内存的函数。我希望字节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'

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

2 个答案:

答案 0 :(得分:0)

一个问题是mov al, bl; save almov bl, al ; reload al的操作数相反。

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

答案 1 :(得分:0)

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

相关问题