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