/* hand written assembly for n! function */ .global main .align 4 .str0: .asciz "The factorial of %d is %d\n" .align 4 ! align on 4 byte boundary fact: save %sp,-104,%sp ! register window shift st %i0,[%fp-20] mov 1,%o0 ! use %o0 as accumulator ld [%fp-20],%i0 lp0: cmp %i0,2 ! if n<2 bl base ! to to base case nop ! pipeline delay slot mov %i0,%o1 ! prepare for multiplication (move to out register) call .mul ! multiplication, result in %o0 nop ! pipeline delay slot ba lp0 ! unconditional branch to lp0 dec %i0 ! n -= 1 ; conveniently fills pipeline delay slot base: mov %o0,%i0 ! move return value back to i0 ret restore ! register window shift in delay slot main: save %sp,-120,%sp mov 6,%o0 ! compute 6! mov 6,%o1 ! save original value in %o1 call fact nop mov %o0,%o2 ! prepare to call printf sethi %hi(.str0),%o0 ! o0 points to printf format string or %o0,%lo(.str0),%o0 call printf nop restore mov 1,%g1 ! trap to OS to terminate program ta 0