13.2 Operation

8x8 Unsigned Multiply Routine shows the instruction sequence for an 8x8 unsigned multiplication. Only one instruction is required when one of the arguments is already loaded in the WREG register.

8x8 Signed Multiply Routine shows the sequence to do an 8x8 signed multiplication. To account for the sign bits of the arguments, each argument’s Most Significant bit (MSb) is tested and the appropriate subtractions are done.

8x8 Unsigned Multiply Routine


        MOVF    ARG1, W   ;

        MULWF   ARG2      ; ARG1 * ARG2 -> PRODH:PRODL

8x8 Signed Multiply Routine


        MOVF    ARG1, W
        MULWF   ARG2       ; ARG1 * ARG2 -> PRODH:PRODL
        BTFSC   ARG2, SB   ; Test Sign Bit
        SUBWF   PRODH, F   ; PRODH = PRODH - ARG1
        MOVF    ARG2, W
        BTFSC   ARG1, SB   ; Test Sign Bit
        SUBWF   PRODH, F   ; PRODH = PRODH - ARG2
Table 13-1. Performance Comparison for Various Multiply Operations
RoutineMultiply MethodProgram
Memory
(Words)Cycles
(Max)Time
@ 64 MHz@ 40 MHz@ 10 MHz@ 4 MHz
8x8 unsignedWithout hardware multiply13694.3 μs6.9 μs27.6 μs69 μs
Hardware multiply1162.5 ns100 ns400 ns1 μs
8x8 signedWithout hardware multiply33915.7 μs9.1 μs36.4 μs91 μs
Hardware multiply66375 ns600 ns2.4 μs6 μs
16x16 unsignedWithout hardware multiply2124215.1 μs24.2 μs96.8 μs242 μs
Hardware multiply28281.8 μs2.8 μs11.2 μs28 μs
16x16 signedWithout hardware multiply5225415.9 μs25.4 μs102.6 μs254 μs
Hardware multiply35402.5 μs4.0 μs16.0 μs40 μs

16 x 16 Unsigned Multiply Routine shows the sequence to do a 16 x 16 unsigned multiplication. The equation below shows the algorithm that is used. The 32-bit result is stored in four registers (RES[3:0]).

16 x 16 Unsigned Multiplication Algorithm

RES3:RES0=ARG1H:ARG1LARG2H:ARG2L=(ARG1HARG2H216)+(ARG1HARG2L28)+(ARG1LARG2H28)+(ARG1LARG2L)

16 x 16 Unsigned Multiply Routine


        MOVF    ARG1L, W
        MULWF   ARG2L           ; ARG1L * ARG2L → PRODH:PRODL
        MOVFF   PRODH, RES1     ;
        MOVFF   PRODL, RES0     ;
;
        MOVF    ARG1H, W        ;
        MULWF   ARG2H           ; ARG1H * ARG2H → PRODH:PRODL
        MOVFF   PRODH, RES3     ;
        MOVFF   PRODL, RES2     ;
;
        MOVF    ARG1L, W
        MULWF   ARG2H           ; ARG1L * ARG2H → PRODH:PRODL
        MOVF    PRODL, W        ;
        ADDWF   RES1, F         ; Add cross products
        MOVF    PRODH, W        ;
        ADDWFC  RES2, F         ;
        CLRF    WREG            ;
        ADDWFC  RES3, F         ;
;
        MOVF    ARG1H, W        ;
        MULWF   ARG2L           ; ARG1H * ARG2L → PRODH:PRODL
        MOVF    PRODL, W        ;
        ADDWF   RES1, F         ; Add cross products
        MOVF    PRODH, W        ; 
        ADDWFC  RES2, F         ;
        CLRF    WREG            ;
        ADDWFC  RES3, F         ;

16 x 16 Signed Multiply Routine shows the sequence to do a 16 x 16 signed multiply. The equation below shows the algorithm used. The 32-bit result is stored in four registers (RES[3:0]). To account for the sign bits of the arguments, the MSb for each argument pair is tested and the appropriate subtractions are done.

16 x 16 Signed Multiplication Algorithm

RES3:RES0=ARG1H:ARG1LARG2H:ARG2L=(ARG1HARG2H216)+(ARG1HARG2L28)+(ARG1LARG2H28)+(ARG1LARG2L)+(1ARG2H[7]ARG1H:ARG1L216)+(1ARG1H[7]ARG2H:ARG2L216)

16 x 16 Signed Multiply Routine


        MOVF    ARG1L, W
        MULW    ARG2L           ; ARG1L * ARG2L → PRODH:PRODL 
        MOVF    PRODH, RES1     ;
        MOVFF   PRODL, RES0     ;
;
        MOVF    ARG1H, W
        MULWF   ARG2H           ; ARG1H * ARG2H → PRODH:PRODL
        MOVFF   PRODH, RES3     ;
        MOVFF   PRODL, RES2     ;
;
        MOVF    ARG1L, W
        MULWF   ARG2H           ; ARG1L * ARG2H → PRODH:PRODL
        MOVF    PRODL, W        ;
        ADDWF   RES1, F         ; Add cross products
        MOVF    PRODH, W        ;
        ADDWFC  RES2, F         ;
        CLRF    WREG            ;
        ADDWFC  RES3, F         ;
;
        MOVF    ARG1H, W        ;
        MULWF   ARG2L           ; ARG1H * ARG2L → PRODH:PRODL
        MOVF    PRODL, W        ;
        ADDWF   RES1, F         ; Add cross products
        MOVF    PRODH, W        ;
        ADDWFC  RES2, F         ;
        CLRF    WREG            ;
        ADDWFC  RES3, F         ;
;
        BTFSS   ARG2H, 7        ; ARG2H:ARG2L neg?
        BRA     SIGN_ARG1       ; no, check ARG1
        MOVF    ARG1L, W        ;
        SUBWF   RES2            ;
        MOVF    ARG1H, W        ;
        SUBWFB  RES3 
; 

SIGN_ARG1:
        BTFSS   ARG1H, 7        ; ARG1H:ARG1L neg?
        BRA     CONT_CODE       ; no, done
        MOVF    ARG2L, W        ;
        SUBWF   RES2            ;
        MOVF    ARG2H, W        ;
        SUBWFB  RES3
;
CONT_CODE:
          :