In: Computer Science
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box.
.text
main:
addi $s0,$zero,2
jal Disp
jal Fun1
jal Disp
j Exit
Fun1:
addi $sp,$sp,-4
sw $s0,0($sp)
addi $s0,$s0,15
jal Disp
lw $s0,0($sp)
addi $sp,$sp,4
jr $ra
Disp:
li $v0,1
move $a0,$s0
syscall
jr $ra
Exit:
li $v0,10
syscall
Hope this will help you. If you have any doubt please let me know.
Please go through all the notes.
Notes: 1) All most all coding part is given; I just stored the return address of Function1 into stack before calling a disp function, because when we are calling a function disp the value of $ra register is changed, and that address is remained in $ra register. To return from function 1 we need an original value that is stored in $ra register when function 1 is called from main to return to main. Hence before calling a function disp in function 1 we have to store the $ra register.
2) note that the value of $s0 is stored in stack pointer in function1 at the return time we are retrieving a $s0 register from stack pointer hence the value of $s0 register before, during and after function 1 is 2, 17 and 2 respectively( other-wise $s0’s value is preserved throughout program and it will return 2,17,17 respectively)
3) Please let me know if you find a difficulty in this
4) Screenshot of output is also attached.
-------------------Coding part---------------------
.text
main:
addi $s0,$zero,2 #setting $s0=2
jal Disp # calling a function Display
jal Fun1 #calling a function 1
jal Disp # calling a function disp again
j Exit # exit the program
Fun1: # function1 def begin
addi $sp,$sp,-4 # adjusting stack pointer
sw $s0,0($sp) # storing $s0's value in stack pointer
addi $s0,$s0,15 # adding 15 into $s0
addi $sp,$sp,-4 # adjusting stack pointer to store a return address
of function 1
sw $ra,0($sp) # storing reaturn address in stack pointer
jal Disp # calling a dispaly function
lw $ra,0($sp) # loading a return address in $ra
addi $sp,$sp,4 # adjusting a stack pointer
lw $s0,0($sp) #loading a value in $s0
addi $sp,$sp,4 # adjusting a stack pointer
jr $ra # return
Disp: #display
li $v0,1 #for printing integer
move $a0,$s0 #printing value of$s0
syscall
jr $ra #return address
Exit: # exit
li $v0,10
syscall
--------------------Screen shot------------------------------