; subroutine DIV16
; 16-Bit / 16-Bit to 16-Bit Quotient & remainder signed Divide
; 2's Complement Format
;
; input: r1, r0 = Dividend X
; r3, r2 = Divisor Y
;
; output: r1, r0 = quotient Q of division Q = X / Y
; r3, r2 = remainder
; Carry C is set if Y = 0, i.e. divide by 0 attempted
;
; calls: UDIV16, Cr0r1, Cr2r3, Mr0r1
;
; alters: acc, r4, r5, r6, r7, flags, Bits 21H & 22H
DIV16: anl PSW, #0E7H ;Register Bank 0
mov a, r3 ;get divisor high byte
orl a, r2 ;OR with low byte
jnz div_OK ;divisor OK if not 0
setb C ;else, overflow
ret
div_OK: push dpl
push dph
push b
acall Cr0r1 ;2's comp -> Mag/Sign
acall Cr2r3 ;2's comp -> Mag/Sign
acall UDIV16
acall Mr0r1 ;Mag/Sign -> 2's Comp
clr C
pop b
pop dph
pop dpl
ret ;done
Source: Maths Subroutines for the 8051 microcontroller, W.G.Marshall 2002
Related topics:
8051 Program - signed 8bit division | 8051 Program - unsigned 8bit division | 8051 Program - unsigned 16bit division | 8051 Program - signed 32bit division | 8051 Program - unsigned 32bit division | 8051 Program - memory subroutines | 8051 Program - math subroutines | 8051 Program - conversion subroutines
List of topics: 8051
No comments:
Post a Comment