Understanding C parsed call

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???? ? , . , , .    !

+5
3

AT & T Intel.

movq -24(%rbp), %rax

Intel

mov rax,[rbp-24]

, rbp, rax, . - src, dest AT & T, Intel dest, src.

, GAS, , gcc gcc test3.c ndisasm -b 64 a.out. my_func, NDISASM , Intel:

000005EF  55                push rbp
000005F0  4889E5            mov rbp,rsp        ; create the stack frame.
000005F3  48897DE8          mov [rbp-0x18],rdi ; s1 into a local variable.
000005F7  488B45E8          mov rax,[rbp-0x18] ; rax = s1 (it a pointer)
000005FB  488B00            mov rax,[rax]      ; dereference rax, store into rax.
000005FE  4883C079          add rax,byte +0x79 ; rax = rax + 121
00000602  488945F8          mov [rbp-0x8],rax  ; void* n = s1 -> sp + 121
00000606  488B45E8          mov rax,[rbp-0x18] ; rax = pointer to s1
0000060A  8B4008            mov eax,[rax+0x8]  ; dereference rax+8, store into eax.
0000060D  0543010000        add eax,0x143      ; eax = eax + 323
00000612  8945F4            mov [rbp-0xc],eax  ; int d = s1 -> k + 323
00000615  5D                pop rbp
00000616  C3                ret

Linux x86-64 (System V ABI) . UNIX Linux x86-64.

+9

( ):

-, :

pushq   %rbp
movq    %rsp, %rbp

%rbp , . %rbp %rsp ( %rbp push).

movq    %rdi, -24(%rbp)

i386 V ABI amd64 system V ABI.

i386 System V ABI ( ). , amd64 System V ABI (%rdi, %rsi, %rdx, %rcx, %r8 %r9, , %xmm0 %xmm7, float). , , i386.

, ( ) .

movq    -24(%rbp), %rax

, %rdi %rax. , %rax ( ) .

movq    (%rax), %rax

%rax.

addq    $121, %rax

121 .

movq    %rax, -16(%rbp)

.

movq    -24(%rbp), %rax

%rax (, -24(%rbp)).

movl    8(%rax), %eax
addl    $323, %eax

, %eax, 323 %eax.

, %rax %eax, , , void* (64 ), , int (32 ).

movl    %eax, -4(%rbp)

, (, , , , , - , ).

popq    %rbp
ret

-, main.

, .

+6

You can go to intel syntax by entering the following command:

$ gcc -S -masm=intel test3.c -o test3.s
+1
source

All Articles