Saturday, April 30, 2016

8051 Program – hex to decimal 8bit

;hex to decimal 8bit number
; input: 4000H
; output: 4001H-4003H
ORG 0H
HEX_ADDR EQU 4000H
DEC_ADDR EQU 4001H
MAIN:
MOV DPTR,#HEX_ADDR ; fill memory
MOV A,#0FFH
MOVX @DPTR,A
LCALL CONV
SJMP MAIN
CONV:
MOV DPTR,#HEX_ADDR ;load HEX_ADDR into dptr register
MOVX A,@DPTR ;move data from external memory location to a
MOV R2,A ;move data from a to r2
CLR C ;clear carry bit
SUBB A,#10 ;subtract with borrow, 0ah from a
JC over ;if carry, jump to label over
MOV A,R2 ;move data from r2 to a
SUBB A,#100 ;subtract with borrow, 64h from a
JC d1 ; if carry, jump to label d1
MOV A,R2 ;move data from r2 to a
MOV B,#100 ;move immediate data 64h to b
DIV AB ;divide a by b
INC DPTR ;increment dptr
MOVX @DPTR,A ; move data from external memory location to a
MOV A,B ;move data from b to a
MOV R3,A ;move data from a to r3
CLR C ; clear carry bit
SUBB A,#10 ;subtract with borrow, 0ah from a
JC d2 ;if carry, jump to label d2
MOV B,#10 ;Load b with immediate data 0ah
MOV A,R3 ;move data from r3 to a
DIV AB ;divide a by b
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
MOV A,B ;move data from b to a
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
SJMP return
d2: MOV A,R3 ;move data from r3 to a
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
SJMP return
d1: MOV A,R2 ;move data from r2 to a
MOV B,#10;load b with immediate data 0ah
DIV AB ;divide a by b
INC DPTR ; increment dptr
MOVX @DPTR,A ;move data from a to external memory location
MOV A,B ;move data from b to a
INC DPTR ;increment dptr
MOVX @DPTR,A ;move data from a to external memory location
SJMP return
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
return:
RET
END

; hex to decimal
ORG 0H
MOV R0,#0FFh ;Input no
MOV A,R0
MOV B,#64h ;B=64h
DIV AB
MOV R1,A ;First dgt
MOV A,B
MOV B,#0Ah ;B=0Ah
DIV AB
MOV R2,A ;Second dgt
MOV A,B
MOV R3,B ;Third dgt
MOV A,R2 ; Pack R2 & R3 to R2
SWAP A
ADD A,R3
MOV R2,A
SJMP $
END



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