Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need an explanation of how I\'m getting this answer. please be detailed in exp

ID: 3712156 • Letter: I

Question


I need an explanation of how I'm getting this answer. please be detailed in explanation.
thanks

1. (10 points) MIPS Function and Stack Usage On the next sheet, using the atached starter code, write a complete MIPS function based on the function header sbown bere: int sum4(int a, int b, int c, int d) Your function must include the code required to protect $ra and the 4 registers used to pass the arguments. Sum addi sp sp - 20 5 1-0 reg vy So Q0 2- add 20, ao ai Ato a +b a3 a3?0(-7) ?pop stack value1 tw Page 1 of 2 1

Explanation / Answer

Given function calculates sum of the four numbers provided as argument. Whenever there is a function call, all the arguments to be passed as arguments to the function plus return address of the function must be pushed onto the stack. For this you have to reserve required space on the space which is accomplished using:

addi sp,sp,-20

Which means that reserver 20 bytes on the stack by decreasing stack pointer address by 20 bytes(4 integer arguments 4*4 bytes + return address 4 bytes). Caling function will push each elemetn one by one from left to right as:

push a

push b

push c

push d

So elements in the stack will be in reverse order, return address will be one top followed by d...c...b...a and can be retrieved using stack pointer(sp) as 0(sp), 4(sp), 8(sp), 12(sp).

Now use add instruction add two operands at a time as below:

add t0, a0, a1

add t1, a2, a3

add v0, t0, t1

Now final result is in v0 which is used in the main and stored at location pointed by "answer".

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote