What is the prolog / epilog code for the assembly executed with rbp / rsp / leave?

I'm just starting to learn assembly for mac using the GCC compiler to build my code. Unfortunately, there are VERY limited resources for learning how to do this if you are a beginner. I finally managed to find a simple code sample that I could start tearing my mind apart, and I got it to build and run correctly. Here is the code:

.text                                           # start of code indicator.
.globl _main                                    # make the main function visible to the outside.
_main:                                          # actually label this spot as the start of our main function.
    push    %rbp                            # save the base pointer to the stack.
    mov     %rsp, %rbp                      # put the previous stack pointer into the base pointer.
    subl    $8, %esp                        # Balance the stack onto a 16-byte boundary.
    movl    $0, %eax                        # Stuff 0 into EAX, which is where result values go.
    leave                                   # leave cleans up base and stack pointers again.
    ret

Comments explain some things in the code (I understand what lines 2-5 do), but I don't understand what that means. I understand the basics of what registers and each register here ( rbp, rsp, espand eax) is used and how big they are, I also know the (usually) what is the stack, but it still goes over my head. Can someone tell me what exactly this is doing? Also, can someone point me towards a good beginner's tutorial?

+3
source share
1 answer

- , LIFO. ( , ) , x86 x86-64 . . Wikibooks x86 ( , - 32- x86- Intel, - 64 - x86-64 AT & T).

, ( Intel):

push %rbp

rbp , 8 rsp ( rbp 8 ), rbp [ss:rsp].

, Intel push rbp :

sub rsp, 8
mov [ss:rsp], rbp

:

mov     %rsp, %rbp

. rsp rbp.

subl    $8, %esp

8 esp esp. , . 32- (eax, ebx, ecx, edx, ebp, esp, esi edi)) x86-64 32 64- (rax, rbx, rcx, rdx, rbp, rsp, rsi rdi) , - 4 , ( Intel):

sub rsp,8
and rsp,0x00000000ffffffff

: sub esp,8 .

4 . 4 . leave sane rsp. x86-64 esp (, , ). :

subq    $8, %rsp

( $8 ). Wikibooks x86 ( , 32- x86 Intel, 64- x86-64 AT & T).

:

movl    $0, %eax

. 0 eax. .

leave

mov rsp, rbp, pop rbp.

ret

, , rip , [ss:rsp], , , 8 rsp.

+11

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


All Articles