org 100h jmp demo ;; Sudan function. BC=N, DE=X, HL=Y; output in HL. sudan: mov a,b ; N=0? ora c jz sdnbse mov a,h ; Y=0? ora l jz sdnbse push b ; Save N and Y (we don't need X) push h dcx h call sudan ; Calculate result of inner call xchg ; Set X = result of inner call pop h ; Get Y dad d ; Set Y = Y + result of inner call pop b ; Get N dcx b ; N = N-1 jmp sudan ; Calculate result of outer call sdnbse: dad d ; Return X+Y (base case) ret ;; Output routine (show 'sudan(N,X,Y) = value'), using CP/M call show: push h! push d! push b! ; Copies of args push h! push d! push b! ; For output lxi d,sdt! call pstr ; Print call pop h! call prhl lxi d,sdc! call pstr pop h! call prhl lxi d,sdc! call pstr pop h! call prhl lxi d,sdi! call pstr pop b! pop d! pop h! ; Restore args call sudan! call prhl ; Find and print result lxi d,sdnl! jmp pstr prhl: lxi d,numbuf! push d! lxi b,-10 prdgt: lxi d,-1 pdiv: inx d! dad b! jc pdiv mvi a,'0'+10! add l! pop h dcx h! mov m,a! push h xchg! mov a,h! ora l! jnz prdgt pop d pstr: mvi c,9! jmp 5 ;; Set up big system stack (using CP/M) demo: lhld 6 sphl ;; Show a couple of values lxi b,0! lxi d,0! lxi h,0! call show lxi b,1! lxi d,1! lxi h,1! call show lxi b,2! lxi d,1! lxi h,1! call show lxi b,3! lxi d,1! lxi h,1! call show lxi b,2! lxi d,2! lxi h,1! call show rst 0 sdt: db 'sudan($' sdc: db ', $' sdi: db ') = $' db '.....' numbuf: db '$' sdnl: db 13,10,'$'