    lcw r1 stack                 ; set up stack
    loa r3 r0                    ; get n

    lcw r2 rec_sum               ; call rec_sum
    cal r2 r2            

    sto r3 r0                    ; print result,
    hlt                          ; and halt


rec_sum
    psh r2                       ; save the return address on the stack
    bne r3 r0 recurse            ; if n!=0 recurse
    add r3 r0 0                  ; if n == 0, result is 0
    brs done

recurse
    psh r3                       ; save n on the stack
    sub r3 r3 1                  ; n = n-1

    lcw r2 rec_sum               ; make recursive call
    cal r2 r2                    ; rec_sum (n-1), answer should be in r3

    pop r2                       ; get n into r2
    add r3 r3 r2                 ; r3 = n + rec_sum (n-1)
done
    pop r2                       ; get the return address
    jmp r2                       ; go back to caller


    dat 100
stack
