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
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".
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.