org $4000
; calculate sum of numbers
; Input: RegD num
; Output: RegD Sum of 1,2,3,...,num
; Errors: may overflow
; 1) binding
num  set  2   ;loop counter 1,2,3
sum  set  0   ;running
calc
; 2) allocation
     pshd           ;allocate num
     movw #0,2,-sp  ;sum=0

; 3) access
loop ldd  sum,sp
     addd num,sp
     std  sum,sp  ;sum = sum+num
     ldd  num,sp
     subd #1
     std  num,sp  ;num = num-1
     bne  loop
     ldd  sum,sp  ;result

; 4) deallocate
     leas 4,sp
     rts

main lds  #$4000
     ldd  #100
     jsr  calc
     bra  *
     org  $FFFE
     fdb  main