Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

x86 64 - movq assembly function

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

movq (%rsp), %rsp
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...