Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (s3a):

Consider the following MIPS assembly language code

OpenStudy (s3a):

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!

ganeshie8 (ganeshie8):

jal instruction at line 1008 sets the register $31 to return address of 1012

OpenStudy (s3a):

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?

ganeshie8 (ganeshie8):

stack pointer reg is $29 right, wat it got to do with reg $31 ?

ganeshie8 (ganeshie8):

the value in $29 reg is decremented. how it wil change the value in $31 ?

OpenStudy (s3a):

What exactly does line 1104 do?

ganeshie8 (ganeshie8):

Jumps to the calculated address and stores the return address in $31

ganeshie8 (ganeshie8):

return adress = immediate adress after jal instruction

OpenStudy (s3a):

So, does $29 get incremented by 4 and then decremented by 4 on line 1100?

ganeshie8 (ganeshie8):

we adding -4 so it gets decremented by 4. thats all to it

OpenStudy (s3a):

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);

ganeshie8 (ganeshie8):

PC counter gets incremented every instruction right

OpenStudy (s3a):

But, is the stack pointer the same thing as the program counter? If so, that would explain the +4-4 to me.

ganeshie8 (ganeshie8):

no ways. stackpointer and pc are two different registers

ganeshie8 (ganeshie8):

i dint get the +4-4 thingy.. can u explain pls

OpenStudy (s3a):

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.

ganeshie8 (ganeshie8):

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

OpenStudy (s3a):

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?

ganeshie8 (ganeshie8):

thats perfect ! both statements are correct.

OpenStudy (s3a):

Thanks a lot, again! :)

ganeshie8 (ganeshie8):

yw :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!