Saturday, April 30, 2016

8051 Program – addition 8bit

; addition of two 8-bit numbers
; R7+R6
; store the result in internal RAM (IRAM)
; and also in external RAM (ERAM)
ORG 0H
ERAM EQU 4000H
MAIN:
MOV R7,#15 ; load data1 in to R7
MOV R6,#20 ; load data2 in to R6
LCALL ADD_8
SJMP MAIN
ADD_8:
MOV R0,#00 ; clear R0
MOV A,R7 ; load data1 in to accumulator
ADD A,R6 ; add data2 with data1
JNC store ; jump if NO carry
INC R0 ; increment R0 if carry is set
store:
MOV DPTR,#ERAM ; point to external RAM
MOVX @DPTR,A ; store the result
INC DPTR ; point to next location
MOV A,R0
MOVX @DPTR,A ; store the carry
RET
END

; addition of two 8-bit numbers
; numbers can be immediate value, in internal RAM or in external RAM
; store the result in internal RAM (IRAM)
; and also in external RAM (ERAM)
ORG 0H
INUM1 EQU 40H
INUM2 EQU 41H
IRESULT EQU 60H
ENUM1 EQU 4000H
ENUM2 EQU 4100H
ERESULT EQU 6000H
DATA1 EQU 15
DATA2 EQU 20
MAIN:
immediate:
MOV R1,#DATA1 ; load data1 in to R1
MOV R2,#DATA2 ; load data2 in to R2
LCALL ADD_8
idirect:
MOV R1,INUM1 ; load data1 in to R1
MOV R2,INUM2 ; load data2 in to R2
LCALL ADD_8
edirect:
MOV DPTR,#ENUM1
MOVX A,@DPTR
MOV R1, A
MOV DPTR,#ENUM2
MOVX A,@DPTR
MOV R2, A
LCALL ADD_8
SJMP MAIN
ADD_8:
MOV R3,#00 ; clear R3 for result
MOV R4,#00 ; clear R4 for carry
MOV A,R1 ; load data1 in to accumulator
ADD A,R2 ; add data2 with data1
MOV R3,A
JNC istore ; jump if NO carry
INC R4 ; increment R4 if carry is set
istore:
MOV A,R3
MOV R0,#IRESULT ; point to internal RAM
MOV @R0,A ; store the result
INC R0 ; point to next location
MOV A,R4
MOV @R0,A ; store the carry
estore:
MOV DPTR,#ERESULT ; point to external RAM
MOV A,R3
MOVX @DPTR,A ; store the result
INC DPTR ; point to next location
MOV A,R4
MOVX @DPTR,A ; store the carry
RET
END



Related topics:
8051 Program - arithmetic operation 8bit   |   8051 Program - subtraction 8bit   |   8051 Program - multiplication 8bit   |   8051 Program - division 8bit   |   8051 Program - addition 16bit   |   8051 Program - subtraction 16bit   |   8051 Program - multiplication 16bit   |   8051 Program - division 16bit   |   8051 Program - addition multibyte   |   8051 Program - multiplication 16bit by 8bit   |   8051 Program - addition 8bit 2digit bcd   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment