I reference This answer the "pushq" can be replaced by "subq and movq", "popq" can be replace d by "movq and addq".
But "PUSH ESP" and "POP ESP" are the special case. Reference This answer.
But result not "0"
pushq %rsp ;pushes the value of the ESP register as it existed before the instruction was executed.
popq %rsp ;increments the stack pointer (ESP) before data at the old top of stack is written into the destination.
pushq %rdx ;Decrements the stack pointer and then stores the source operand on the top of the stack.
popq %rdx ;Loads the value from the top of the stack to the location specified with the destination operand (or explicit opcode) and then increments the stack pointer.
rax =
rdx =
0x28 | 0x12 |
0x30 | 0x34 | <--- rsp
0x38 | 0x56 |
0x40 | 0x78 |
0x48 | 0x9A |
movq %rsp, %rax
rax = 0x30
rdx =
0x28 | 0x12 |
0x30 | 0x34 | <--- rsp
0x38 | 0x56 |
0x40 | 0x78 |
0x48 | 0x9A |
pushq %rsp ;store, using rsp as the address, then subtract 8 from rsp
rax = 0x30
rdx =
0x28 | 0x12 | <--- rsp
0x30 | 0x30 |
0x38 | 0x56 |
0x40 | 0x78 |
0x48 | 0x9A |
popq %rdx ;load, using rsp as the address, then add 8 to the rsp
rax = 0x30
rdx = 0x30
0x28 | 0x12 | <--- rsp
0x30 | 0x30 |
0x38 | 0x56 |
0x40 | 0x78 |
0x48 | 0x9A |
subq %rdx, %rax ;Return 0
rax = 0x00
rdx = 0x30
0x28 | 0x12 |
0x30 | 0x30 | <--- rsp
0x38 | 0x56 |
0x40 | 0x78 |
0x48 | 0x9A |