In: Computer Science
4. Translate the following C code to MIPS assembly (in two separate files).
Run the program step by step and observe the order of instructions being executed and the value of $sp.
int main()
{
int x=2;
z=Subfunc(x);
printf(“Value of z is: %d”, z);
}
int Subfunc(int x)
{
return x+1;}
Submission file: Lab4_4a.asm
and Lab4_4b.asm
Value of $sp before and after the program execution remains the same. Only during function call it will decrease that to store the saved registers like return value , stack pointer or if some values are changed. But, here in this program it won't matter much but mainly in recursive kind of functions stack usage is very much crucial
MIPS PROGRAM :-
.data
print_prompt: .asciiz "Value of z is: "
.text
.globl main
main:
#int x = 2
addi $a0, $zero, 2
#z = Subfunc(x)
jal Subfunc
move $t0, $v0
#printf("Value of
z is: %d ")
addi $v0, $zero, 4
la $a0, print_prompt
syscall
addi $v0, $zero, 1
move $a0, $t0
syscall
#system exit
addi $v0, $zero , 10
syscall
Subfunc:
#creating stack
addiu $sp, $sp, -4
sw $fp, 4($sp)
#return x+1
addiu $v0, $a0, 1
#releasing stack
addiu $sp, $sp, 4
jr $ra