Assembler Functions

I philosophized about the purpose of the stack a bit, and after some coding, I realized what kind of power it was. The only thing that lies in my stomach is how it works with functions? I tried to make some simple functions to add two numbers using universal registers, but I suppose it’s not how it works in C, for example .. where are all the parameters, local variables and where is the result stored?

how would you rewrite this with assembler? (how would a compiler for C rewrite it?)

int function(int a, int &b, int *c){
 return a*(b++)+(*c);
}

I know this example, it seems to suck .. but in this way I can understand all the possibilities

+3
source share
3 answers

, . , , . , .

+5

-, (int&) C, ++.

, gcc, -S. .

g++ -S func.c

func.s, ( .. x86):

    .text
.globl __Z8functioniRiPi
__Z8functioniRiPi:
LFB2:
    pushq   %rbp
LCFI0:
    movq    %rsp, %rbp
LCFI1:
    movl    %edi, -4(%rbp)
    movq    %rsi, -16(%rbp)
    movq    %rdx, -24(%rbp)
    movq    -16(%rbp), %rax
    movl    (%rax), %edx
    movl    %edx, %ecx
    imull   -4(%rbp), %ecx
    movq    -24(%rbp), %rax
    movl    (%rax), %eax
    addl    %eax, %ecx
    incl    %edx
    movq    -16(%rbp), %rax
    movl    %edx, (%rax)
    movl    %ecx, %eax
    leave
    ret

++ (__Z8functioniRiPi). g++ -O2:

    .text
    .align 4,0x90
.globl __Z8functioniRiPi
__Z8functioniRiPi:
LFB2:
    pushq   %rbp
LCFI0:
    movq    %rsp, %rbp
LCFI1:
    movl    (%rsi), %ecx
    movl    %ecx, %eax
    imull   %edi, %eax
    addl    (%rdx), %eax
    incl    %ecx
    movl    %ecx, (%rsi)
    leave
    ret

-O3 ; .

, . ^ _ ^

+5

What Aaron said about the designation of conventions is the right answer. For my personal study of the topic, I found Smashing the Stack For Fun and Profit to be a great exercise as a stack frame and what can happen when it gets corrupted. Finlay I recommend a collection for hackers that discusses important assembler concepts as well as interesting things you can do with your debugger.

+3
source

Source: https://habr.com/ru/post/1736366/


All Articles