Saturday, April 30, 2016

8051 Program – decimal to hex

;decimal to hex conversion
; dec: 4000H
; hex: 4001H
ORG 0H
DATA_ADDR EQU 4000H
MAIN:
MOV DPTR,#DATA_ADDR
MOV A,#99
MOVX @DPTR,A
LCALL CONV
SJMP MAIN
CONV:
MOV DPTR,#DATA_ADDR ; Load DATA_ADDR into dptr register
MOVX A,@DPTR ; move data from external memory location to a
MOV R2,A ;move data from r2 to a
CLR C ;clear carry bit
SUBB A,#0AH ;subtract with borrow, 0ah from a
JC over ;if carry, jump to label over
MOV A,R2 ;move data from r2 to a
ANL A,#0F0H ;and a with 0f0h
SWAP A ;swap a
MOV B,#0AH ;move 0ah to b
MUL AB ;multiply and b
MOV R3,A ;move data from a to r3
MOV A,R2 ;move data from r2 to a
ANL A,#0FH ;move 0fh to a
ADD A,R3 ;add a and r3
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
SJMP here
over: MOV A,R2 ;move data from r2 to a
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
here:
RET
END

; decimal to hex
ORG 0H
MOV R0,#16 ;R0=Input byte
MOV A,R0
ANL A,#0F0h
SWAP A
MOV B,#0Ah
MUL AB
MOV R1,A
MOV A,R0
ANL A,#0Fh
ADD A,R1
MOV R1,A ;R1=Output byte
SJMP $
END



Related topics:
8051 Program - hex to decimal 8bit   |   8051 Program - decimal to ascii   |   8051 Program - ascii to decimal   |   8051 Program - hex to ascii   |   8051 Program - ascii to hex   |   8051 Program - bcd to ascii   |   8051 Program - ascii to bcd   |   8051 Program - bcd to hex 8bit   |   8051 Program - hex to bcd 8bit   |   8051 Program - hex to bcd 12bit   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment