代码之家  ›  专栏  ›  技术社区  ›  Kpuonyer

如何在程序集中将普通字节转换为ascii?

  •  0
  • Kpuonyer  · 技术社区  · 11 年前

    我正在尝试为我的真实模式操作系统编写一个函数,该函数将转储内存。我希望字节0x0a显示为“0A”,0xf3显示为“F3”,依此类推。我编写的代码使用由“0123456789ABCDEF”组成的表,并使用单个半字节作为偏移量来查找正确的字符。首先,它保存值 al 。下一步它会移动 向右移动4,移除较低的半字节并向下移动较高的半字节。然后将表加载到 di ,并清零 ah 。接下来,它添加了 ax 以查找偏移。然后将值移动到 然后,它遵循相同的步骤,只是使用低半字节而不是高半字节。然而,当我运行它时,它只重复打印“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 回复  |  直到 11 年前
        1
  •  0
  •   Jester    11 年前

    一个问题是 mov al, bl; save al 以及 mov bl, al ; reload al 将操作数反转。

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

        2
  •  0
  •   Community Mohan Dere    8 年前

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

    推荐文章