#int main(void) { # printf("Welcome"); # int x = 99; # f1(); # printf("Goodbye"); # printf("%d",x); # return 0; #} #void f1(void) { # printf("Hi there\n"); # f2(); # printf("It is me again\n"); #} #void f2(void){ # printf("I love chips\n"); #} #This example uses the push and pop pseudo instructions that only #work in mipsy .data main_str1: .asciiz "Welcome\n" main_str2: .asciiz "Goodbye\n" f1_str1: .asciiz "Hi there\n" f1_str2: .asciiz "It is me again\n" f2_str1: .asciiz "I love chips\n" .text main: begin # sets the frame pointer push $ra # store $ra on stack push $s0 # store $s0 on stack li $v0, 4 # printf("Welcome"); la $a0, main_str1 syscall li $s0, 99 # we use an s (safe) register to make sure # our value 99 still exists after our function call jal f1 # f1() li $v0, 4 # printf("Goodbye"); la $a0, main_str2 syscall li $v0, 1 # printf("%d",x) move $a0, $s0 syscall li $v0, 0 # set return value to 0 pop $s0 # store $s0 on stack pop $ra # restore $ra from stack end # restores the frame pointer jr $ra # return f1: begin push $ra # store $ra on stack push $s0 li $s0, 42 # If we had not pushed $s0 onto the stack #etc # This would overwrite the value of $s0 for # for the main function li $v0, 4 # printf("Hi there\n"); la $a0, f1_str1 syscall jal f2 li $v0, 4 # printf("It is me again\n"); la $a0, f1_str2 syscall pop $s0 pop $ra end jr $ra f2: begin push $ra # store $ra on stack push $s0 li $v0, 4 # printf("I love chips\n"); la $a0, f2_str1 syscall li $s0, 10 # If we had not pushed $s0 onto the stack # This would overwrite the value of $s0 for # for the main function #etc pop $s0 pop $ra end jr $ra