In: Computer Science
3. Translate the following C code to MIPS assembly code (in two separate files).
int main()
{
printf(“before subroutine!\n”);
Subfunc();
printf(“after subroutine!\n!”);
}
void Subfunc()
{printf(“I am subroutine!\n”);}
Submission file: Lab4_3a.asm for the main routine
and Lab4_3b.asm for the sub-routine.
Greetings!!
Code:
.data
before: .asciiz "Before subroutine!\n"
after: .asciiz "After subroutine!\n"
within: .asciiz "I am subroutine!\n"
.text
#MAIN STARTS HERE
main:
#DISPLAY THE BEFORE SUBROUTINE MESSAGE
la $a0,before #load the address of message
before subroutine
li $v0,4 #parameter for display
string
syscall #display
#SUBROUTINE CALL
jal Subfunc
#RETURN FROM SUROUTINE AND DISPLAY THE MESSAGE AFTER
SUBROUTINE
la $a0,after #load the address of message
after subroutine
li $v0,4 #parameter for display
syscall #display
#TERMINATE THE EXECUTION
li $v0,10 #parameter for termination
syscall
#MAIN ENDS HERE
#SUBROUTINE DEFINITION
Subfunc:
#DISPLAY THE MESSAGE I AM SUBROUTINE
la $a0,within #load the address of message
i am subroutine
li $v0,4 #load parameter for display
string
syscall #display
#RETURN TO MAIN
jr $ra
#END OF SUBROUTINE
Output screenshot:
Hope this helps