; 16bit addition
; R6 R7
; + R4 R5
; = R1 R2 R3
; Add the low bytes R7 and R5, leave the answer in R3
; Add the high bytes R6 and R4, adding any carry from step 1, and leave the answer in R2.
; Put any carry from step 2 in the final byte, R1
ORG 0H
MAIN:
MOV R6,#1AH ;Load the first value into R6 and R7
MOV R7,#44H
MOV R4,#22H ;Load the second value into R4 and R5
MOV R5,#0DBH
LCALL ADD_16
SJMP MAIN
ADD_16:
; step 1
MOV A,R7 ;Move the low-byte into the accumulator
ADD A,R5 ;Add the second low-byte to the accumulator
MOV R3,A ;Move the answer to the low-byte of the result
; step 2
MOV A,R6 ;Move the high-byte into the accumulator
ADDC A,R4 ;Add the second high-byte to the accumulator, plus carry from step 1.
MOV R2,A ;Move the answer to the high-byte of the result
; step 3
MOV A,#00h ;By default, the highest byte will be zero.
ADDC A,#00h ;Add zero, plus carry from step 2.
MOV R1,A ;Move the answer to the highest byte of the result
RET
END
; add two 2 byte numbers
; register operation
ORG 0H
MOV R1,#12
MOV R2,#34
MOV R3,#56
MOV R4,#78 ;3412+7856 = 0AC68
MOV R7,#00 ;3rd byte=0
CLR C
MOV A,R1
ADD A,R3
MOV R5,A
MOV A,R2
ADDC A,R4
MOV R6,A
JNC SKIP
MOV R7,#01 ;3rd byte=1
SKIP: SJMP $
END
Related topics:
8051 Program - arithmetic operation 8bit | 8051 Program - addition 8bit | 8051 Program - subtraction 8bit | 8051 Program - multiplication 8bit | 8051 Program - division 8bit | 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