title: Printing hex values in x86 assembly
body: <p>I need to make a routine that will convert a memory address into a string of bytes. That string would then be the input for a function that prints null-terminated strings (which I was already able to make). For example, if I have an address 0x1bf9 I need to print the text "1bf9" to the screen. The book hasn't gone into 32 bit mode yet, but it kind of hinted that we would need it for that too. This is what I have so far: </p>
  
  <pre><code>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
  </code></pre>