์ด์ ๋ธ๋ฆฌ ์ฝ๋ ๋ถ์ 3 - example 3 :: ์กฐ๊ฑด๋ฌธ
* ์ด ๊ฒ์๊ธ์ AT&T ๋ฌธ๋ฒ์ ๋ฐ๋ผ ์์ฑ๋์์ต๋๋ค.
(AT&T ์ Intel ์ฐจ์ด ์ฐธ๊ณ -
REVERSING ์์ํ๊ธฐ - ๊ธฐ์ด ๊ฐ๋ 4 :: ์ด์ ๋ธ๋ฆฌ ์ธ์ด
์ด์ ๋ธ๋ฆฌ ์ธ์ด : ์์ฐ์ด์ ๊ธฐ๊ณ์ด ์ฌ์ด๋ก, CPU ์ ๋ช ๋ น์ด๋ค์ ์์ด์ ์ฝ์์ธ ๊ธฐํธ๋ก ํ๊ธฐํ ๊ฒ์ด๋ค. ์ด ๊ธฐํธ๋ค์ CPU ์ ๋ช ๋ น์ด(๊ธฐ๊ณ์ด) ์ ์ผ๋์ผ ๋์ํ๋ค. ์ปดํจํฐ ๊ตฌ์กฐ์ ๋ฐ๋ผ ์ฌ์ฉํ๋ ๊ธฐ๊ณ์ด
iforint.tistory.com
.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
.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 ์ ๋ฌธ์์ด ํ ๋น
movl $10, -8(%rbp) : -8(%rbp) ์ 10 ์ ์ฅ
movl $20, -4(%rbp) : -4(%rbp) ์ 20 ์ ์ฅ
cmpl $10, -8(%rbp) : -8(%rbp) ์ ๊ฐ(10) ๊ณผ 10 ๋น๊ต
์ฆ, ( -8(%rbp) - 10 )
jne .L2 : ๋น๊ต ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅด๋ฉด L2 ๋ก ์ ํ.
์ฆ, ZF ๊ฐ 0 ์ด์ด์ผ ์ ํ
movl $.LC0, %edi
call puts
.L2:
cmpl $10, -4(%rbp) : -4(%rbp) ์ ๊ฐ (20) ๊ณผ 10 ๋น๊ต
jne .L3 : ๋น๊ต ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅด๋ฉด L3 ๋ก ์ ํ
movl $.LC1, %edi
call puts
jmp .L4 : L4 ๋ก ์ ํ
.L3:
cmpl $20, -4(%rbp) : -4(%rbp) ์ ๊ฐ (20) ๊ณผ 20 ๋น๊ต
jne .L4 : ๋น๊ต ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅด๋ฉด 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
#include <stdio.h>
int main(){
int a = 10;
int b = 20;
if (a == 10)
puts("a is 10");
else 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;
}
์ด์ํ๋ค...
gcc ๋ก ์ด์ ๋ธ๋ฆฌ ์ฝ๋๋ฅผ ์์ฑํ ๊ฒฐ๊ณผ, ์ฒซ๋ฒ์งธ ๋ถ๊ธฐ์์ L3 ๋ก ์ ํํ๋ค.
๊ทธ๋์ ์กฐ๊ฑด๋ฌธ์ ์์๊ฐ L3 L4 ๊ฐ ์๋๋ผ L4 L3 ๊ฐ ๋ ๊ฒ์ธ๊ฑฐ ๊ฐ์๋ฐ..
๊ทธ๋ผ ์์ ์ ์ด์ ๋ธ๋ฆฌ ์ฝ๋์ ๋ค๋ฅด๋ค.
์ด์ ๋ธ๋ฆฌ ์ฝ๋์์ ์กฐ๊ฑด๋ฌธ์ cmp ์ ๋์๋๋๋ฐ, JMP ๋ else (if) ์ ๋์๋๋ ๊ฒ์ผ๋ก ์๊ฐํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
์ง๊ธ L1 ์์ ๋ง์ง๋ง์ jmp ๊ฐ ์๋ ๊ฑธ๋ก ๋ณด์ ์ดํ ๋จ๊ณ๋ฅผ ์ฝ๋๋ฅผ ๋ฐ๋ผ ์์ฐจ์ ์ผ๋ก ์งํ๋๋ ๊ฒ์ด ์๋๋ผ else (if) ๋ฅผ ํตํด์ ์ ํํด๊ฐ๋ ๊ฒฝ๋ก๊ฐ ์์์ ์๊ฒ ๋์๋ค.
๊ทธ๋ฌ๋๊น if(a==10) ๋ค์๋ else if ๊ฐ ์ค์ง ์์์ผ ํ๋ค๋ ๋ป์ด ๋๋ค.
๊ทธ๋ ๊ฒ ์๊ฐํ๊ณ ์ฝ๋๋ฅผ ์์ ํ๋, ๋ค์๊ณผ ๊ฐ์๊ณ , ์ด์ ๋ธ๋ฆฌ ์ฝ๋๋ก ๋ง๋๋ ์์ ์ฝ๋์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์๋ค.!!
#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;
}