title: How to convert normal bytes to ascii in assembly?
body: <p>I am trying to write a function for my real mode operating system that will dump memory. I want the byte 0x0a to show up as '0A', 0xf3 to show up as 'F3' and so on and so forth. The code I wrote uses a table that consists of '0123456789ABCDEF', and uses the individual nibble as an offset to find the right character. First it saves the value, which is in <code>al</code>. Next it shifts <code>al</code> right by 4, removing the lower nibble and moving the higher nibble down. It then load the table into <code>di</code>, and zeros out <code>ah</code>. Next, it adds <code>ax</code> to <code>di</code> to find the offset. It then moves the value at <code>di</code> into al, and prints it. It then follows the same steps except that it then uses the low nibble instead of the high nibble. However, when I run it, it only prints out '33' repeatedly, instead of the actual hex numbers. Here is my code:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>What is the problem with it? Thank you in advance.</p>