.ORIG x3000 JSR MULTIPLY HALT ; MULTIPLY SUBROUTINE ; Takes the input at X and Y and stores the product X*Y ; at RESULT. ; INPUTS: X, Y ; OUTPUTS: RESULT X .FILL 0 Y .FILL 0 RESULT .FILL 0 MULTIPLY ; Insert code here. ST R0, TMP_R0 ST R1, TMP_R1 ST R2, TMP_R2 ST R3, TMP_R3 ST R7, TMP_R7 AND R2, R2, 0 ; R2 = result LD R0, X ; R0 = a LD R1, Y ; R1 = b MUL_LP BRz MUL_SKIP ; while (b!= 0) AND R3, R1, x01 ; if (b AND 1) != 0 BRz MUL_SH ADD R2, R2, R0 ; res = res + a MUL_SH ADD R0, R0, R0 ; a <<= 1 ST R1, RSH_IN JSR RSH LD R1, RSH_RES ; b >>= 1 BRnp MUL_LP MUL_SKIP ST R2, RESULT LD R7, TMP_R7 LD R3, TMP_R3 LD R2, TMP_R2 LD R1, TMP_R1 LD R0, TMP_R0 MUL_RET RET ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; DO NOT MODIFY ; ; RSH SUBROUTINE ; Effectively performs a "right shift" by ; left-shifting the input at RSH_IN by 15 bits ; and storing the result at RSH_RES ; ; INPUT: RSH_IN ; OUTPUT: RSH_RES RSH_IN .FILL 0 RSH_RES .FILL 0 RSH ST R0, TMPTMP_R0 ; Save registers we intend to use ST R1, TMPTMP_R1 ST R3, TMPTMP_R3 AND R3, R3, 0 ADD R3, R3, 15 LD R0, RSH_IN AND R1, R1, 0 RLOOP ADD R1, R1, R1 ; Left shift R1 ADD R0, R0, 0 BRzp RSKIP ADD R1, R1, 1 RSKIP ADD R0, R0, R0 ; Left shift R0 ADD R3, R3, -1 BRp RLOOP ST R1, RSH_RES LD R3, TMPTMP_R3 ; Restore registers LD R1, TMPTMP_R1 LD R0, TMPTMP_R0 RSH_RET RET ; First level tmp registers TMP_R0: .FILL 0 TMP_R1: .FILL 0 TMP_R2: .FILL 0 TMP_R3: .FILL 0 TMP_R4: .FILL 0 TMP_R5: .FILL 0 TMP_R6: .FILL 0 TMP_R7: .FILL 0 ; Second level tmp registers TMPTMP_R0: .FILL 0 TMPTMP_R1: .FILL 0 TMPTMP_R2: .FILL 0 TMPTMP_R3: .FILL 0 TMPTMP_R4: .FILL 0 TMPTMP_R5: .FILL 0 TMPTMP_R6: .FILL 0 TMPTMP_R7: .FILL 0 .END