I want to learn about C call. For this, I wrote the following code:
#include <stdio.h>
#include <stdlib.h>
struct tstStruct
{
void *sp;
int k;
};
void my_func(struct tstStruct*);
typedef struct tstStruct strc;
int main()
{
char a;
a = 'b';
strc* t1 = (strc*) malloc(sizeof(strc));
t1 -> sp = &a;
t1 -> k = 40;
my_func(t1);
return 0;
}
void my_func(strc* s1)
{
void* n = s1 -> sp + 121;
int d = s1 -> k + 323;
}
Then I used GCC with the following command:
gcc -S test3.c
and came up with the assembly. I will not show all the code I received, but rather I will insert the code for the my_func function. It:
my_func:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq %rdi, -24(%rbp)
movq -24(%rbp), %rax
movq (%rax), %rax
addq $121, %rax
movq %rax, -16(%rbp)
movq -24(%rbp), %rax
movl 8(%rax), %eax
addl $323, %eax
movl %eax, -4(%rbp)
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
As I understand it, this is what happens: First, the caller database pointer is pushed onto the stack, and its stack pointer becomes the new base pointer for setting the stack for the new function. But then everything else I do not understand. As far as I know, arguments (or a pointer to an argument) are stored on the stack. If so, what is the purpose of the second instruction,
movq -24(%rbp), %rax
% rax 24 % rbp. % rax???? ? , . , , . !