# A live coding example done as a recap on functions. # This has a simple function with 1 parameter and a return value .text main: #Local max $s0 #Local result $t0 main__prologue: begin push $ra push $s0 main__body: li $s0, 10 #int max = 10; move $a0, $s0 jal sum_to move $t0, $v0 #int result = sum_to(max); li $v0, 4 la $a0, msg_sum1 #printf("Sum 1.. "); syscall li $v0, 1 #printf("%d", max); move $a0, $s0 syscall li $v0, 4 # printf(" = "); la $a0, msg_sum2 syscall li $v0, 1 #printf("%d", result); move $a0, $t0 syscall li $v0, 11 #putchar('\n'); li $a0, '\n' syscall li $v0, 0 main__epilog: pop $s0 pop $ra end jr $ra ################################################ sum_to: # Parameter $a0 n # Locals # $t0 sum # $t1 i sum_to__body: li $t0, 0 #int sum = 0; li $t1, 1 #int i = 1; sum_to_loop: bgt $t1, $a0, sum_to_loop_end #if (i > n) goto sum_to_loop_end; add $t0, $t0, $t1 #sum = sum + i; addi $t1, $t1, 1 #i = i + 1; b sum_to_loop sum_to_loop_end: move $v0, $t0 # return sum jr $ra .data msg_sum1: .asciiz "Sum 1.. " msg_sum2: .asciiz " = "