- 8051

8051 code to convert hexadecimal to ascii value

In this article let’s learn how to convert Hexadecimal value to Ascii equivalent number.

Hexadecimalvalue             Asciivalue
01H             31H
02H             32H
03H             33H
04H             34H
05H             35H
06H             36H
07H             37H
08H             38H
09H             39H
0AH             41H
0BH             42H
0CH             43H
0DH             44H
0EH             45H
0FH             46H

Algorithm:

 1)Initialize R0 with number which is required to find equivalent Ascii code.
 2)Compare it with 0AH and jump to label1 if it is equal.
3)Compare the carry bit to find which is greater and lesser.
4)If the carry bit is not set(it implies it is greater)jump to label2.
 5)If it is lesser add the number with 30H.
6)If it is greater add the number with 37H.

Code goes here:



ORG 0000h
LJMP MAIN
ORG 40h
MAIN:  MOV R0,#01H ; move the numbers to be converted
MOV A,R0
CJNE A,#0AH,LABEL1  ; compare the number with A
LABEL1:JNC LABEL2 ;If the number is greater than A jump to label2
ADD A,#30H ; If the number is lesser than A add with 30H
SJMP STOP
LABEL2:ADD A,#37H   ;If the number is greater than A add with 37H
STOP:
       END

Leave a Reply