Machine Programming 1: Basics
Index
Index in books
3.1 A Historical Perspective 202p
3.2 Program Encodings 205p
3.3 Data Formats 213p
3.4 Accessing Information 215p
3.5 Arithmetic and Logical Operations 227p
Index in PPT
History of Intel processors and architecture
C, assembly, machine code
Assembly Basics: Resigisters, operands, move
Airthmetic & logical operation
포스팅은 PPT 인덱스를 따르며 강의 메모 겸 핵심만 간단하게 정리했습니다.
History of Intel processors and architecture
Intel x86 Processors
Dominate laptop/desktop/server market
- CISC I(Complex Instruction Set Computer)
X86-64 processor
- 32bit에서 64bit 로의 확장된 버전. 컴퓨터 시스템이 직접 사용 가능한 주소인 메모리의 양을 획기적으로 증가시켰다. (Transition to a 64-bit architecture from the previous 32-bit x86 architecture.)
- general-purpose register의 수가 16개로 증가했으며 각 레지스터는 64bits를 사용한다. 이전 버전인 x86(32bits)는 8개의 레지스터, 그리고 32bits를 사용하였다.
- 이는 컴퓨팅 성능과 메모리 핸들링에 있어 크게 향상된 것이다.
C, assembly, machine code
정의
- Architecture: 어셈블리어/기계어를 이해하고 사용하려면 알아야 하는 프로세서 디자인의 일부. ex) ISA(instruction set specification), register
- Microarchitecture: 아키텍쳐의 구현 ex) cache size, corer frequency
- Code Forms
- Assembly code : 기계어의 문자 표현
- Machine code: 프로세서가 실행하는 byte-level의 프로그램
- 머신 코드는 실행 중인 실제 바이트와 어셈블리 레벨 버전을 모두 통합합니다 machine code incorporates both the actual bytes that are operated executing as well as the assembly level version of it
Assembly / Machine code view
Register file
많이 사용되는 프로그램 데이터
PC (Program Counter)
다음 명령어의 주소를 가리킴.
x86-64에서는 RIP 이라고 불린다 (%rip)
Condition Codes
가장 최근에 행해진 산술 / 논리 연산의 상태 정보를 저장함.
Memory
Byte 주소로 불러낼 수 있는 배열array (=바이트로 된 주소를 갖고 있는 배열)
code와 user data
프로시져를 지원하는 stack이 존재
Turning C into Object code / Compiling into Aseembly
gcc –Og p1.c p2.c -o p
-Og : Opimization debug
type | programs | by |
text | C program ( .c ) | |
text | Asm program ( .s ) | by compiler |
binary | Object program ( .o ) | by Assembler |
binary | Executable program ( p ) | by Linker |
- non-shark machines(Andrew, Linux, Mac, OS-‐X,) 에서는 다른 gcc와 다른 컴파일러 세팅을 사용하므로 명령어 입력 다른 결과를 얻을 수 있음.
Assembly Characteristics
Data type
- 정수(Integer) 데이터 타입은 1, 2, 4, 8 bytes 를 사용하며 데이터 값과 주소(untyped pointers)에 사용된다.
- **부동소수점(Floating point)**는 4, 8, 10 bytes를 사용한다.
- 코드는 일련의 명령어를 담고 있는 byte sequence 이다.
- 배열이나 구조체 같은 집합 타입(aggregate types)은 존재하지 않는다. 그저 메모리상에 연속적으로 할당된 byte일 뿐이다.
Operation 연산
- 산술 연산 함수를 레지스터나 메모리 데이터 위에서 실행한다.
- 메모리와 레지스터 사이에서 데이터가 이동한다.
- 메모리에서 데이터를 **로드(load)**하여 레지스터에 넣는다.
- 레지스터의 데이터를 메모리에 **저장(store)**한다.
- control을 전송한다.
- 프로시저로/프로시저에서의 비조건적 jump (Unconditional Jump)
- 조건 분기(Conditional Branches)
기타 machine code 관련 정보
- Object Code
- Machine Instruction (example)
- Disassembler
- 기계어인 Object code 를 text형태인 assembly code로 변환하는 것.
- Alternate Disassembly
- What can be disassembled?
Assembly Basics: Registers, operand, move
Register
레지스터의 종류
name | meaning | function | 비고 |
%rsp | stack pointer | stack의 top을 가리키는 포인터 | 스택 포인터로만 사용됨. (불변) |
%rax | return value를 담는 레지스터 | 현재 진행 중인 stack frame에서 return 할 value를 담는 데 사용된다. | |
%rbp | base pointer | callee가 저장됨 | optinal 하다 |
%rdi | dest index | 함수의 첫번째 인자를 담음 | |
%rsi | src index | 함수의 두번째 인자를 담음 |
접두사 | 용량 |
%r | 64 bits |
%e | 32 bits |
% | 16 bits |
- 과거 IA32시절엔 8개의 레지스터가 존재했고 각각 특정 용도로 사용되었는데 64bit로 발전하며 레지스터의 갯수도 증가했다. 지금은 그 흔적이 이름에 남아있는 레지스터들이 8개, 숫자가 들어간 이름을 가진 레지스터가 8개씩 남아 있다.
- 특정 명령어에서 특별하게 사용될 수 있다.
IA32 : history
Movind Data : [movq]
movq src, des
#**movq의 연산 조합
src dest src, dest
imm reg movq $0x4, %rax
imm mem movq $-147, (%rax)
reg reg
reg mem
mem reg
...**
- 하나의 명령어를 사용하여 memory에서 memory로 전송할 수 없다.
연산 타입
- Immediate : 상수 정수 데이터
- 접두사 ‘$’ 를 가짐.
- 1,2, 4byte로 인코딩된다.
- Register : 16개의 정수 레지스터중의 하나
- %rsp 는 스택 포인터로만 쓰인다.
- Memory: 레지스터에 의해 주어진 주소에 있는 연속된 8 bytes.
- simple memory addresssing modes
- exmaple of simple addressing modes
- understanding swap()
- complete Memory Addressing Modes
Arithmetic & logical operation
C컴파일러는 계산(computation)을 수행하기 위해 서로 다른 명령어의 조합을 알아낸다.
Address Computation Instruction : [leaq]
leaq Src, Dst
Some arithmetic operation
'공부기록 > CS' 카테고리의 다른 글
[TIL][CS 기초] CPU vs GPU | SSD, HDD (0) | 2024.03.11 |
---|---|
[TIL][CS 기초] 32 Bit vs 64 Bit | 2's complement, 1's complement | 실수 표현법 (0) | 2024.03.07 |
[TIL][Fri][CSAPP] ch 6. The memory hierachy (0) | 2023.11.10 |
[TIL][CS기초] Compile | Compiler, Interpreter | Type System | Typing :: Static & Dynamic (0) | 2023.11.01 |
[TIL][CS 기초] Memory structure : Stack, Heap | Garbage Collect (0) | 2023.10.23 |