;----------------------------------------------------------------- ; EX66.ASM UNSIGNED MULTIPLY - SHIFT & ADD C.P. Diduch 2001 ; ; Using the shift and add algorithm compute the product P= X * Y, ; where P = [P1, P0]. The 16-MSB's of the product are denoted, P1, ; and the 16-LSB's of the product are dentoed P0. Assume X and Y ; are read at input ports 0x0000 and 0x0001 and the 32-bit product ; is writtent to output ports 0x0003 and 0x0002. ; ; X Multiplier ; [Y1, Y] Multiplicand (shifted left through Y1) is stored in [R2, R1]. ; [P1, P0] Partial sums and final product stored in [R4, R3]. ; ; Author(s): ; Signature(s): Date: ;----------------------------------------------------------------- .ORG 0x8000 ; "assign the following to ROM" ; ; void main(void) { ; ; do { L0: LOADP R0, 0x0000 ; X = inport(0x0000) ; LOADP R1, 0x0001 ; Y = inport(0x0001) ; XOR R2, R2, R2 ; Y1 = 0; // Reg, var. in R2. XOR R5, R5, R5 ; i = 0; // Reg, var. in R5. XOR R4, R4, R4 ; [P1, P0] = 0; XOR R3, R3, R3 ; ADDL R6, NULL, 1 ; MASK = 0x0001; ; L1: SUBL NULL, R5, 16 ; while (i < 16) { JMPNC L4 ; AND NULL, R6, R0 ; if(Bit-i of X == 1) { JMPZ L2 ; ADD R4, R4, R2 ; P1 = P1 + Y1 ; ADD R3, R3, R1 ; P0 = P0 + Y ; JMPNC L2 ; if (CY) {P1 = P1 + 1} ADDL R4, R4, 1 ; } ; L2: SHIFTU R2, R2, 1(NULL) ; [Y1, Y] = [Y1, Y] << 1; SHIFTU R1, R1, 1(NULL) ; JMPNC L3 ; if("1" is shifted out of Y) { ADDL R2, R2, 1 ; Set LSB of Y1 ; } ; L3: ADDL R5, R5, 1 ; i = i + 1; SHIFTU R6, R6, 1(NULL) ; MASK = MASK << 1; JMP L1 ; } ; L4: STOREP 0x0002, R3 ; outport(0x0002, P0); STOREP 0x0003, R4 ; outport(0x0003, P1); JMP L0 ; } ; }