Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting hex digit to ASCII character

5,317 views
Skip to first unread message

THall91516

unread,
Apr 11, 1996, 3:00:00 AM4/11/96
to
I am having difficulty changing a single hex digit in my al or ah register
to a corresponding ASCII character that I can display on screen. I have
tried to use a string -> '0123456789ABCDEF' in my data segment, but I do
not know how to index the value in my register to get the correct letter.
Can anybody help me. This (if it is not obvious) is my first asm
programs, so I am hoping the solution will not be too difficult.

Thanks in advance

Ari Lukumies

unread,
Apr 12, 1996, 3:00:00 AM4/12/96
to

To convert a hex digit in the lowest 4 bits of al to ASCII:

and al,0fh
add al,'0'
cmp al,'9'
jbe skipit
add al,7
skipit: ; do something

For example, to print a 16-bit hex value stored in AX:

prword: push ax ; save ax
xchg ah,al ; al gets the most significant byte
call prbyte ; print the byte
pop ax ; restore ax (lower byte)
prbyte: push ax ; save ax
shr al,4 ; move most significant nibble to rightmost
call prnib ; print the nibble
pop ax ; restore ax (lower nibble)
prnib: and al,0fh ; mask off unneeded bits
add al,'0' ; add ASCII of 0
cmp al,'9' ; was it A-F?
jbe prout ; no, it was 0-9
add al,7 ; adjust to A-F
prout: mov dl,al ; move character to dl
mov ah,5 ; DOS service: print character
int 21h ; execute
ret ; return to caller

Later,
AriL
--
All my opinions are mine and mine alone.

Rocky

unread,
Apr 14, 1996, 3:00:00 AM4/14/96
to
In article <4kjob9$p...@newsbf02.news.aol.com> thall...@aol.com (THall91516) writes:
>From: thall...@aol.com (THall91516)
>Subject: Converting hex digit to ASCII character
>Date: 11 Apr 1996 15:59:37 -0400

>I am having difficulty changing a single hex digit in my al or ah register
>to a corresponding ASCII character that I can display on screen. I have
>tried to use a string -> '0123456789ABCDEF' in my data segment, but I do
>not know how to index the value in my register to get the correct letter.
>Can anybody help me. This (if it is not obvious) is my first asm
>programs, so I am hoping the solution will not be too difficult.

>Thanks in advance


HEX_OUT: ;OUTPUTS A HEX BYTE IN AL AS 2 ASCII CHARS
PUSH AX ;WITHOUT CORRUPTING AX
MOV AH,AL
ROR AL,4
CALL HEX_NIB
MOV AL,AH
CALL HEX_NIB
POP AX
RET

HEX_NIB: ;OUTPUTS 1 NIBBLE
PUSH AX
AND AL,0FH
CMP AL,0AH
JC HEX_NIB_1

ADD AL,7

HEX_NIB_1:
ADD AL,30H
CALL PRINT_A ;OUTPUT THE CHARACTER TO WHEREVER
POP AX
RET

The table method could be used when needing to convert to DTMF
characters - '0124567890*#ABCD'

Cheers - Rocky

Erwin Paguio

unread,
Apr 14, 1996, 3:00:00 AM4/14/96
to THall91516

In article <4kjob9$p...@newsbf02.news.aol.com>, thall...@aol.com (THall91516) writes:
|> I am having difficulty changing a single hex digit in my al or ah register
|> to a corresponding ASCII character that I can display on screen. I have
|> tried to use a string -> '0123456789ABCDEF' in my data segment, but I do
|> not know how to index the value in my register to get the correct letter.
|> Can anybody help me. This (if it is not obvious) is my first asm
|> programs, so I am hoping the solution will not be too difficult.

There are several ways to solve this. Choose whichever you prefer.

The first and second solution assumes that the value is in AL (from 00h to 0Fh)
and it returns the ASCII char in AL ('0' - '9', 'A' - 'F')

ADD AL, 90h
DAA
ADC AL, 40h
DAA

Don't worry too much on the math. Just try it and see... :-) It's like magic,
right?

The second solution is quite similar (it also makes use of the decimal
instruction) but is even shorter!

CMP AL, 0Ah
SBB AL, 69h
DAS

The two codes above are not my original. I just picked them up from
somewhere. I hope they are useful to you.

The third solution is through the use of tables. With this method, you could
even apply encryption (char substitition or something similar). Anyway, here
goes:

In you data segment, you should have something like this:

Digits DB '0123456789ABCDEF'

Then assuming AL already contains the hex digit, you just need this in your
code:

MOV BX, OFFSET Digits
XLAT

The XLAT (translate instruction) is equivalent to an instruction like
AL = DS:[BX+AL]

Just a note: Make sure AL is within the valid range, otherwise the returned
value might not be what you expected.

If you need any more help, just drop me a line...

|> Thanks in advance

You're welcome...

YO!
Erwin
--
Erwin D. Paguio
http://rh.iist.unu.edu/~ep/ydeeps.htm
Pascal and ASM Enthusiast


Teresa Reiko

unread,
Apr 16, 1996, 3:00:00 AM4/16/96
to
>
> ADD AL, 90h
> DAA
> ADC AL, 40h
> DAA

This is Allison's Algorithm 1.

This is how it works:

0-9 A-F

ADD AL,90h

90-99 9A-9F

DAA

90-99 00-05(c)

ADC AL,40h

D0-D9 41-46

DAA

30-39 41-46
= =
'0'-'9' 'A'-'F'

>
> CMP AL, 0Ah
> SBB AL, 69h
> DAS
>

This is Allison's Algorithm 2.

This is how it works:

0-9 A-F

CMP AL,0Ah

0-9(c) A-F

SBB AL,69h

96-9F(a,c) A1-A6(c)

DAS

30-39 41-46
= =
'0'-'9' 'A'-'F'

I have no idea who this Allison person is.

I think these, along with the explanations, should be
put in the FAQ.

----- for every vision there is an equal and opposite revision ... -----
------------------------------------------------------------------------
Teresa Reiko Chief Programmer, Tenbyte Software tj...@nwlink.com
------------------------------------------------------------------------

0 new messages