;*CONVERTS 5 DIGIT DECIMAL NUMBER (12345) INTO 3 BYTES BINARY NUMBER
BIN_1 EQU 42H ; LSB
BIN_2 EQU 41H ; MIDDLE SB
BIN_3 EQU 40H ; MSB
DEC_1 EQU 50H ; TEN THOUSANDS OF 5 DIGIT DECIMAL NUMBER
DEC_2 EQU 51H ; THOUSANDS OF 5 DIGIT DECIMAL NUMBER
DEC_3 EQU 52H ; HUNDREDS OF 5 DIGIT DECIMAL NUMBER
DEC_4 EQU 53H ; TENS OF 5 DIGIT DECIMAL NUMBER
DEC_5 EQU 54H ; UNITS OF 5 DIGIT DECIMAL NUMBER
FACTOR EQU 60H ; AUXILLIARY VARIABLE
;MAIN CODE:
;EXAMPLE;
;TO CONVERT 5 DIGIT DECIMAL NUMBER (12345) INTO 3 BYTES BINARY NUMBER
MOV R2,#00 ; NEW LINE
; \
MOV BIN_1,#00 ; |
MOV BIN_2,#00 ; | 3 BYTES BINARY OUTPUT
MOV BIN_3,#00 ; |
; /
MOV DEC_1,#05D ; UNITS
MOV DEC_2,#04D ; TENS
MOV DEC_3,#03D ; HUNDREDS
MOV DEC_4,#02D ; THOUSANDS (1st Decimal place)
MOV DEC_5,#01D ; TEN THOUSANDS (2nd Decimal place)
MOV A,DEC_1 ; TAKE THE UNITS DIRECTLY AS IT IS TO THE LSB
MOV BIN_1,A
MOV A,DEC_2 ; TAKE THE TENS BY MULTIPLYING WITH #10D AND
MOV B,#10 ; AND ADDING TO THE LSB
MUL AB
ADD A,BIN_1
MOV BIN_1,A
MOV R0,#52H ; THE ADDRESS OF 3rd DIGIT
MOV FACTOR,#00D ; INITIALIZE AS ZERO
MOV A,@R0 ; TAKE THE 3rd DIGIT INTO R1 FOR PROCESSING
MOV R1,A
CJNE R1,#00,LOOP ; SKIP IF THE DIGIT IS A ZERO
SJMP ITS_ZERO
LOOP:
MOV A,#100D ; ADD CONSTANT 100D TO LSB AS MANY TIMES
CLR C
ADD A,BIN_1
MOV BIN_1,A
JNC NO_EXCEED
INC BIN_2 ; INCREASE THE NEXT HIGHER BYTES WHEN THE LSB
MOV A,BIN_2 ; OVERFLOWS
CJNE A,#00H,NO_EXCEED
INC BIN_3
NO_EXCEED:
DJNZ R1,LOOP
;-----------
CJNE R2,#00,CONTINUE
SJMP ITS_ZERO
CONTINUE:
DEC R2
SJMP LOOP
;-----------
ITS_ZERO:
INC R0 ; GET NEXT DIGIT WHEN THE LOOP EXHAUSTS
CJNE R0,#55H,PROCEED ; GET OUT WHEN ALL THE 3 DIGITS HAVE BEEN
; WORKED UPON. (1st and 2nd digits are used
; before the loop)
SJMP OUT_LOOP
PROCEED:
INC FACTOR ; CALCULATE AS HOW MANY TIMES THE CONSTANT
MOV A,@R0 ; 100D HAS TO BE ADDED TO LSB DEPENDING ON
CJNE A,#00,NOT_ZERO ; THE VALUE OF THE DIGIT ACQUIRED BY @R0
SJMP ITS_ZERO
NOT_ZERO:
PUSH FACTOR
MULTIPLY:
MOV B,#10D
CLR OV
MUL AB
JNB OV,SINGLE
PUSH ACC
MOV A,B
ADD A,R2
MOV R2,A
POP ACC
SINGLE:
DJNZ FACTOR,MULTIPLY
MOV R1,A
POP FACTOR
SJMP LOOP
OUT_LOOP:
MOV R1,BIN_2
MOV R2,BIN_1
MOV R0,BIN_3
END
;IDEA:
; LSB
;+ (DEC_1)
;+ (DEC_2 x 10D)
;+ (DEC_3 x 1 x 100D)
;+ (DEC_4 x 10 x 100D)
;+ (DEC_5 x 100 x 100D)
Related topics:
8051 Program - 16 bit hex to bcd | 8051 Program - bcd byte | 8051 Program - 16 bit binary to ascii | 8051 Program - byte to ascii | 8051 Program - memory subroutines | 8051 Program - math subroutines | 8051 Program - conversion subroutines
List of topics: 8051
No comments:
Post a Comment