tags:

views:

73

answers:

1

I was reading some code and was not sure what this line does

movq (%rsp), %rsp

Thanks

+4  A: 

movq (assuming you're talking about x86) is a move of a quadword (64-bit value). This particular instruction:

movq (%rsp), %rsp

looks very much like code that will walk up through stack frames. This particular instruction grabs the quadword pointed to by the current stack pointer, and loads it into the stack pointer, overwriting it.

By way of example, this code sequence (based on real code, and in Intel rather that AT&T format) will continuously load the stack pointer from its contents until the value 16 bytes beyond it is 0.

576  cmpq    [rsp+0x10],0x0
582  jz      594
588  movq    rsp,[rsp]
592  jmp     576
594  ...

It's possible it may not be stack-frame walking code but it's be unusual since it would be suborning the stack pointer for something it's not usually used for.

It is unusual in that moving up stack frames usually involves stack pointer and base pointer but that's usually for just going up one level (i.e., a return from a function).

For the sort of code shown above where you want to move up multiple levels, it's probably faster to just use the stack pointer until you get where you need to be, then pop the base pointer off then (calling conventions will often push the current base pointer before changing it, so that a simple pop will recover the old value).

paxdiablo
This is certainly 64-bit, since the RSP register only exists in 64-bit (on IA32 architectures, and I don't recall another architecture that uses RSP). The analysis of it's purpose sounds reasonable as well.
Zooba