# This version replaces direct invocation of the OS to do output # with calls to the standard C-- runtime (which provides # methods printString(char* str) and printInt(int val), among others) .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) sw $fp, 4($sp) 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 $sp, $sp, -4 # make room for arg to printString addi $t0, $gp, msg # compute argument address sw $t0, 0($sp) # push argument jal printString lw $t0, result($gp) sw $t0, 0($sp) jal printInt addi $t0, $gp, newline sw $t0, 0($sp) jal printString addi $sp, $sp, 4 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