[英]Assembly code for __rdtsc() in -O0 vs -O3
I have the following code:我有以下代码:
#include <x86intrin.h>
int main() {
return __rdtsc();
}
And I tried to compile on my machine (Intel i7-6700 CPU) and objdump
我试图在我的机器(Intel i7-6700 CPU)和
objdump
上编译
g++ -Wall test_tsc.cpp -o test_tsc -march=native -mtune=native -O0 -std=c++20
objdump -M intel -d test_tsc > test_tsc.O0
Then in test_tsc.O0
:然后在
test_tsc.O0
:
0000000000401122 <main>:
401122: 55 push rbp
401123: 48 89 e5 mov rbp,rsp
401126: 0f 31 rdtsc
401128: 48 c1 e2 20 shl rdx,0x20
40112c: 48 09 d0 or rax,rdx
40112f: 90 nop
401130: 5d pop rbp
401131: c3 ret
401132: 66 2e 0f 1f 84 00 00 nop WORD PTR cs:[rax+rax*1+0x0]
401139: 00 00 00
40113c: 0f 1f 40 00 nop DWORD PTR [rax+0x0]
What do push rbp
and mov rbp,rsp
do? push rbp
和mov rbp,rsp
有什么作用? It seems like they were for saving the stack pointer but then there isn't really a function call.看起来他们是为了保存堆栈指针,但实际上并没有 function 调用。 If g++ consider
__rdtsc()
a function call, then would there be something like call
afterward?如果 g++ 考虑
__rdtsc()
一个 function 调用,那么之后会有类似call
的东西吗?
Thanks.谢谢。
rbp
is the base pointer, not the stack pointer. rbp
是基指针,而不是堆栈指针。 The base pointer is used for backtrace during debugging but it is not necessary for actually running.基指针用于调试期间的回溯,但实际运行时不需要。
It is preserved through function calls so with -O3
only the expected assembly is generated:它通过 function 调用保留,因此使用
-O3
仅生成预期的程序集:
main:
rdtsc
salq $32, %rdx
orq %rdx, %rax
ret
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.