I started to study the x86 assembly today by analyzing the assembly code corresponding to this C ++ example (I know there is something like atoi, but I wanted this example to be minimal):
#include <vector>
std::vector<int> range(int N) {
std::vector<int> v(N);
for (unsigned int i = 0; i < N; ++i)
v[i] = i;
return v;
}
int main() {
return range(100).back();
}
If compiled with g++ -O0 -S -fno-stack-protector return_by_value.cpp, this will result in this excerpt:
... <snip>
_Z5rangei:
.LFB509:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
.cfi_lsda 0x3,.LLSDA509
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $40, %rsp
.cfi_offset 3, -24
movq %rdi, -40(%rbp)
movl %esi, -44(%rbp)
leaq -25(%rbp), %rax
movq %rax, %rdi
call _ZNSaIiEC1Ev
movl $0, -24(%rbp)
movl -44(%rbp), %eax
movslq %eax, %rsi
leaq -25(%rbp), %rcx
leaq -24(%rbp), %rdx
... <snip>
I was surprised to see an odd (i.e. not a multiple of 8) offset:, leaq -25(%rbp), %raxmoreover, this is an instruction q, and we also have it -24(%rbp). For what reason does the compiler read across 8-byte boundaries?
Nibor source
share