Thursday, May 26, 2016

8051 Program - signed 16bit multiply divide

; subroutine MULDIV
; 16-Bit x 16-Bit to 32-Bit Product Signed Multiply followed by
; 32-Bit / 16-Bit to 32-Bit Quotient & remainder signed Divide
; 2's Complement Format
;
; input: r1, r0 = multiplicand X
; r3, r2 = multiplier Y
; r5, r4 = divisor Z
;
; output: r3, r2, r1, r0 = quotient Q of division Q = (X x Y) / Z
; r7, r6, r5, r4 = remainder
; Carry C is set if Z = 0, i.e. divide by 0 attempted
;
; calls: UMUL16, UDIV32, Cr0r1, Cr2r3, Cr4r5, Mr0r3
;
; alters: acc, flags, Bits 21H & 22H

MULDIV:anl PSW, #0E7H ;Register Bank 0
mov a, r4 ;get divisor high byte
orl a, r5 ;OR with low byte
jnz muld_OK ;divisor OK if not 0
setb C ;else, overflow
ret
muld_OK:lcall Cr0r1 ;2's comp -> Mag/Sign
lcall Cr2r3 ;2's comp -> Mag/Sign
lcall UMUL16
jb 21H, divn1 ;test X sign
divn:lcall Cr4r5 ;2's comp -> Mag/Sign
lcall UDIV32
lcall Mr0r3 ;Mag/Sign -> 2's Comp
clr C ;divisor is not 0v
ret
divn1:jbc 22H, divn ;test Y sign
setb 22H
sjmp divn


Source: Maths Subroutines for the 8051 microcontroller, W.G.Marshall 2002



Related topics:
8051 Program - signed 16bit multiply Accumulate table   |   8051 Program - signed 16bit multiply Accumulate   |   8051 Program - unsigned 8bit multiplication   |   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