Saturday, April 30, 2016

8051 Program – hex to bcd 12bit

; 12bit hex to bcd conversion
; input: R6 (MSB) & R7 (LSB)
;output: R0 (MSB) & R1 (LSB)
; step1:convert HEX LSB (R7) to BCD
; step2:check if the HEX MSB (R6) is 0
;if not zero we add 256 to the BCD
;decrement the HEX MSB (R6) until it becomes zero
ORG 0H
HEX_ADDR EQU 4000H
MAIN:
MOV R7,#0Ah
MOV R6,#0BCh
LCALL CONV
SJMP MAIN
CONV:
MOV R0,#00H
MOV R1,#00H
CLR C
MOV A,R7
MOV B,#100;divide LSB by 100
DIV AB
MOV R0,A;save the 100th place
MOV A,B
MOV B,#10;divide LSB by 10
CLR C
DIV AB
SWAP A
ORL A,B
MOV R1,A;save the units & tenth place
MOV A,R6
CJNE A,#00h,conv_msb;check if the MSB is 0 if not then continue
SJMP return
;add 256 to the BCD number & decrement the HEX MSB by 1 until it becomes 0
conv_msb:
CLR C
MOV A,R1
ADD A,#56H
DA A
MOV R1,A
JNC d3
CLR C
MOV A,R0
ADD A,#01H
DA A
MOV R0,A
d3:
CLR C
MOV A,R0
ADD A,#02H
DA A
MOV R0,A
DJNZ r6,conv_msb
return: RET
END



Related topics:
8051 Program - decimal to hex   |   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 - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment