Tuesday, May 31, 2016

8051 Program - 16 bit hex to bcd

; This routine is for 16 bit Hex to BCD conversion
; Accepts a 16 bit binary number in R1,R2 and returns 5 digit BCD in R7,R6,R5,R4,R3( upto 64K )
ORG 0H
Hex2BCD:
MOV R1,#0fFH; MSByte
MOV R2,#0FFH; LSByte
MOV R3,#00D
MOV R4,#00D
MOV R5,#00D
MOV R6,#00D
MOV R7,#00D
MOV B,#10D
MOV A,R2
DIV AB
MOV R3,B
MOV B,#10; R7,R6,R5,R4,R3
DIV AB
MOV R4,B
MOV R5,A
CJNE R1,#0H,HIGH_BYTE; CHECK FOR HIGH BYTE
SJMP ENDD
HIGH_BYTE:
MOV A,#6
ADD A,R3
MOV B,#10
DIV AB
MOV R3,B
ADD A,#5
ADD A,R4
MOV B,#10
DIV AB
MOV R4,B
ADD A,#2
ADD A,R5
MOV B,#10
DIV AB
MOV R5,B
CJNE R6,#00D,ADD_IT
SJMP CONTINUE
ADD_IT:
ADD A,R6
CONTINUE:
MOV R6,A
DJNZ R1,HIGH_BYTE
MOV B, #10D
MOV A,R6
DIV AB
MOV R6,B
MOV R7,A
ENDD:
SJMP $
END



Related topics:
8051 Program - bcd byte   |   8051 Program - 16 bit binary to ascii   |   8051 Program - byte to ascii   |   8051 Program - 5 digit decimal to binary   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment