본문 바로가기

Learning/└◆Reversing

[참고]어셈블리 예제

 

[EX] sample.c (C 언어 작성) --- 변환 --> sample.a (어셈블리어)

 

$ cd

$ cd tmp

$ vi sample.c

int function(int a, int b){
                char buffer[10];
                a = a + b;
                return a;
}

 

int main(void){
                int c;
                c = function(1,2);
                return 0;
}

$ gcc -S -o sample.a sample.c

$ ls -l

-rw-rw-r-- 1 level1 level1 570 1125 12:17 sample.a

-rw-rw-r-- 1 level1 level1 131 1125 12:16 sample.c

 

$ file *

sample.a: ASCII assembler program text

sample.c: ASCII C program text

 

$ vi sample.a

        .file   "sample.c"
        .text
.globl function
        .type   function,@function
function:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $24, %esp
        movl    12(%ebp), %eax
        addl    %eax, 8(%ebp)
        movl    8(%ebp), %eax
        leave
        ret
.Lfe1:
        .size   function,.Lfe1-function
.globl main
        .type   main,@function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax

        subl    %eax, %esp
        subl    $8, %esp
        pushl   $2
        pushl   $1
        call    function
        addl    $16, %esp
        movl    %eax, -4(%ebp)
        movl    $0, %eax
        leave
        ret
.Lfe2:
        .size   main,.Lfe2-main
        .ident  "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"

 

 

 

[EX2] sample.c (C 언어) -----> sample (바이너리) -----> disassemble (어셈블리어)

$ gcc -o sample sample.c

$ file sample*

sample: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped

sample.a: ASCII assembler program text

sample.c: ASCII C program text

 

$ gdb -q sample

(gdb) disassemble main

Dump of assembler code for function main:

0x08048305  <main+0>:  push  %ebp

0x08048306  <main+1>:  mov   %esp,%ebp

0x08048308  <main+3>:  sub   $0x8,%esp

0x0804830b  <main+6>:  and   $0xfffffff0,%esp

0x0804830e  <main+9>:  mov   $0x0,%eax

0x08048313  <main+14>: sub   %eax,%esp

0x08048315  <main+16>: sub   $0x8,%esp

0x08048318  <main+19>: push  $0x2

0x0804831a  <main+21>: push  $0x1

0x0804831c  <main+23>: call  0x80482f4 <function>

0x08048321  <main+28>: add   $0x10,%esp

0x08048324  <main+31>: mov   %eax,0xfffffffc(%ebp)

0x08048327  <main+34>: mov   $0x0,%eax

0x0804832c  <main+39>: leave

0x0804832d  <main+40>: ret

0x0804832e  <main+41>: nop

0x0804832f  <main+42>: nop

End of assembler dump.

 

 

 

(정리)

sample.c (C 언어) -----> sample.a (어셈블리어)

sample.c (C 언어) -----> sample (바이너리) ----- gdb ----> disassemble (어셈블리어)