#// An example of a C program with a function with #// one input and a return value ##include PRINT_INT=1 PRINT_STR=4 .data str1: .asciiz "Sum 1.. " str2: .asciiz " = " str3: .asciiz "\n" .text main: #int main(void) { la $sp, stack_top main__prologue: push $ra push $s0 main__body: li $s0, 10 #int max = 10; move $a0, $s0 jal sum_to #int result = sum_to(max); move $t0, $v0 li $v0, PRINT_STR la $a0, str1 syscall # printf("Sum 1.. "); li $v0, PRINT_INT move $a0, $s0 syscall # printf("%d", max); li $v0, PRINT_STR la $a0, str2 syscall li $v0, PRINT_INT move $a0, $t0 syscall li $v0, PRINT_STR la $a0, str3 syscall li $v0, 0 main__epilogue: pop $s0 pop $ra jr $ra sum_to: #int sum_to(int n) { # ret in sum_to__prologue: push $ra push $s0 sum_to__body: # ret in $t0, result in $v0, n in $s0 move $s0, $a0 bnez $s0, sum_to__else # if(n != 0) goto sum_to__else; li $t0, 0 # ret = 0; b sum_to__end # goto sum_to__end; sum_to__else: sub $a0, $s0, 1 jal sum_to # result = sum_to(n-1); add $t0, $s0, $v0 # ret = n + result; sum_to__end: move $v0, $t0 sum_to__epilogue: pop $s0 pop $ra # return ret; jr $ra .data .align 2 stack_bottom: .space 0x1000 stack_top: