Thanks in advance
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.
>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
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
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
------------------------------------------------------------------------