;16bit division
; R1 R0
; / R3 R2
; = R3 R2
; shift left the divisor such that the number of digits
; in the divisor is the same as the number of digits in the dividend
; shift right the divisor and substract this shifted divisor from the dividend
; repeat the process again until the divisor has shifted into its original position
ORG 0H
MAIN:
MOV R0,#0B3H ;Load the divident value into R6 and R7
MOV R1,#00H
MOV R2,#08H ;Load the divisor value into R4 and R5
MOV R3,#00H
LCALL DIV_16
SJMP MAIN
DIV_16:
CLR C ;Clear carry initially
MOV R4,#00h ;Clear R4 working variable initially
MOV R5,#00h ;CLear R5 working variable initially
MOV B,#00h ;Clear B since B will count the number of left-shifted bits
lshift:
INC B ;Increment counter for each left shift
MOV A,R2 ;Move the current divisor low byte into the accumulator
RLC A ;Shift low-byte left, rotate through carry to apply highest bit to high-byte
MOV R2,A ;Save the updated divisor low-byte
MOV A,R3 ;Move the current divisor high byte into the accumulator
RLC A ;Shift high-byte left high, rotating in carry from low-byte
MOV R3,A ;Save the updated divisor high-byte
JNC lshift ;Repeat until carry flag is set from high-byte
rshift: ;Shift right the divisor
MOV A,R3 ;Move high-byte of divisor into accumulator
RRC A ;Rotate high-byte of divisor right and into carry
MOV R3,A ;Save updated value of high-byte of divisor
MOV A,R2 ;Move low-byte of divisor into accumulator
RRC A ;Rotate low-byte of divisor right, with carry from high-byte
MOV R2,A ;Save updated value of low-byte of divisor
CLR C ;Clear carry, we don't need it anymore
MOV 07h,R1 ;Make a safe copy of the dividend high-byte
MOV 06h,R0 ;Make a safe copy of the dividend low-byte
MOV A,R0 ;Move low-byte of dividend into accumulator
SUBB A,R2 ;Dividend - shifted divisor = result bit (no factor, only 0 or 1)
MOV R0,A ;Save updated dividend
MOV A,R1 ;Move high-byte of dividend into accumulator
SUBB A,R3 ;Subtract high-byte of divisor (all together 16-bit substraction)
MOV R1,A ;Save updated high-byte back in high-byte of divisor
JNC result ;If carry flag is NOT set, result is 1
MOV R1,07h ;Otherwise result is 0, save copy of divisor to undo subtraction
MOV R0,06h
result:
CPL C ;Invert carry, so it can be directly copied into result
MOV A,R4
RLC A ;Shift carry flag into temporary result
MOV R4,A
MOV A,R5
RLC A
MOV R5,A
DJNZ B,rshift ;Now count backwards and repeat until "B" is zero
MOV R3,05h ;Move result to R3/R2
MOV R2,04h ;Move result to R3/R2
RET
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 - subtraction 16bit | 8051 Program - multiplication 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