CPU 15 5 What is a Processor Abstraction
CPU (1/5) 5 What is a Processor? Abstraction Seungjae Baek
CPU (2/5) 6 Various Viewpoints of Processor ü Transistor + Gate + Logic + Clock ü ALU (Arithmetic Logic Unit) + CU (Control Unit) + Registers + BUS ü Instruction Set Architecture (CISC, RISC, VLIW, EPIC, …) ü Performance Characteristics (Pipeline, Superscalar, Cache, …) Seungjae Baek
CPU (3/5) 7 Instruction Set Architecture: Register + Instructions 0 x. FF address data registers processor instructions and data memory ü ü ü Register model Memory model Instruction model 0 x 00. . 00 Seungjae Baek
CPU (4/5) 8 Performance Characteristics: Pipeline, Superscalar, Cache Ifet Dec Dfet Exe Res For efficient pipeline • Similar latency of instructions (not complex) • Branch prediction, Out-of order executions • Conflict between I. fetch and D. fetch • L 1, L 2 cache … Seungjae Baek
CPU (5/5) 9 Performance Characteristics: Pipeline, Superscalar, Cache 8086 Pentium 각 component는 이후 자세히 다룸 Seungjae Baek
Register Model (2/3) 11 레지스터 기능 ü 범용 레지스터 § § ü Segment 레지스터 § § ü EAX(accumulator) – it is favored by the CPU for arithmetic operations. EBX(base) – it can hold the address of a procedure or variable. ECX(counter) – it acts as a counter for repeating or looping instructions EDX(data) – it has a special role in multiply and divide operations. CS(code segment) – it holds the base location of all executable instructions. DS(data segment) – it is the default base location for variables. SS(stack segment) – it contains the base location of the stack. ES(extra segment) – it is an additional base location for memory variables. 인덱스 레지스터 § EBP(base pointer) – it contains an assumed offset from the SS register. It is used by a subroutine to locate variables that were passed on the stack by a calling program. § ESP(stack pointer) – it contains the offset of the top of the stack. § ESI(source index) – it takes its name from the string movement instructions, in which the source string is pointed to by the SI register § EDI(destination index) – it acts as the destination for string movement instruction. ü ü EIP : Instruction Pointer (PC: Program Counter) EFLAGS : Control and Status Register rax, rbx, rip, … for Intel 64 Seungjae Baek
Register Model (3/3) 12 EFLAGS Register: Control and Status Flag 이 외에도 IA CPU에는 CR 0, CR 2, CR 3, IDTR, GDTR, 디버깅 레지스터, FPU 레지스터, MMX 레지스터 등 다양한 레지스터가 존재 Seungjae Baek
Memory Model (1/4) 13 메모리 주소 추상화 ü ü ü logical address (virtual address) linear address physical address logical address segmentation linear address paging physical address Seungjae Baek
Memory Model (2/4) 14 Segmentation on IA ü ü ü Real Address Model: 8086 compatible, 1 MB 지원 (seg. <<4 + offset) Flat Model: protected mode with segment descriptor Segmented Model: protected mode with segment descriptor table real address model segmented model 태스크의 메모리 구조와 연관성을 파악할 수 있기를. . Seungjae Baek
Memory Model (3/4) 15 Paging on IA ü 32 bit: 2 -level paging 사용 § Page directory, page table § Disable일 경우에는 linear와 physical address가 동일 (CR 0) ü 64 bit: 4 -level paging 사용 § PML 4, page directory pointer, page directory, page table 32 bit CPU 64 bit CPU (Source: Intel 64 and IA 32 Architectures SW Developer’s Manual, Volume 3: System Programming Guide) 아이디어는 inode의 index block 관리 방법과 유사 Seungjae Baek
Memory Model (Optional) (4/4) 16 Paging and Segmentation Seungjae Baek
Instruction Model (1/2) 17 Instruction format here: movl 0 x 8049388, %eax addl 0 x 8049384, %eax movl %eax, 0 x 804946 c (Source: Intel 64 and IA 32 Architectures SW Developer’s Manual, Volume 3: Basic Architecture) Seungjae Baek
Instruction Model (2/2) 18 Instruction Set (Opcode Set) summary ü General Purpose § § § ü Data Transfer Instruction: MOV, CMOVNZ, XCHG, PUSH, POP Arithmetic Instruction: ADD, SUB, MUL, DIV, DEC, INC, CMP Logical Instruction: AND, OR, XOR, NOT Shift and Rotate Instruction: SAR, SAL, ROR, ROL Bit and Byte Instruction: BT, BTS, BTC Control Transfer Instruction: JMP, JE, JZ, JNE, LOOP Function related Instruction: CALL, RET, LEAVE String Instructions: MOVS, CMPS, LODS Flag Control Instruction: STC, CLC, STD, CLD, STI, CLI Segmented Register Instruction: LDS, LES Miscellaneous: INT, NOP, CPUID Special Purpose § § FPU Instruction: FLD, FST, FADD, FSUB, FCOM SIMD Instruction (MMX) : MOVD, MOVQ, PADD, PSUB SSE Instruction: MOVSS, ADDSS System Instruction: LGDT, SGDT, LIDT 각 명령어는 IA-32 Basic Architecture 참조 Seungjae Baek
Instruction Detail (1/12) 19 Data Transfer Instruction ü gcc –S를 사용 (참고: gcc 3. 4. 6 버전으로 만듬, gcc 4. * 버전을 사용 하면 난독화 때문에 차이 있음, assembly를 배우는 목적에는 3. 4. 6 이 더 좋음) 주석문: # or /* */ 사용 operand : reg, mem, literal reg: %로 시작 memory: alphanumeric literal: $으로 시작 movl 2, a 하면 어떻게 될까요? gcc –S –O 3 move_exam. c 를 하면 어떻게 될까요? Seungjae Baek
Instruction Detail (2/12) 20 Data Transfer Instruction (cont’) 기본 명령(mov)에 [l|w|b] suffix 가능 l: Long (4 bytes) w: Word (2 bytes) b: Byte (1 byte) Default는 long MASM과 AT&T 어셈블리 언어 차이점 1. MASM에는 [l, w, b]가 없음. 그 대신 byte ptr, word ptr, dword ptr 사용. 2. MASM에는 $, % 없음. 메모리는 [] 사용 3. MASM에서 두개의 operands를 사용 할 경우 source와 destination의 위치 가 바뀜 (eg. add eax, dword ptr [a]) 4. MASM에서는 lcall, lret 대신 call far, ret far 사용 5. Operand addressing: [] vs () 6. 그 외 (변환, 변수 주소 지정, 다중 section)) Seungjae Baek
Instruction Detail (3/12) 21 Arithmetic Instruction 물론 subl b, %eax 가능 mul은 operand와 eax를 곱함. 그 결과가 edx, eax에 저장 (div는 edx와 eax 값을 operand 로 나눔. 그 결과 몫은 eax, 나머 지는 edx에 저장) b=0 x 40000001; 로 초기화 되면 ? Seungjae Baek
Instruction Detail (5/12) 23 Control Transfer Instruction: for while, do while 반복문은 for 반복문의 다른 형태 Seungjae Baek
Instruction Detail (6/12) 24 Control Transfer Instruction: invoked function stack frame for main 222 111 ret. address EBP saved ebp a ESP b EBP를 ESP에 복사. 그리고 pop. 결국 지역 변수와 saved ebp를 stack에서 꺼냄 EIP에 ret. address를 pop. 결국 호출 함수로 제어 전달. ESP를 감소시키고 operand를 stack에 넣는다. (cf. movl $222, 4(%esp) ) EIP를 stack에 넣고 operand로 분기. 스택의 인자들을 pop 함수 리턴 값은 eax에. Seungjae Baek
Instruction Detail (7/12) 25 function example ü result = sum(final_number) 함수를 어셈블리 언어로 작성해 보자. . text directive: text 영역 임을 선언, 다음 명령들을 text section에 추가 . global directive: asm_sum을 linker에서 볼 수 있게 선언 메모리 지정 방법: displacement(base) 또는 displacement(base, index, scale) Seungjae Baek
Instruction Detail (8/12) 26 function example 수행 결과 여러 파일이 있을 경우 make가 효과적. Seungjae Baek
Instruction Detail (9/12) 27 Standalone assembly program . data directive: 다음 명령들을 data section에 추가. long directive: a 주소에 long 변수 초기화. (0 x 10이나 10+2 등도 가능) . string directive: arg 주소에 스트 링 초기화. Seungjae Baek
Instruction Detail (10/12) 28 directive ü ü ü 어셈블러에게 정보 제공 (프로그램 직접 수행과는 관련 없음). 으로 시작 대표적인 directive § § § § . file, . include. text, . data, . comm, . section. long, . byte, . string, . ascii, . float, . quad. global, . align, . size. set, . equal, . rept, . space. macro, . endm. if, . else, . endif … GNU assembler 내용 참조. 또는 쉘에서 “info as” Seungjae Baek
Instruction Detail (11/12) 29 Software Interrupt ü write() 함수 호출 system call의 인자 system call의 index IDT table의 index Seungjae Baek
Instruction Detail (12/12) 30 Software Interrupt (cont’) ü 인터럽트 처리 및 시스템 호출 서비스 Kernel IDT sys_call_table (sysent[]) 0 x 0 divide_error() 0 sys_no_syscall() debug() 1 sys_exit() nmi() 2 sys_fork() 3 sys_read () 4 system_call() sys_write () …. 0 x 80 system_call() 47 …. sys_fork() sys_write() sys_getpid() …. 255 sys_no_syscall() Seungjae Baek
부록 32 Soft. ICE in Windows 2000 Seungjae Baek
부록 33 Trace 32 Seungjae Baek
- Slides: 33