TITLE "MP2 by Douglas Jones" ; a program to output the multiplication table using a ; rather stupid recursive multiply algorithm USE "hawk.h" USE "monitor.h" SUBTITLE "MULT, the recursive multiply algorithm" ; the algorithm used here can be briefly described as ; mult( i, j ) { ; if (i == 0) return 0 ; if (i == 1) return j ; return mult( j/2, i ) + mult( j - j/2, i ) ; } ; the code below is based on the following variation of the above, rewritten ; so there is just one return at the end instead of multiple returns scattered ; through the code ; mult( i, j ) { ; prod = i ; if (i != 0) { ; if (i == 1) { ; prod = j; ; } else { ; prod = mult( j/2, i ) + mult( j - j/2, i ) ; } ; } ; return prod ; } ; activation record ;RETAD = 0 I = 4 JREM = 8 PROD = 12 ARSIZE = 16 ; code MULT: ; expects R3 = i, one multiplicand ; R4 = j, another multiplicand ; uses R5 = jdiv2 = j/2 ; returns R3 = prod = i * j ; ; prod = i -- basis for i == 0 TESTR R3 BZS MULTQT ; if (i != 0) { CMPI R3,1 BNE MULTEL ; if (i == 1) { MOVE R3,R4 ; prod = j -- basis for i == 1 BR MULTQT MULTEL: ; } else { -- recusive general case STORES R1,R2 ; -- save retad (slight optimization) STORE R3,R2,I ; -- save i MOVE R5,R4 ; SRU R5,1 ; jdiv2 = j/2 SUB R4,R4,R5 STORE R4,R2,JREM ; jrem = j - j/2 MOVE R4,R3 ; -- param 2 = i MOVE R3,R5 ; -- param 3 = jdiv2 ADDI R2,R2,ARSIZE JSR R1,MULT ADDI R2,R2,-ARSIZE STORE R3,R2,PROD ; prod = mult( jdiv2, i ) LOAD R3,R2,JREM ; -- param 1 = jrem LOAD R4,R2,I ; -- param 2 = i ADDI R2,R2,ARSIZE JSR R1,MULT ADDI R2,R2,-ARSIZE ; prod2 = mult( jrem, i ) LOAD R4,R2,PROD ADD R3,R3,R4 ; prod = prod + prod2 LOADS R1,R2 ; -- restore retad (slight optimization) ; } MULTQT: ; } -- prod is in R3 at exit JUMPS R1 ; return prod SUBTITLE "main() a main program to print the multiplication table" ; activation record structure ;RETAD = 0 ARSIZE = 4 ; code S MAIN INT MAIN MAIN: ; uses R8 = x ; R9 = y STORES R1,R2 ; -- save RA LIS R9,1 ; y = 1 MAINYLP: ; loop { -- one iteration per row LIS R3,2 ; -- parameter x coord ADDI R4,R9,-1 ; -- parameter y coord ADDI R2,R2,ARSIZE LIL R1,PUTAT JSRS R1,R1 ; putat( 2, y - 1 ) -- start a row ADDI R2,R2,-ARSIZE LIS R8,1 ; x = 1 MAINXLP: ; loop { -- per item in the row MOVE R3,R8 ; -- parameter x MOVE R4,R9 ; -- parameter y ADDI R2,R2,ARSIZE JSR R1,MULT ; prod = mult( x, y ) ADDI R2,R2,-ARSIZE ; -- parameter 1 already in R3 LIS R4,4 ; -- parameter 2 field width ADDI R2,R2,ARSIZE LIL R1,PUTDEC JSRS R1,R1 ; putdec( prod, 4 ) ADDI R2,R2,-ARSIZE ADDSI R8,1 ; x = x + 1 CMPI R8,10 BLE MAINXLP ; } while (x <= 10) ADDSI R9,1 ; y = y + 1 CMPI R9,10 BLE MAINYLP ; } while (y <= 10) LOADS R1,R2 ; -- recover RA JUMPS R1 ; return END