; ; main routine: establish the stack, then read ; and write. ; ; recursive multiplication. assumes ; b is positive. ; ; int mult(int a, int b){ ; if( b <= 0 ){ ; return 0 ; else ; return a + mult(a, b-1) ; } ; lcw r1 stack ; set up stack loa r3 r0 ; get first parameter loa r2 r0 ; get second parameter psh r2 ; put second parameter on the stack lcw r2 mult ; call mult cal r2 r2 ; pop r0 ; just for consistency, clean up the stack sto r3 r0 ; write result, hlt ; and halt mult psh r2 ; save the return address loa r2 r1 4 ; get at the 2nd argument, b ; a = r2, b = r3 bgt r2 r0 else ; 0 < r2, i.e. recursive case add r3 r0 0 ; return 0 brs endif else sub r2 r2 1 ; r2 = a-1 psh r3 ; save first argument, a, on stack (it's going to get overwritten by the return!) psh r2 ; add r2 as 2nd argument, r3 shouldn't have changed lcw r2 mult ; call mult recursively cal r2 r2 pop r0 ; remove 2nd argument (b-1) off stack pop r2 ; load 'a' back into r2 off of the stack add r3 r3 r2 ; r3 = a + mult(a, b-1) endif pop r2 ; get the return address jmp r2 ; return ; ; stack area: 50 words ; dat 100 stack