mov# -12, r0
mov r0, -(r6)
mov# 17, r0
mov r0, -(r6)
jsr mult2
halt


; ----------------------------------

MULT2: MOV R6, R0
       MOV R1, -(R6)
       MOV R2, -(R6)
       MOV R3, -(R6)
       INC R0
       MOV (R0)+, R1
       MOV (R0)+, R2

       CLR R3           ; stores how many operands were negative

       ; arithmetically-negate R1 if necessary, and store count in R3
       TST R1
       BGE CONT1
       CLR R0
       SUB R1, R0
       MOV R0, R1
       INC R3

       ; arithmetically-negate R2 if necessary, and adjust count in R3
CONT1: TST R2
       BGE CONT2
       CLR R0
       SUB R2, R0
       MOV R0, R2
       INC R3

CONT2: ; call other MULT subroutine
       MOV R1, -(R6)
       MOV R2, -(R6)
       JSR MULT
       INC R6
       INC R6

       ; arithmetically-negate result if appropriate
       MOV# 1, R1
       BIT R1, R3
       BEQ RET2
       CLR R2
       SUB R0, R2
       MOV R2, R0

 RET2: MOV (R6)+, R3
       MOV (R6)+, R2
       MOV (R6)+, R1
       RTS



; ----------------------------------


MULT:  MOV R6, R0
       MOV R1, -(R6)
       MOV R2, -(R6)
       INC R0
       MOV (R0)+, R1
       MOV (R0)+, R2

       CLR R0
       TST R1
       BEQ RET
LOOP:  ADD R2, R0
       DEC R1
       BGT LOOP

 RET:  MOV (R6)+, R2
       MOV (R6)+, R1
       RTS
