카테고리

asm (27) bootloader_x86_grub (1) C (92) compile (11) config (76) CPP (13) CSS (1) debugging (7) gimp (1) Go (1) html (1) Java (1) JavaScript (1) kernel (19) LibreOffice (3) Linux system progamming (21) MFC (1) opencv (4) OpenGL (1) PHP (1) Python (4) qemu (29) shell (3) socket (7) troubleshooting (2) ubuntu18.04 (2) windows (1)

2018/12/06

asm 스택(함수 내부 구조)

스택 (함수 내부)
스택 이해 - 스택 구조
스택 관계 레지스트
EBP: 스택 베이스 주소를 가리키는 레지스터
DSP: 스택 최상단 주소를 가리키는 레지스터
EIP: 다음 실행할 명령어를 가리키는 레지스터(함수 종료시 RET Address 추가)

100 <--- High Memory(100)
...
...
...
...
050 Ret Address
051 EBP <---EBP
...
...
...
000 <--- Low Memory(0) == ESP

Add(4,8) 동작.
100 <--- High Memory(100)
...
...
... 8 int b == mov eax,DWORD PTR[ebp+0xc]
... 4 int a == mov eax,DWORD PTR[ebp+0x8]
050 Ret Address
051 EBP <---EBP 함수호출 Add(4,8)
... 값 대입 0xc = int c; == mov eax,DWORD PTR[ebp-0x4]
...
...
000 <--- Low Memory(0) == ESP

Add(4,8) // 함수 호출 시 스택 메모리 위치에 변수 값 대입
int add(int a, int b){
    int c;
    c=a+b;
    return c
}

In Assembly
push ebp
mov ebp, esp
sub esp 0x10

mov eax, DWORD PTR [ebp+oxC];12
mov edx, DWORD PTR [ebp+0x8];8
lea eax,[ebx+eax*1]
mov DWORD PTR[ebp-0x4),eax
mov eax, DWORD PTR [ebp-0x-4]

leave
ret

asm
vi asm1.c
#include <stdio.h>
int main(){
    return oxff;
}

Compile:
gcc -o asm1 asm1.c

objdump -d -o -M intel asm1
검색 main
메모리 구조 확인.

댓글 없음:

댓글 쓰기