Link to home
Start Free TrialLog in
Avatar of Maccey21
Maccey21

asked on

A simple hex dump

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.
Avatar of Mike_s
Mike_s

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.

Mike
ASKER CERTIFIED SOLUTION
Avatar of dimitry
dimitry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
          cbw
          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
                das

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 '$'.