4.45 In section 3.4.2, the x86-64 pushq instruction was described as decrementin
ID: 3574789 • Letter: 4
Question
4.45
In section 3.4.2, the x86-64 pushq instruction was described as decrementing the stack pointer and then storing the register at the the stack pointer location. So, if we had an instruction of the form pushq REG, for some register REG, it would be equivalent to the code sequence:
subq $8, %rsp //Decrement the stack pointer
movq REG, (%rsp) /// Store REG on stack
A) In light of analysis done in Practice Problem 4.7, does this code sequence correctly describe the behavior of the instruction pushq %rsp? Explain.
B) How would you rewrite the code sequence so that it correctly describes both the cases where REG is %rsp as well as any other register?
Explanation / Answer
Solution:
(a)
Consider the code sequence push REG instruction.
subq $8, %rsp //Decrement the stack pointer
movq REG, (%rsp) /// Store REG on stack
From the above code REG is any register and push REG will push the value to the stack at pointer %rsp that means which represents top of the stack and the value stores in the register REG.
The code sequence push %rsp in the line number 6 of the practice problem it is equivalent to the below code.
· movq %rsp , %rsp Read % rsp from the stack.
· subq $8 ,%rsp Decrement the stack pointer.
Then the execution of the instruction
movq %rsp %eax(it is taken from the line number 8 of the practice problem 4.7)
The above line will return 0xabcd as movq %rsp, %rsp instruction will store the value top of the stack pointer %rsp pushing %rsp and moving the value of %rsp in %eax that will give 0xabcd as result.
From the problem 4.7 the test function will return the value as 0xabcd, which describes the above code the sequence is correct.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.