logo

Select Sidearea

Populate the sidearea with useful widgets. Itโ€™s simple to add images, categories, latest post, social media icon links, tag clouds, and more.
[email protected]
+1234567890
 

Subroutine Multiplication for 8051 Microcontroller

;==================================================================
; subroutine MUL8
; 8-Bit x 8-Bit to 16-Bit Product Signed Multiply
; 2’s Complement format

; input: r0 = multiplicand X
; r1 = multiplier Y

; output: r1, r0 = product P = X x Y.

; calls: UMUL8, Cr0, Cr1, Mr0r1

; alters: acc, C, Bits 21H & 22H
;==================================================================

MUL8:      anl PSW, #0E7H ; Register Bank 0           acall Cr0 ; 2's comp -> Mag/Sign           acall Cr1 ; 2's comp -> Mag/Sign           acall UMUL8           acall Mr0r1 ; Mag/Sign -> 2's Comp           ret
;==================================================================; subroutine UMUL8; 8-Bit x 8-Bit to 16-Bit Product Unsigned Multiply;; input: r0 = multiplicand X; r1 = multiplier Y;; output: r1, r0 = product P = X x Y.;; alters: acc;==================================================================
UMUL8:     push b           mov a, r0 ; read X and ...           mov b, r1 ; ... Y           mul ab ; multiply X and Y           mov r1, b ; save result high ...           mov r0, a ; ... and low           pop b           ret
;====================================================================; subroutine MUL816; 8-Bit x 16-Bit to 32-Bit Product signed Multiply; 2's Complement format           ;; input: r0 = multiplicand X; r3, r2 = multiplier Y;; output: r3, r2, r1, r0 = product P = X x Y (r3 = sign extension)           ;; calls: Cr0, Cr2r3, Mr0r3           ;; alters: acc, C, Bits 21H & 22H;====================================================================
MUL816:    push b           anl PSW, #0E7H ; Register Bank 0           acall Cr0 ; 2's comp -> Mag/Sign           acall Cr2r3 ; 2's comp -> Mag/Sign           mov a, r0 ; load X low byte into acc           mov b, r2 ; load Y low byte into B           mul ab ; multiply           push acc ; stack result low byte           push b ; stack result high byte           mov a, r0 ; load X into acc again           mov b, r3 ; load Y high byte into B           mul ab ; multiply           pop 00H ; recall X*YL high byte           add a, r0 ; add X*YL high and X*YH low           mov r1, a ; save result           clr a ; clear accumulator           addc a, b ; a = b + carry flag           mov r2, a ; save result           pop 00H ; get low result           mov r3, #0           acall Mr0r3 ; Mag/Sign -> 2's Comp           pop b           ret
;====================================================================; subroutine MUL16; 16-Bit x 16-Bit to 32-Bit Product Signed Multiply; 2's Complement format;; input: r1, r0 = multiplicand X; r3, r2 = multiplier Y;; output: r3, r2, r1, r0 = product P = X x Y;; calls: UMUL16, Cr0r1, Cr2r3, Mr0r3;; alters: acc, C, Bits 21H & 22H;====================================================================
MUL16:     anl PSW, #0E7H ; Register Bank 0           acall Cr0r1 ; 2's comp -> Mag/Sign           acall Cr2r3 ; 2's comp -> Mag/Sign           acall UMUL16           acall Mr0r3 ; Mag/Sign -> 2's Comp           ret
;====================================================================; subroutine UMUL16; 16-Bit x 16-Bit to 32-Bit Product Unsigned Multiply;; input: r1, r0 = multiplicand X; r3, r2 = multiplier Y;; output: r3, r2, r1, r0 = product P = X x Y;; alters: acc, C;====================================================================
UMUL16:    push B           push dpl           mov a, r0           mov b, r2           mul ab ; multiply XL x YL           push acc ; stack result low byte           push b ; stack result high byte           mov a, r0           mov b, r3           mul ab ; multiply XL x YH           pop 00H           add a, r0           mov r0, a           clr a           addc a, b           mov dpl, a           mov a, r2           mov b, r1           mul ab ; multiply XH x YL           add a, r0           mov r0, a           mov a, dpl           addc a, b           mov dpl, a           clr a           addc a, #0           push acc ; save intermediate carry           mov a, r3           mov b, r1           mul ab ; multiply XH x YH           add a, dpl           mov r2, a           pop acc ; retrieve carry           addc a, b           mov r3, a           mov r1, 00H           pop 00H ; retrieve result low byte           pop dpl           pop B           ret
;=================================================================
; subroutine Cr0
; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion

; input:     r0 = signed byte

; output:    r0 = magnitude
;            Bit 21H = sign (21H is set if r0 is a negative number)

