; ; main routine: establish the stack, then read ; and write. ; lcw r1 stack ; set up stack loa r3 r0 ; get variable lcw r2 sum ; call sum cal r2 r2 ; sto r3 r0 ; write result, hlt ; and halt sum psh r2 ; save the return address on the stack bgt r3 r0 recurse ; check base case add r3 r0 0 ; if n <= 0, result is 0 brs done recurse psh r3 ; save n on the stack sub r3 r3 1 ; r3 = n-1 lcw r2 sum ; make recursive call cal r2 r2 ; sum(n-1), answer should be in r3 pop r2 ; get n into r2 add r3 r3 r2 ; r3 = n + sum(n-1) done pop r2 ; get the return address jmp r2 ; go back to where we were called from ; ; stack area: 50 words ; dat 100 stack