deS*_*gis 2 c++ g++ disassembly
我有一个类似于这个的真实世界程序,我将称之为test.cpp:
#include <stdlib.h>
extern void f(size_t i);
int sample(size_t x)
{
size_t a = x;
size_t i;
for (i = a-2; i>=0; i--) {
f(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我是一个无限循环.
如果我运行以下命令:
g++ -S -o test.s test.cpp
Run Code Online (Sandbox Code Playgroud)
我得到以下装配顺序:
.file "test.cpp"
.text
.globl _Z6samplem
.type _Z6samplem, @function
_Z6samplem:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %rdi, -24(%rbp)
movq -24(%rbp), %rax
movq %rax, -8(%rbp)
movq -8(%rbp), %rax
subq $2, %rax
movq %rax, -16(%rbp)
.L2:
movq -16(%rbp), %rax
movq %rax, %rdi
call _Z1fm
subq $1, -16(%rbp)
jmp .L2
.cfi_endproc
.LFE0:
.size _Z6samplem, .-_Z6samplem
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)
我不是汇编语言的专家,但我希望看到比较的代码i >= 0和循环中的条件跳转.这里发生了什么??
Ubuntu Linux上的GNU C++ 4.6.3
Jam*_*lis 11
size_t是无符号的,所以条件i>=0总是如此true.i要消极是不可能的.