main:
# Args: void
# Returns:
# - $v0: int
#
# Locals:
# - $s0: int input
# - $t0: int f
#
# Stack: [$ra, $s0]
# Uses: [$ra, $s0, $v0, $t0, $a0]
# Clobbers: [$v0, $t0, $a0]
#
# Structure:
# -> main
# -> [prologue]
# -> [body]
# -> [epilogue]
main__prologue:
begin
push $ra
push $s0
# The following two instructions are equivalent to `push $s0`
# addi $sp, $sp, -4
# sw $s0, ($sp)
main__body:
li $v0, 5 # syscall 5: read_int
syscall #
move $s0, $v0 # scanf("%d", &input);
move $a0, $v0
jal factorial #
move $t0, $v0 # int f = factorial(input);
li $v0, 1 # syscall 1: print_int
move $a0, $s0 #
syscall # printf("%d", input);
li $v0, 4 # syscall 4: print_string
la $a0, main__result_msg #
syscall # printf("!= ");
li $v0, 1 # syscall 1: print_int
move $a0, $t0 #
syscall # printf("%d", f);
li $v0, 11 # syscall 11: print_char
li $a0, '\n' #
syscall # putchar('\n');
# Restore the original value of $s0 from memory
# lw $s0, ($sp)
# addi $sp, $sp, 4
main__epilogue:
pop $s0
pop $ra
end
li $v0, 0 #
jr $ra # return 0;
factorial:
# Args:
# - $a0: int n
# Returns:
# - $v0: int
# Locals:
# - $s0: int n
#
# Stack: [$ra, $s0]
# Uses: [$ra, $s0, $v0, $a0]
# Clobbers: [$v0, $t0, $a0]
#
# Structure:
# -> main
# -> [prologue]
# -> [body]
# -> n_eq_0
# -> [epilogue]
factorial__prologue:
begin
push $ra
push $s0
factorial__body:
move $s0, $a0
beqz $a0, factorial__n_eq_0 # if (n != 0) {
addi $a0, $a0, -1
jal factorial # factorial(n - 1)
mul $v0, $v0, $s0 # return n * factorial(n - 1);
j factorial__epilogue # }
factorial__n_eq_0:
li $v0, 1 # return 1;
factorial__epilogue:
pop $s0
pop $ra
end
jr $ra
.data
main__result_msg:
.asciiz "! = "
C Function with No Parameters or Return Value