; alters:    acc
;=================================================================
Cr0:       mov a, r0 ; read X into accumulator           jb acc.7, Cr0a ; X is negative if bit 7 is 1           clr 21H ; clear sign bit if 'positive'           ret ; done
Cr0a:      cpl a ; X negative, find abs value           inc a ; XA = complement(XT)+1           mov r0, a ; save magnitude           setb 21H ; set sign bit if 'negative'           ret
;=================================================================; subroutine Cr1; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion ;; input: r1 = signed byte;; output: r1 = magnitude; Bit 22H = sign (22H is set if r1 is a negative number);; alters: acc;=================================================================
Cr1: mov a, r1 ; read X into accumulator           jb acc.7, Cr1a ; X is negative if bit 7 is 1           clr 22H ; clear sign bit if 'positive'           ret ; done
Cr1a: cpl a ; X negative, find abs value           inc a ; XA = complement(XT)+1           mov r1, a ; save magnitude           setb 22H ; set sign bit if 'negative'           ret;===================================================================           ; subroutine Cr0r1           ; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion           ;           ; input: r1, r0 = signed word           ;           ; output: r1, r0 = magnitude           ; Bit 21H = sign (21H is set if negative number)           ;           ; alters: acc, C;===================================================================
Cr0r1: mov a, r1 ; high byte into accumulator           jb acc.7, c0a ; negative if bit 7 is 1           clr 21H ; clear sign bit if 'positive'           ret ; done
c0a: setb 21H ; set sign flag           mov a, r0 ; number is negative           cpl a ; complement           add a, #1 ; and add +1           mov r0, a            mov a, r1 ; get next byte           cpl a ; complement           addc a, #0           mov r1, a           ret;====================================================================           ; subroutine Cr2r3           ; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion           ;           ; input: r3, r2 = signed word           ;           ; output: r3, r2 = magnitude           ; Bit 22H = sign (22H is set if negative number)           ;           ; alters: acc, C;====================================================================
Cr2r3: mov a, r3 ; read high into accumulator           jb acc.7, c1a ; negative if bit 7 is 1           clr 22H ; clear sign bit if 'positive'           ret ; done
c1a: setb 22H ; set sign flag           mov a, r2 ; number is negative           cpl a ; complement           add a, #1 ; and add +1           mov r2, a            mov a, r3 ; get next byte           cpl a ; complement           addc a, #0           mov r3, a           ret
;====================================================================           ; subroutine Cr4r5           ; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion           ;           ; input: r5, r4 = signed word           ;           ; output: r5, r4 = magnitude           ; Bit 22H = sign (22H is set if negative number)           ;           ; alters: acc, C;====================================================================
Cr4r5: mov a, r5 ; read high into accumulator           jb acc.7, c3a ; negative if bit 7 is 1           clr 22H ; clear sign bit if 'positive'           ret ; done
c3a: setb 22H ; set sign flag           mov a, r4 ; number is negative           cpl a ; complement           add a, #1 ; and add +1           mov r4, a            mov a, r5 ; get next byte           cpl a ; complement           addc a, #0           mov r5, a           ret
;====================================================================           ; subroutine Cr0r3           ; 32-Bit 2's Complement -> magnitude / Sign Bit Conversion           ;           ; input: r3, r2, r1, r0 = signed word           ;           ; output: r3, r2, r1, r0 = magnitude           ; Bit 21H = sign (21H is set if negative number)           ;           ; alters: acc;====================================================================
           Cr0r3: mov a, r3 ; read high into accumulator           jb acc.7, c2a ; negative if bit 7 is 1           clr 21H ; clear sign flag if 'positive'           ret ; done
c2a: setb 21H ; set sign flag           mov a, r0 ; number is negative           cpl a ; complement           add a, #1 ; and add +1           mov r0, a            mov a, r1 ; get next byte           cpl a ; complement           addc a, #0           mov r1,a           mov a, r2 ; get next byte           cpl a ; complement           addc a, #0           mov r2,a           mov a, r3 ; get next byte           cpl a ; complement           addc a, #0           mov r3, a           ret ; done
;==================================================================           ; subroutine Mr0           ; 8-Bit magnitude / Sign Bit -> 2's Complement Conversion           ;           ; input: r0 = magnitude           ; Bits 21H & 22H = sign bits of operands X and Y           ; (set if negative)           ;           ; output: r0 = signed byte           ;           ; alters: acc;==================================================================
Mr0: jb 21H, Mr0b ; test X sign           jb 22H, Mr0a ; test Y sign           ret
Mr0b: jnb 22H, Mr0a           ret
Mr0a: mov a, r0 ; if r0 negative, get abs value           cpl a ; complement magnitude of X           inc a ; r0 = complement(r0)+1           mov r0, a ; save in 2's complement           ret ; done
;====================================================================           ; subroutine Mr0r1           ; 16-Bit magnitude / Sign Bit -> 2's Complement Conversion           ;           ; input: r1, r0 = magnitude           ; Bits 21H & 22H = sign bits of operands X and Y           ; (set if negative)           ;           ; output: r1, r0 = signed word           ;           ; alters: acc, C;====================================================================
Mr0r1: jb 21H, Mr0r1b ; test X sign           jb 22H, Mr0r1a ; test Y sign           ret
Mr0r1b: jnb 22H, Mr0r1a           ret
Mr0r1a: mov a, r0 ; negate number           cpl a ; complement           add a, #1 ; and add +1           mov r0, a            mov a, r1 ; get next byte           cpl a ; complement           addc a, #0           mov r1, a           ret
;====================================================================           ; subroutine Mr0r3           ; 32-Bit magnitude / Sign Bit -> 2's Complement Conversion           ;           ; input: r3, r2, r1, r0 = magnitude           ; Bits 21H & 22H = sign bits of operands X and Y           ; (set if negative)           ;           ; output: r3, r2, r1, r0 = signed word           ;           ; alters: acc, C;====================================================================
Mr0r3: jb 21H, Mr0r3b ; test X sign           jb 22H, Mr0r3a ; test Y sign           ret
Mr0r3b: jnb 22H, Mr0r3a           ret
Mr0r3a: mov a, r0 ; negate number           cpl a ; complement           add a, #1 ; and add +1           mov r0, a            mov a, r1 ; get next byte           cpl a ; complement           addc a, #0           mov r1, a           mov a, r2 ; get next byte           cpl a ; complement           addc a, #0           mov r2, a           mov a, r3 ; get next byte           cpl a ; complement           addc a, #0           mov r3, a           ret ; done