
* ๊ธฐ๋ณธ ํด์
.file "example1.c"
: ๋๋ฒ๊ฑฐ์์ ์ฌ์ฉํ๋ ์๋ณธ ํ์ผ ์ด๋ฆ์ ๊ฐ๋ฆฌํด
โก Intel ๋ฌธ๋ฒ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ค๋ฅธ์ชฝ ๊ฐ์ ์ผ์ชฝ์ผ๋ก ๋ฃ๋๋ค !!
.section .rodata
: ./rodata ์น์ ์ ์, ์ด ์น์ ์ ์ฝ๊ธฐ ์ ์ฉ ๋ฐ์ดํฐ ๋ณ์
.text
: text section → ์ฝ๋๋ค์ ์ ํ ์น์ / ํ๋ก๊ทธ๋จ ์ฝ๋ ์คํํ ๋ ์ฌ์ฉ
.global main
: ์ ์ฒด ์ฝ๋์ ๊ฑธ์ณ ์ ๊ทผ ๊ฐ๋ฅํ ํจ์
.type main, @function
: main ํจ์ ์ ์ → ์ด๊ฑธ ํด์ผ๋ง global main ์ฝ๋๋ฅผ ์ป์ ์ ์๋ค
.LBF0
: ํจ์์ ์์์ ์๋ฏธํ๋ 'local label'
.cfi_startproc
: 'call from information'
์ด์ ๋ธ๋ฌ์๊ฒ dwarf format ๋๋ฒ๊น information์ ์ง์
.cfi_endproc
: ํจ์ ๋
.size main, .-main
: size ์ธ์คํธ๋ญ์ ์ ๋ฉ์ธ ํจ์์ ํฌ๊ธฐ๋ฅผ ์ธํ ํจ
* .-main ์ ์ค๋ธ์ ํธ ํ์ผ์ ์ฐ์ฌ์ง mainํจ์์ ์ ํํ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๊ณ ์์
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1)4.8.4"
: indent ์ธ์คํธ๋ญ์ ์ ์ด๋ค ์ปดํ์ผ๋ฌ๋ฅผ ์ฌ์ฉํด์ ์ปดํ์ผํ๊ณ ์คํํ์๋์ง์ ๋ํ ์ ๋ณด ๋ฌธ์์ด "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1)4.8.4"์ ์ค๋ธ์ ํธ ํ์ผ์ ์ ์ฅํ๊ธฐ ์ํ instruction
(ex) Hello World ํ๋ก๊ทธ๋จ : ์ค๋ธ์ ํธ ํ์ผ)
# example1.asm
# example1.asm
.file "example1.c"
.section .rodata
.LC0:
.string "Hello world"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section .note.GNU-stack,"",@progbits
ํด์ํ mainํจ์ ๋ถ๋ถ์ ๋ค์๊ณผ ๊ฐ๋ค.
main:
pushq %rbp
movq %rsp, %rbp
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
popq %rbp
ret
push1 %rbp → rbp ์คํ์ ์ ํผ์ฐ์ฐ์๋ฅผ ์ ์ฅํ๊ณ , rsp์ ๊ฐ์ 8 ์ค์ธ๋ค.
movq %rsp, %rbp → rbp์ ๊ฐ์ rsp์ ๋ฃ๋๋ค.
movl $.LC0. %edi → printf์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก LC0์ ์ ์ฅ๋ ๋ฌธ์๋ฅผ ์ ์ฅ
movl $0, %eax → eax์ 0์ ์ฅ
call printf → printf ํจ์ ํธ์ถ
movl $0, %eax → eax์ 0์ ์ฅ
popq %rbp → rbp ๊บผ๋
ret → return address ์ฃผ์๋ก ๋ฆฌํด
์ด๋ฅผ ๋ฐํ์ผ๋ก ์์ฑํ C์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}
๋์ค์ด์ ๋ธ ์ฝ๋์ด๋ฏ๋ก ์ค๋ฅธ์ชฝ ๊ฐ์ ์ผ์ชฝ์ผ๋ก ๋ฃ๋๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
๋์ค์ด์ ๋ธ ์ฝ๋๋ผ๋ ๊ฑธ ๊ณ ๋ คํ๋ฉด ์์์ ๋ดค๋ ์ด์ ๋ธ๋ฆฌ ์ฝ๋์ ๋์ผํ ๊ฑธ ํ์ธํ ์ ์๋ค.
*objdump -d๋ฅผ ์จ์ ์ด์ ๋ธ๋ฆฌ ์ฝ๋๋ฅผ ํ์ธํ๊ณ ์ถ์์ผ๋ ์๊พธ ์ค๋ฅ๊ฐ ๋ด๋ค...
# example2.asm
# example2.asm
.file "example2.c"
.section .rodata
.LC0:
.string "result : %d \n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $10, -12(%rbp)
movl $20, -8(%rbp)
movl -8(%rbp), %eax
movl -12(%rbp), %edx
addl %edx, %eax
movl %eax, -4(%rbp)
movl -8(%rbp), %eax
movl -12(%rbp), %edx
addl %edx, %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section .note.GNU-stack,"",@progbits
ํด์ํ mainํจ์ ๋ถ๋ถ์ ๋ค์๊ณผ ๊ฐ๋ค.
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $10, -12(%rbp)
movl $20, -8(%rbp)
movl -8(%rbp), %eax
movl -12(%rbp), %edx
addl %edx, %eax
movl %eax, -4(%rbp)
movl -8(%rbp), %eax
movl -12(%rbp), %edx
addl %edx, %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
ret
- push1 %rbp → ํจ์ ํ๋กค๋ก๊ทธ
- movq %rsp, %rbp → rsp์ ๊ฐ์ rbp์ ๋ฃ๋๋ค
- subq $16, %rsp → ์คํ ํฌ์ธํฐ rsp๋ฅผ 0x10(16byte)๋งํผ ๊ณต๊ฐ ํ๋ณด
- movl $10, -12(%rbp) → 10์ rbp ๋ ์ง์คํฐ์ -12๋งํผ์ ์ฃผ์(offset)์ ์ ์ฅ
- 12byte ๋งํผ์ ์์ ๊ณต๊ฐ์ ํ ๋นํด์ ์ซ์ 10์ ์คํ์ ๋ฃ๋ ๊ฒ! (๋ค์ ์ฐ์ฐ ์ค๋น)
- movl $20, -8(%rbp) → 20์ rbp ๋ ์ง์คํฐ์ rbp-0x8์ ์ ์ฅ
- movl -8(%rbp), %eax → eax๋ ์ง์คํฐ์ 20 ์ ์ฅ
- rbp๋ก๋ถํฐ 8byte ์๋์ ๊ณต๊ฐ์ ์ ์ฅ๋์ด ์๋ ๊ฐ(์ฌ๊ธฐ์ 20)์ eax ๋ ์ง์คํฐ์ ์ ์ฅ
- movl -12(%rbp), %ed rbp๋ก๋ถํฐ 12byte ์๋์ ๊ณต๊ฐ์ ์ ์ฅ๋์ด ์๋ ๊ฐ(10)์ edx์ ์ ์ฅ
- addl %edx, %eax → edx ๋ ์ง์คํฐ์ ์๋ ๊ฐ(10)๊ณผ eax ๋ ์ง์คํฐ์ ์๋ ๊ฐ(20) ๋ํด์ eax์ ์ ์ฅ
- movl %eax, -4(%rbp) → eax(30)๋ฅผ rbp-0x4์ ์ ์ฅ
- movl -8(%rbp), %eax → eax๋ ์ง์คํฐ์ 20 ์ ์ฅ
- movl -12(%rbp), %edx → edx์ 10์ ์ฅ
- addl %edx, %eax → 10 + 20(30)์ eax์ ์ ์ฅ
- movl %eax, %esi → esi์ 30 ์ ์ฅ
- movl $.LC0, %edi → printf์ ์ฒซ ๋ฒ์งธ ์ธ์ ๊ฐ์ LC0์ฃผ์์ ์คํธ๋ง์ผ๋ก ๊ฐ์ ธ์์ edi์ ์ ์ฅ
- movl $0, %eax → eax๋ฅผ 0์ผ๋ก ์ด๊ธฐํ
- call printf → printf ํจ์ ํธ์ถ
- movl $0, %eax → eax(๋ฆฌํด๊ฐ)๋ฅผ 0์ผ๋ก ์ด๊ธฐํ
- leave → ํจ์ ์ํ๋ก๊ทธ
- ret → return address ์ฃผ์๋ก ๋ฆฌํด
์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑํ c์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#include <stdio.h>
int main(){
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf("%d\n", a + b);
return 0;
}
์์ ์ฝ๋๋ฅผ ๋์ค์ด์ ๋ธํ๋ ์ด์ ๋ธํ ์ฝ๋์ ๋ฐ๋๋ก ๋ง์๋จ์ด์ก๋ค.
# example3
# example3.asm
.file "example3.c"
.section .rodata
.LC0:
.string "a is 10"
.LC1:
.string "b is 10"
.LC2:
.string "b is 20"
.LC3:
.string "a=b"
.LC4:
.string "a!=b"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $10, -8(%rbp)
movl $20, -4(%rbp)
cmpl $10, -8(%rbp)
jne .L2
movl $.LC0, %edi
call puts
.L2:
cmpl $10, -4(%rbp)
jne .L3
movl $.LC1, %edi
call puts
jmp .L4
.L3:
cmpl $20, -4(%rbp)
jne .L4
movl $.LC2, %edi
call puts
.L4:
movl -8(%rbp), %eax
cmpl -4(%rbp), %eax
jne .L5
movl $.LC3, %edi
call puts
jmp .L6
.L5:
movl $.LC4, %edi
call puts
.L6:
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section .note.GNU-stack,"",@progbits
main ์ด์ ์ ๋จผ์ ์ดํด๋ณด์.
.LC0:
.string "a is 10"
.LC1:
.string "b is 10"
.LC2:
.string "b is 20"
.LC3:
.string "a=b"
.LC4:
.string "a!=b"
๋ค์๊ณผ ๊ฐ์ด LC0, LC1, LC2, LC3, LC4์ ๊ฐ๊ฐ ๋ฌธ์์ด์ ํ ๋นํด์ฃผ์๋ค.
์ด์ mainํจ์์ ์ด์ ๋ธ๋ฆฌ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $10, -8(%rbp)
movl $20, -4(%rbp)
cmpl $10, -8(%rbp)
jne .L2
movl $.LC0, %edi
call puts
.L2:
cmpl $10, -4(%rbp)
jne .L3
movl $.LC1, %edi
call puts
jmp .L4
.L3:
cmpl $20, -4(%rbp)
jne .L4
movl $.LC2, %edi
call puts
.L4:
movl -8(%rbp), %eax
cmpl -4(%rbp), %eax
jne .L5
movl $.LC3, %edi
call puts
jmp .L6
.L5:
movl $.LC4, %edi
call puts
.L6:
movl $0, %eax
leave
ret
- pushq %rbp → ํจ์ ํ๋กค๋ก๊ทธ
- movq %rsp, %rbp → rsp์ rbp๊ฐ์ ๋ฃ๋๋ค
- subq $16, %rsp → rsp๋ฅผ 0x10(16byte)์ผ๋ก ์คํ ๊ณต๊ฐ ํ๋ณด
- movl $10, -8(%rbp) → rbp-0x8์ 10์ ์ฅ
- movl $20, -4(%rbp) →rbp-0x4์ 20์ ์ฅ
- cmpl $10, -8(%rbp) → rbp-0x8๊ฐ (10)๊ณผ 10๋น๊ต (a == 10?)
- jne .L2 → ๋น๊ต ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅด๋ค๋ฉด L2๋ก ์ ํ
- movl $.LC0, %edi → ๊ฐ๋ค๋ฉด "a is 10"์ ์ธ์๋ก ๊ฐ์ ธ์ด
- call puts → puts ํจ์ ํธ์ถ
.L2:
- cmpl $10, -4(%rbp) → rbp-0x4์ ๊ฐ(20)๊ณผ 10 ๋น๊ต (b == 10?)
- jne .L3 → ๋น๊ตํด์ ๊ฐ์ง ์์ผ๋ฉด L3์ผ๋ก ์ ํ
- movl $.LC1, %edi → ๋ง๋ค๋ฉด "b is 10"์ ์ธ์๋ก ๊ฐ์ ธ์ด
- call puts → puts ํจ์ ํธ์ถ
- jmp .L4 → L4๋ก ์ ํ
.L3:
- cmpl $20, -4(%rbp) → rbp-0x4์ ๊ฐ(20)๊ณผ 20 ๋น๊ต (b == 20?)
- jne .L4 → ๋น๊ตํด์ ๊ฐ์ง ์์ผ๋ฉด L4๋ก ์ ํ
- movl $.LC2,%edi → ๋ง๋ค๋ฉด "b is 20"์ ์ธ์๋ก ๊ฐ์ ธ์ด
- call puts → puts ํจ์ ํธ์ถ
.L4:
- movl -8(%rbp), %eax → eax์ ๊ฐ rbp-0x8์ ์ ์ฅ (c = a)
- cmpl -4(%rbp), %eax → eax์ ๊ฐ๊ณผ rbp-0x4์ ๊ฐ(20)์ ๋น๊ต (c == b?)
- jne .L5 → ๋น๊ตํด์ ๋ค๋ฅด๋ค๋ฉด L5๋ก ์ ํ
- movl $.LC3, %edi → ๋ง๋ค๋ฉด "a=b"์ ์ธ์๋ก ๊ฐ์ ธ์ด
- call puts → puts ํจ์ ํธ์ถ
.L5:
- movl $.LC4, %edi → "a!=b"๋ฅผ ์ธ์๋ก ๊ฐ์ ธ์ด
- call puts → puts ํจ์ ํธ์ถ
.L6:
- movl $0, %eax → eax๋ฅผ 0์ผ๋ก ์ด๊ธฐํ
- leave → ํจ์ ์ํ๋ก๊ทธ
- ret → return address ์ฃผ์ ๋ฐํ
์์ฑํด์ค C ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#include <stdio.h>
int main(){
int a = 10;
int b = 20;
if (a == 10)
puts("a is 10");
if (b == 10)
puts("b is 10");
else if (b == 20)
puts("b is 20");
if (a == b)
puts("a=b");
else
puts("a!=b");
return 0;
}
์ด๋ฅผ ๋ค์ ๋์ค์ด์ ๋ธ ํด๋ณธ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.
# example4.asm
# example4.asm
.file "example4.c"
.section .rodata
.LC0:
.string "result : %d\n"
.text
.globl function
.type function, @function
function:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
imull -8(%rbp), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size function, .-function
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $50, -8(%rbp)
movl $60, -4(%rbp)
movl -4(%rbp), %edx
movl -8(%rbp), %eax
movl %edx, %esi
movl %eax, %edi
call function
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section .note.GNU-stack,"",@progbits
์ ์ฒด ์ฝ๋๋ ์์ ๊ฐ๊ณ ,
์ด ์ค์์ ๋ค์ ์ฝ๋๋ฅผ ๋ณด๋ฉด
.LC0:
.string "result : %d\n"
.LC0: ๋ "result : %d\n"์ด๋ผ๋ ์คํธ๋ง์ผ๋ก ์ ์๊ฐ ๋์ด์์์ ์ ์ ์๋ค.
์ด๋ฒ์๋ main ํจ์ ๋ง๊ณ ๋ ๋ ๋ค๋ฅธ ํจ์ function์ด ๋ฑ์ฅํ๋ค.
function:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
imull -8(%rbp), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
leave
ret
- pushq %rbp → ํจ์ ํ๋กค๋ก๊ทธ
- movq %rsp, %rbp → rsp์ rbp์ ๊ฐ์ ๋ฃ๋๋ค
- subq $16, %rsp → rsp๋ฅผ 0x10(16byte)์ผ๋ก ์คํ ํ๋ณด
- movl %edi, -4(%rbp) → rbp-0x4์ ๋งค๊ฐ๋ณ์๋ก ๊ฐ์ ธ์จ edi์ ์ฅ
- movl %esi, -8(%rbp) → rbp-0x8์ ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก ๊ฐ์ ธ์จ esi ์ ์ฅ
- movl -4(%rbp), %eax → eax์ rbp-0x4์ ์ฅ
- imull -8(%rbp), %eax → eax์ rbp-0x8 ๊ฐ ๊ณฑํด์ eax์ ์ ์ฅ
- movl %eax, %esi → esi์ ๋ ๋ฒ์งธ ์ธ์๋ก eax๋ฅผ ๋ฃ์
- movl $.LC0, %edi → ์ฒซ ๋ฒ์งธ ์ธ์๋ก "result : %d\n" ๊ฐ์ ธ์ด
- movl $0, %eax → eax 0์ผ๋ก ์ด๊ธฐํ
- call printf → printf ํจ์ ํธ์ถ
- leave → ํจ์ ์ํ๋ก๊ทธ
- ret → return address ์ฃผ์ ๋ฐํ
int function(int a, int b) {
printf("result : %d\n", a*b);
return 0;
}
function ํจ์๋ฅผ C์ธ์ด๋ก ๋ํ๋ด๋ฉด ์์ ๊ฐ๋ค.
์ด์ mainํจ์๋ฅผ ์ดํด๋ณด์.
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $50, -8(%rbp)
movl $60, -4(%rbp)
movl -4(%rbp), %edx
movl -8(%rbp), %eax
movl %edx, %esi
movl %eax, %edi
call function
movl $0, %eax
leave
ret
- pushq %rbp → ํจ์ ํ๋กค๋ก๊ทธ
- movq %rsp, %rbp → rsp์ rbp๊ฐ์ ๋ฃ๋๋ค.
- subq $16, %rsp → rsp๋ฅผ 0x10(16byte)์ผ๋ก ๊ณต๊ฐ ํ๋ณด
- movl $50, -8(%rbp) → rbp-0x8์ 50 ์ ์ฅ
- movl $60, -4(%rbp) → rbp-0x4์ 60 ์ ์ฅ
- movl -4(%rbp), %edx → ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์ edx์ rbp-0x4์ ๊ฐ(60) ์ ์ฅ
- movl -8(%rbp), %eax → ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์ eax์ rbp-0x8์ ๊ฐ(50) ์ ์ฅ
- movl %edx, %esi → esi ๋ ์ง์คํฐ์ edx(function์ ๋ ๋ฒ์งธ ์ธ์) ์ ์ฅ
- movl %eax, %edi → edi ๋ ์ง์คํฐ์ eax(function์ ์ฒซ ๋ฒ์งธ ์ธ์) ์ ์ฅ
- call function → function (ํจ์)ํธ์ถ
- movl $0, %eax → eax๋ฅผ 0์ผ๋ก ์ด๊ธฐํ
- leave → ํจ์ ์ํ๋ก๊ทธ
- ret → ret address ์ฃผ์ ๋ฐํ
mainํจ์์ function ํจ์๋ฅผ ๋ชจ๋ C์ธ์ด ์ฝ๋๋ก ๋ฐ๊พธ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
#include <stdio.h>
int function(int a, int b) {
printf("result : %d\n", a*b);
return 0;
}
int main() {
int a = 50;
int b = 60;
function(a, b);
return 0;
}
# example5.asm
# example5.asm
.file "example5.c"
.section .rodata
.LC0:
.string "number %d \n"
.LC1:
.string "%d * %d = %d \n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0, -8(%rbp)
jmp .L2
.L3:
movl -8(%rbp), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
addl $1, -8(%rbp)
.L2:
cmpl $9, -8(%rbp)
jle .L3
movl $0, -4(%rbp)
jmp .L4
.L5:
movl -8(%rbp), %eax
imull -4(%rbp), %eax
movl %eax, %ecx
movl -4(%rbp), %edx
movl -8(%rbp), %eax
movl %eax, %esi
movl $.LC1, %edi
movl $0, %eax
call printf
addl $1, -4(%rbp)
.L4:
cmpl $4, -4(%rbp)
jle .L5
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section .note.GNU-stack,"",@progbits
.LC0:
.string "number %d \n"
.LC1:
.string "%d * %d = %d \n"
์ ์ฝ๋๋ฅผ ๋ณด๋ฉด .LC0๊ณผ .LC1์ ๊ฐ๊ฐ์ ๋ฌธ์์ด๋ก ์ ์ํ ๊ฑธ ํ์ธํ ์ ์๋ค.
main ํจ์๋ฅผ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $0, -8(%rbp)
jmp .L2
.L3:
movl -8(%rbp), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
addl $1, -8(%rbp)
.L2:
cmpl $9, -8(%rbp)
jle .L3
movl $0, -4(%rbp)
jmp .L4
.L5:
movl -8(%rbp), %eax
imull -4(%rbp), %eax
movl %eax, %ecx
movl -4(%rbp), %edx
movl -8(%rbp), %eax
movl %eax, %esi
movl $.LC1, %edi
movl $0, %eax
call printf
addl $1, -4(%rbp)
.L4:
cmpl $4, -4(%rbp)
jle .L5
movl $0, %eax
leave
ret
- pushq %rbp → ํจ์ ํ๋กค๋ก๊ทธ
- movq %rsp, %rbp → rsp์ rbp๊ฐ์ ๋ฃ๋๋ค.
- subq $16, %rsp → rsp๋ฅผ 0x10(16byte)๋งํผ ์คํ ๊ณต๊ฐ ํ๋ณด
- movl $0, -8(%rbp) → rbp-0x8์ 0 ์ ์ฅ
- jmp .L2 → L2๋ก ์ ํ
.L3:
- movl -8(%rbp), %eax → eax ๋ ์ง์คํฐ์ rbp-0x8 ์ ์ฅ
- movl %eax, %esi → esi ๋ ์ง์คํฐ์ eax ๊ฐ(rbp-0x8) ์ ์ฅ
- movl $.LC0, %edi → edi ๋ ์ง์คํฐ์ LC0 ๋ฌธ์์ด ์ ์ฅ
- movl $0, %eax → eax ๋ ์ง์คํฐ 0์ผ๋ก ์ด๊ธฐํ
- call printf → printf ํจ์ ํธ์ถ
- addl $1, -8(%rbp) → rbp-0x8์ 1 ๋ํจ (i++)
.L2:
- cmpl $9, -8(%rbp) → rbp-0x8์ ๊ฐ๊ณผ 9 ๋น๊ต
- jle .L3 → ์๊ฑฐ๋ ๊ฐ์ผ๋ฉด L3์ผ๋ก ์ด๋
- movl $0, -4(%rbp) → ํฌ๋ฉด rbp-0x4์ ์ ์ฅ
- jmp .L4 → L4๋ก ์ด๋ (๋ฃจํ์์ ๋์ค๋ฉด)
.L5:
- movl -8(%rbp), %eax → eax ๋ ์ง์คํฐ์ rbp-0x8 ๊ฐ ์ ์ฅ
- imull -4(%rbp), %eax → eax = eax * [rbp-0x4]
- movl %eax, %ecx → ecx์ eax ๊ฐ ์ ์ฅ
- movl -4(%rbp), %edx → edx์ rbp-0x4๊ฐ ์ ์ฅ
- movl -8(%rbp), %eax → eax์ rbp-0x8๊ฐ ์ ์ฅ
- movl %eax, %esi → ๋ ๋ฒ์งธ ์ธ์ eax๋ก ์ด๋
- movl $.LC1, %edi → ์ฒซ ๋ฒ์งธ ์ธ์ LC1 ๋ฌธ์์ด๋ก ์ด๋
- movl $0, %eax → eax 0์ผ๋ก ์ด๊ธฐํ
- call printf → printf ํจ์ ํธ์ถ
- addl $1, -4(%rbp) → rbp-0x4 ๊ฐ์ 1 ์ฆ๊ฐ
.L4:
- cmpl $4, -4(%rbp) → rbp-0x4๊ฐ๊ณผ 4 ๋น๊ต
- jle .L5 → 4๋ณด๋ค ๊ฐ๊ฑฐ๋ ์์ผ๋ฉด L5๋ก ์ด๋
- movl $0, %eax → eax 0์ผ๋ก ์ด๊ธฐํ
- leave → ํจ์ ์ํ๋ก๊ทธ
- ret → return address ๋ฐํ
C์ธ์ด๋ก ๋ฐ๊ฟ์ค ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#include <stdio.h>
int main() {
int a = 0, b;
for (int i = 0; i < 10; i++) {
printf("number %d \n", a);
a++;
}
for (int j = 0; j < 5; j++) {
b = j;
printf("%d * %d = %d \n", a, b, a*b);
}
return 0;
}
'DreamHack > Reversing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Reversing.Kr] Easy Keygen (0) | 2022.07.03 |
---|