int factorial(int x) { if(x==0) return(1); return(factorial(x-1) * x); } -------------- .file 1 "fact.c" # Cc1 defaults: # -mgas -mgpOPT # Cc1 arguments (-G value = 8, Cpu = default, ISA = 1): # -quiet -dumpbase -o gcc2_compiled.: __gnu_compiled_c: .text .align 2 .globl factorial .text .loc 1 3 .ent factorial factorial: .frame $fp,24,$31 # vars= 0, regs= 2/0, args= 16, extra= 0 .mask 0xc0000000,-4 # Function prolog .fmask 0x00000000,0 subu $sp,$sp,24 # allocate stack frame sw $31,20($sp) # save the return address on stack sw $fp,16($sp) # save old procedure activation frame move $fp,$sp # setup new procedure activation frame sw $4,24($fp) # store x into stack lw $2,24($fp) # load x into $2 bne $2,$0,$L2 # if x!=0 goto L2 li $2,0x00000001 # load's 1 into $2 j $L1 # jump to function epilogue $L2: lw $3,24($fp) # load x into $3 subu $2,$3,1 # $2 = x -1 move $4,$2 # arg0 = x - 1 jal factorial # call factorial lw $4,24($fp) # $4 = x mult $2,$4 # HI/LO = x -1 * x mflo $3 # $3 = (x-1)*x move $2,$3 # return value = $3 j $L1 # jump down 1 $L1: move $sp,$fp # sp not trusted here lw $31,20($sp) lw $fp,16($sp) addu $sp,$sp,24 j $31 .end factorial ---------------------------- factorial: subu $sp, $sp, 8 # allocate stack space sw $ra, 4($sp) # store return address sw $a0, 0($sp) # store arg0 addi $a0, $a0, -1 #x - 1 beqz $a0, atzero # if x-1 == 0 jump to atzero jal factorial # call factorial j done # jump to epilogue atzero: li $v0, 1 # set return value to 1 done: lw $a0, 0($sp) # load x from stack mult $v0, $a0 # Hi/LO = v0*a0 mflo $v0 # return value = LO lw $ra, 4($sp) # restore return address addu $sp, $sp, 8 # de-allocate stack space jr $ra # exit