mov# 12, r2
mov# 4, r3
mov# 7, r4
mov# 2, r5


   MOV X, R1

   MOV R2, -(R6)  ; y
   MOV R3, -(R6)  ; z
   JSR MULT2
   INC R6
   INC R6

   SUB R0, R1

   MOV R4, -(R6)  ; a
   MOV R5, -(R6)  ; b
   JSR MULT2
   INC R6
   INC R6

   ADD R1, R0

   HALT


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

X: .WORD 12


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
