Consider the following MIPS assembly language code
Problem: Consider the following MIPS assembly language code: 1000 lw $5, 0($3) # does some work 1004 beq $5, $0, done # does some work 1008 jal foo1 1012 ... foo1: 1100 addi $29, $29, -4 # $29 is the stack pointer 1104 sw $31, 0($29) # $31 is the return address 1108 jal foo2 1112 ... After executing the instruction on line 1104, the contents of register $31 is: (a) 1008 (b) 1112 (c) The content of $29 (d) 1108 (e) 1012 Answer: The answer is (e). I can see that (c) is wrong but, could someone please explain to me why (e) is the correct answer? If more information is needed, just ask. Any input would be greatly appreciated!
jal instruction at line 1008 sets the register $31 to return address of 1012
But, on line 1100, the stack pointer is decremented by 4 and then that updated stack pointer is stored into the return register which seems to clash with what you said ... could you elaborate, please?
stack pointer reg is $29 right, wat it got to do with reg $31 ?
the value in $29 reg is decremented. how it wil change the value in $31 ?
What exactly does line 1104 do?
Jumps to the calculated address and stores the return address in $31
return adress = immediate adress after jal instruction
So, does $29 get incremented by 4 and then decremented by 4 on line 1100?
we adding -4 so it gets decremented by 4. thats all to it
Is the program counter the same thing as $29? I ask because http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html says $t = $s + imm; advance_pc (4);
PC counter gets incremented every instruction right
But, is the stack pointer the same thing as the program counter? If so, that would explain the +4-4 to me.
no ways. stackpointer and pc are two different registers
i dint get the +4-4 thingy.. can u explain pls
If they were the same thing (which you said, they are not), I was thinking that adding 4 to the program counter and subtracting 4 from the stack pointer would "neutralize" each other.
OK. as i said, PC gets incremented automatically after completing each instruction. we dont have to do anything explicit. where as stackpointer we need to program
Okay, I think I got it. Tell me if the following is correct.: jal foo1 stores the address of line 1012 in $31 and then runs the code in foo1 but, foo1 does not change the contents of $31 so the final answer is just 1012. Also, line 1104 goes to the address of $29 and stores the contents of $31 in $29 and not the contents of $29 into $31, right?
thats perfect ! both statements are correct.
Thanks a lot, again! :)
yw :)
Join our real-time social learning platform and learn together with your friends!