Here is some simple C++ code. I define a variable-length array in test_func
. I want to know the relationship between the array arr
and the rbp
register.
void test_func(int m, int n) {
int arr[m];
for(int i = 0; i < m; ++i) {
arr[i] = m;
}
cout << "ok";
}
int main() {
int m = 30, n = 12;
test_func(m, n);
return 0;
}
I use gdb to check out their addresses, but I find out nothing.
Then I use g++ -E test.cpp -o test.i , g++ -S test.i -o test.S
. I find some strange Assembly code.
_Z9test_funcii:
.LFB1559:
pushq %rbp
.seh_pushreg %rbp
pushq %r12
.seh_pushreg %r12
subq $40, %rsp
.seh_stackalloc 40
leaq 128(%rsp), %rbp
.seh_setframe %rbp, 128
.seh_endprologue
movl %ecx, -64(%rbp)
movl %edx, -56(%rbp)
movq %rsp, %rax
movq %rax, %r8
movl -64(%rbp), %eax
cltq
subq $1, %rax
movq %rax, -112(%rbp)
movq %rax, %rdx
addq $1, %rdx
movq %rdx, %r11
movl $0, %r12d
movq %rax, %rdx
addq $1, %rdx
movq %rdx, %r9
movl $0, %r10d
addq $1, %rax
salq $2, %rax
addq $15, %rax
shrq $4, %rax
salq $4, %rax
call ___chkstk_ms
subq %rax, %rsp
movq %rsp, %rax
addq $3, %rax
shrq $2, %rax
salq $2, %rax
movq %rax, -120(%rbp)
movl $0, -100(%rbp)
.L3:
movl -100(%rbp), %eax
cmpl -64(%rbp), %eax
jge .L2
movq -120(%rbp), %rdx
movl -100(%rbp), %eax
cltq
movl -64(%rbp), %ecx
movl %ecx, (%rdx,%rax,4)
addl $1, -100(%rbp)
jmp .L3
.L2:
movq %r8, %rsp
nop
leaq -88(%rbp), %rsp
popq %r12
popq %rbp
ret
.seh_endproc
.def __main; .scl 2; .type 32; .endef
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
I want to know the position of arr
on the stack and its relationship with the rbp
register.