example3.asm의 코드는 아래와 같다.
# 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 함수 부분을 살펴보았다.
main:
.LFB0:
.cfi_startproc
pushq %rbp #rbp를 스택에 push
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp #rbp를 rsp로 옮겨줌, rbp = rsp
.cfi_def_cfa_register 6
subq $16, %rsp #rsp-16으로 스택 공간 확보
movl $10, -8(%rbp) #rbp-8에 10 저장
movl $20, -4(%rbp) #rbp-4에 20 저장
cmpl $10, -8(%rbp) #rbp-8에 저장된 값과 10을 비교
jne .L2 #jne : jump if not equal -> 위에서 비교한 값이 같지 않으면 L2로 점프
movl $.LC0, %edi #edi에 LC0 저장, 여기서 LC0은 "a is 10"
call puts #puts 함수 호출
.L2:
cmpl $10, -4(%rbp) #rbp-4에 저장된 값과 10을 비교
jne .L3 #위에서 비교한 값이 같지 않으면 L3으로 점프
movl $.LC1, %edi #edi에 LC1 저장, 여기서 LC1은 "b is 10"
call puts #puts 함수 호출
jmp .L4 #L4로 점프
.L3:
cmpl $20, -4(%rbp) #rbp-4에 저장된 값과 20을 비교
jne .L4 #위에서 비교한 값이 같지 않으면 L4로 점프
movl $.LC2, %edi #edi에 LC2 저장, 여기서 LC2는 "b is 20"
call puts #puts 함수 호출
.L4:
movl -8(%rbp), %eax #eax에 rbp-8에 저장된 값을 저장
cmpl -4(%rbp), %eax #eax에 저장된 값과 rbp-4에 저장된 값을 비교
jne .L5 #위에서 비교한 값이 같지 않으면 L5로 점프
movl $.LC3, %edi #edi에 LC3 저장, 여기서 LC3은 "a=b"
call puts #puts 함수 호출
jmp .L6 #L6으로 점프
.L5:
movl $.LC4, %edi #edi에 LC4 저장, 여기서 LC4는 "a!=b"
call puts #puts 함수 호출
.L6:
movl $0, %eax #eax에 0 저장
leave #rbp를 꺼내 원래의 스택 프레임으로 돌아감
.cfi_def_cfa 7, 8
ret #return address 주소로 리턴
.cfi_endproc
위의 내용을 바탕으로 C언어로 나타내면 아래와 같다.
#include <stdio.h>
int main() {
int a, b;
a = 10;
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;
}
'Security > Reversing' 카테고리의 다른 글
[어셈블리어 분석 실습] example 5 (0) | 2022.05.08 |
---|---|
[어셈블리어 분석 실습] example 4 (0) | 2022.05.08 |
[어셈블리어 분석 실습] example 2 (0) | 2022.05.08 |
[어셈블리어 분석 실습] example 1 (0) | 2022.05.08 |
[어셈블리어 분석 실습] 기본 내용 (0) | 2022.05.08 |
댓글