A simple hex dump

Maccey21 used Ask the Experts™
I am having increased trouble in converting a simple string from ascii to hex format and outputting to screen.
I need some simple code to show me how it is done. I do have an idea of how to do it but cant put code on to it.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Read each character into a byte register. Then convert both the high and low nibbles (4 bits) into hex and output them. To separate the high and low nibbles you just need to do an AND with 0Fh to get the low part, and do an AND with 0F0h then shift right by 4 bits for the high part.


First take a charcter form your buffer
something like mov al,[si] ; where si is a pointer to your buffer.
This assumes that al contains ascii "0 - 9" "A - F' but also works with 'a - f' lower case.

          add     al,40h
          and     ah,09h
          add     al,ah
          and     al,0Fh

al now has the hex nibble (4 bits) ie. 0 to 0fh
you can shift it left 4 places to make it the high nibble

and to go back the other way assume al has a value between 0 and 0fh ( 4 bits )

                cmp   al,10
                sbb   al,69h

al now has '0 - 9 ... A - F' ascii

put this byte for byte in your output buffer. and make it a string. With dos INT 21h subfunction 9, use dx reg. as a pointer to a ascii string. the last character in the string must be a '$'.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial