Instructions Set Architecture
기기의 instructions과 states를 결정한다. 상태(레지스터 + 메모리)
이를 구현하는 것이
Microarchitecture (=cpu)
동일 ISA에 대해 여러 microarchitecture가 존재한다!!!
Classification of ISA
수많은 방식으로 구분되는데, 그중 가장 흔한 방식이 architectural complexity
1. CISC
Complex Instruction Set Computers
- 가변크기 명령어
- 복잡하지만 코드 사이즈는 작음
ex ) x86, x86-64 (intel, AMD)
2. RISC
Reduced Instruction Set Computers
- 명령어 단순
- 고정크기 명령어 → 보통 4 바이트
ex) ARM, MIPS, RISC-V
Instuction
1. Opcode
operator 작동 구체화
메모리와 레지스터 사이 데이터를 이동 (mov)
산술 함수 수행함 → 레지스터/메모리 데이터 (add, sub)
Branch to another location in the program.. 실행흐름 변경 ( jmp , call, io)
2. Operands
매개변수 특정화
Literal (상수 변수)
Registers
Memory address
Machine State
= 레지스터 + 메모리 상태
ARU나 Cache는 transparent화 된다! ↔ vertual
x86-64 Registers
1) 가장 basic한 general purpose registers
- 각각의 레지스터는
64비트 quad word
32비트 double word
16비트 word
8비트 byte
→ legacy compatibility
2) Other registers
%rip : 포인터 레지스터. 특수목적 레지스터로 다음 명령 수행 주소를 나타낸다
%rflag : 플래그 레지스터 . 상태정보를 담고 있는 레지스터 → 상태코드
%rsp : 스탭 포인터
※ 오리지널 8086 은 16 비트 → 여전히 8비트 지원
※ 32비트는 IA32 를 extension
argument
1. %rdi 2. %rse 3. %rdx ... 4. %rcx 5. %r8 6. %r9
→ 6개 초과한 추가적인 argument는 스택에 저장된다. stack → 메모리
%rax → return value
Compiling into Assembly
1.어셈블리어로 파일을 생성하는 c 컴파일러 사용
어셈블리(중간단계)는 machine-level execution model의 핵심이다.
하드웨어는 오직 binary representation 만 이해하기 때문에!
$gcc -S ex.c -Og -fcf-protection=none
→ .s 확장자로 저장된 어셈블리 코드
정적분석 - 메모리, 변수값, 스택상태 알수 없음.
프로그램 실행 x
2. GNU gdb
$gcc -c -Og ex.c -fcf-protection=none
- intel x86-64 수많은 예외와 보호기능 있음. 적절한 옵션을 통해 이것들을 무시한다.
ex ) -fno-stack-protector, -fcf-protection=none, -mno-red-zone
-c : 소스파일을 오브젝트 파일로 생성. 링킹단계는 수행하지 않기 때문에 실행파일이 생성되지는 않는다.
→ 명령어 확장자가 .o인 오브젝트 파일 생성
동적분석 - 메모리, 변수값, 스택, 레지스터 상태 알 수 있음.
프로그램 실행 O
$gcc -c -Og ex.c -fcf-protection=none
$(gdb) ex.o
(gdb) disas func
→ 명령어 단위로 register와 memory 상태 추적 low-level debugging
3. Disassembly
$gcc -c -Og ex.c -fcf-protection=none
$objdump -d ex.c
.o 오브젝트 파일
링킹 과정 포함. 컴파일된 바이너리 코드가 실제로 어떤 기계어로 처리되었는지 확인 가능
Assembly-code syntax
ATT format AT&T
→ 리눅스. macOS
$objdump -d ex.o
Intel format
→ 윈도우
$objdump -M intel -d ex.o
//-M intel of att
Difference between att and intel format
operand order : mov src dst (att 소스먼저) mov dst src(intel 목적지 먼저)
register prefixes : 레지스터 이름 앞 %
memory access notation : att 는 (), intel 은 []
→ binary value는 att든 intel이든 같다!!
'Major S-T-U-D-Y > System Programming' 카테고리의 다른 글
9. Derived type - array, pointer, and structure (1) (1) | 2024.11.12 |
---|---|
8. Procedure Call and Stack (0) | 2024.11.12 |
GNU make (1) | 2024.10.18 |
GNU vi 주요 명령어 정리 (0) | 2024.10.03 |
1. A Tour of Computer Systems (1) (0) | 2024.09.13 |