# In this version we switch to using a standard prologue - a bit of # code that knows the system dependent way to start and stop a program. # The real entry point (__start) is in the prologue. # The place a user program logically starts executing is now "main". .data # begin data section N: .word 5 # reserve and initialize a word result: .space 4 # reserve but don't initalize a word msg: .asciiz "The factorial is: " # message string newline: .asciiz "\n" .text # begin program text .global main main: addi $sp, $sp, -8 # procedure entry (as main has no local variables) sw $ra, 0($sp) # we'll get to what this means... sw $fp, 4($sp) # "soon" addi $t0, $0, 1 # $t0 will hold result, initially 1 lw $t1, N($gp) # $t1 will hold N, initially 9 top: beq $t1, $0, bottom mult $t0, $t0, $t1 # result = result * N addi $t1, $t1, -1 # decrement N j top # goto top bottom: sw $t0, result($gp) # we'd better save result addi $v0, $0, 4 # finished w/ loop, now print out addi $a0, $gp, msg # message, by invoking syscall 4 syscall # (print_string) addi $v0, $0, 1 # now print out result, by lw $a0, result($gp) # invoking syscall 1 (print_int) syscall addi $v0, $0, 4 # print a newline (w/ syscall 4) addi $a0, $gp, newline syscall # go back to the prologue (which will terminate us) lw $ra, 0($sp) # fetch caller's return address lw $fp, 4($sp) # restore $fp to what it was on entry addi $sp, $sp, 8 # restore $sp to what it was on entry jr $ra