Saturday, April 30, 2016

8051 Program – subtraction 16bit

;16bit subtraction
; R6 R7
; - R4 R5
; = R2 R3
; Subtract the low bytes R5 from R7, leave the answer in R3
; Subtract the high byte R4 from R6, less any borrow, and leave the answer in R2.
ORG 0H
MAIN:
MOV R6,#22H ;Load the first value into R6 and R7
MOV R7,#0DBH
MOV R4,#1AH ;Load the second value into R4 and R5
MOV R5,#0F9H
LCALL SUB_16
SJMP MAIN
SUB_16:
; step 1
MOV A,R7 ;Move the low-byte into the accumulator
CLR C ;Always clear carry before first subtraction
SUBB A,R5 ;Subtract the second low-byte from 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
SUBB A,R4 ;Subtract the second high-byte from the accumulator
MOV R2,A ;Move the answer to the high-byte of the result
RET
END

; subtract 2 byte number from another 2 byte number
; register operation
ORG 0H
MOV R1,#56
MOV R2,#78
MOV R3,#12
MOV R4,#34;7856-3412 = 4444
CLR C
MOV A,R1
SUBB A,R3
MOV R5,A
MOV A,R2
SUBB A,R4
MOV R6,A
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 - addition 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