[ 주요 ]
1. Instruction Set Architecture (ISA)
-1. Instruction Set Architecture
-2. Instruction Set
2. MIPS ISA Basics
-0. MIPS ISA Key Idea
-1. Registers
-2. Arithmetic
-3. Data transfer
3. Procedures in MIPS ISA
4. Translating and Starting a Program
5. Examples
1. Instruction Set Architecture (ISA)
-1. Instruction Set Architecture
ISA : 프로세서 등 HW를 직접 컨트롤할 수 있게 하는 정보로, Register 등에 대한 정보, Instruction 등을 포함한다. 컴퓨터의 abstract model의 부분. 소프트웨어가 CPU를 어떻게 컨트롤할지를 정한다. programmer's view of processors. 하드웨어와 프로그래머가 어떻게 지시하고 수행할지에 대한 규약. 레지스터의 개수, 상태, 명령의 형식과 내용, 메모리 모델, I/O, interrupt 등 많은 것들에 대한 규약이다.
ISA는 Processor's insturction set, Programmer accessible register within processor, Information necessary to interact with memory, How processor reacts to interrupt from the programming view point를 포함한다.
명령어, 접근가능한 레지스터, 메모리와 상호작용 위해 필요한 정보, interrupt에 대한 반응 방법 등.
-2. Instruction Set
CPU 따라 다른 ISA. 초기의 컴퓨터는 매우 간단한 Instruction set갖고 있었고, 많은 최근 컴퓨터도 마찬가지.
2. MIPS ISA Basics
-0. MIPS ISA Key Idea
MIPS를 위한 instruction set design의 목표는 하드웨어의 simpilicity를 통해 Maximize performance and minimize cost와 reduce design time(컴파일러와 HW의)을 하는 것이다!!!!
MIPS 디자인 원리는 Simplicity favors regularity, Smaller is faster, Make the common case fast, Good design demands good compromises이다.
-1. Registers
레지스터는 CPU 안에 있다. Temporary storage이다. 메인 메모리보다 빠르지만 비싸다.
MIPS는 32*32-bit register file을 갖고 있다. 즉, 32개의 32비트 레지스터 파일을 사용한다. 32비트의 길이를 가진 것이 32개 있다는 것. Numbered 0 to 31. 32비트(4바이트) 데이터를 "word"라고 부른다. $t0, $t1, ..., $t9 for temporary valeus, $s0, $s1, ... , $s7 for saved variables.
레지스터의 유형은 General Purpose Register와 Special Purpose Register 두 개가 있다.
General Purpose Register : For computation purpose. 계산 목적. 변수 선언 시 대부분 여기에 저장.
Special Purpose Register : PC(Program Counter), AC(Accumulator), IR(Instruction Register). 프로그래머가 접근 불가한 것도 있다.
- PC : Special register to hold address of next instruction. 주소를 저장. ***
- AC : Special register which golds the result of the computation temporarily. 계산과 관련.
- IR : Special register which holds the instruction fetched from the memory. instruction 자체를 저장. secondary memory(HDD) 등에 저장된 파일을 런하면 main memory에 있다. 이걸 CPU IR에 한 줄씩 읽어온다.***
PC와 IR 둘은 instruction과 관련있다. 아래 사진과 같이 명령어는 main memory에 저장되어 있고, CPU에 load되는데 IR에 저장된다. CPU 안에서는 IR의 명령어를 data path를 사용하여 처리한다. 이 때, PC는 다음 명령어의 address를 갖고 있다.
-2. Arithmetic
(1) Arithmetic Operations
대부분의 Arithmetic/logic insturctions는 3개의 operands를 갖고 있다.
add a, b, c #a = b+c
sub a, b, c #a = b-c
(2) Arithmetic with Register Operand
Add and subract
ex) f = (g+h) - (i+j);
# f --> $s0, g --> $s1, h --> $s2, i --> $s3, j --> $s4
add $t0, $s1, $s2. #temp t0 = g+h
add $t1, $s3, $s4 #temp t1 = i+j
sub $s0, $t0, $t1 #f = t0 - t1
(3) Arithmetic with Immediate Operand
Constant data specified in an instruction : subtraction은 없다. -1값을 더하면 된다.
ex) addi $s3, $s3, 4 #s3 = s3 + 4
ex) addi $s2, $s1, -1 #s2 = s1 + (-1)
(4) Constant Zero
레지스터 0(i.e., $zero)는 상수 0이다. overwritten 할 수 없다.
ex) add $t2, $s1, $zero #t2 =s1 + 0 --> t2 = s1
ex) addi $t2, $zero, 10 #t2 = 0 + 10 --> t2 = 10인데 그냥 addi $t2, $s1, 0해줘도 됨.
-3. Data transfer
(1) Memory Operands
Main memory는 주로 composite data를 저장한다. composite data란 Arrays, structures, dynamic data이다. int, fp 등은 primitive data이다. 더 이상 쪼개질 수 없는 데이터 형태이다. 이 primitive data를 조합해서 배열, 구조체, 동적할당 등 새로운 형태를 구성한 것이 composite data. 메인메모리는 이것을 저장한다.
Arithmetic operations를 사용하기 위해서 memory에서 registers로 값을 load해야 한다. MIPS에서는 모든 operand는 register file에 있어야만 한다. 또한 결과는 register에서 memory로 이동시켜서 저장해야 한다. arithmetic operations는 결과를 memory address에 저장할 수 없기 때문이다.
Memory is byte addressed. 메모리는 바이트식으로 저장된다. 8-bit(1 byte)가 각 address의 단위이다.
Words are aligned in memory. Word는 4byte이다. 주소는 반드시 4의 배수여야 한다.
MIPS는 Big Endian이다.
Endian은 컴퓨터가 데이터를 읽고 쓰는 방향을 정의하는 것(여러개의 연속된 대상을 1차원 공간에 배열하는 방법)이다.
Big Endian의 경우, 가장 왼쪽 바이트가 워드의 주소가 된다. 빅 엔디안은 상위 바이트를 하위 번지수에 저장한다. 인간이 보통 사용하는 방법처럼 왼쪽에서 오른쪽으로 바이트를 배열한다. 이러면 큰 단위의 바이트가 앞자리에 위치한다. MSB(Most Significant Bit)가 포함된 바이트의 주소가 워드의 주소가 된다.
Little Endian의 경우 가장 오른쪽 바이트가 워드의 주소가 된다. 상위 바이트를 상위 번지수에 저장. 오른쪽에서 왼쪽 방향으로 바이트를 배열한다. LSB(Least Significant Bit)가 포함된 바이트의 주소가 워드의 주소가 된다.
이 때, 87654321이 아닌 78563412인 이유는 최소 단위가 8bit이기 때문이다. 컴퓨터는 바이트 단위로 메모리를 관리. 그리고 하나의 16진수를 표현하는 데에는 4bit가 필요. 1byte = 8bit = 4bit*2이기 때문에 두자리 단위로 정렬한 결과가 나온다. 최소 단위 내부의 순서는 바뀌지 않는다.
리틀엔디안의 장점은 1111 1111 0000 0000일 때 앞의 두자리만 읽으면 값을 얻을 수 있다는 것이다. 만약, 같은 수가 빅엔디안이라면 0000 0000 1111 1111이라 네자리를 읽어와야 한다.
(2) Data Transfer Operations *****
Load word
lw $s1, 20($s2) #$s1 = Memory[$s2 + 20]
Offset : 20. $s2에 저장된 주소에서의 거리. 이 때 a[2]라면 8($t0)같이 써줘야 한다. *4해줘야 함. 한 단위가 워드. 워드는 4byte.
Base register : $s2. 기준 메모리 주소 담고 있다.
Store word
sw $s1, 20($s2) #Memory[$s2 + 20] = $s1
Offset : 20 / Base register : $s2
Load Word Example *******
ex) g = h + a[8] #g --> $s1, h --> $s2, base address of a --> $s3
lw $t0, 32($s3) #load word
add $s1, $s2, $t0
Store Word Example
ex) a[12] = h + a[8] #h --> $s2, base address of a --> $s3
lw $t0, 32($s3) #load word
add $t0, $s2, $t0
sw $t0, 48($s3) #store word
(3) Byte/Halfword Operations
Byte는 1byte. Halfword는 2byte. C언어에서 primitive data의 최소 단위가 1바이트이므로 바이트는 굉장히 유용하다.
부호가 있는 바이트를 로드해올 때는 Sign extension을 쓴다. 제일 앞이 0, 즉 양수이면 부족한 자릿수를 다 0으로 채워주고, 제일 앞이 1, 즉 음수이면 부족한 자릿수를 다 1로 채워주는 것이다.
부호가 없는 바이트를 로드해올 때는 Zero extension을 쓴다. 무조건 부족한 자릿수를 다 0으로 채워준다.
Load Byte / Load Halfword
lb rt, offset(rs) / lh rt, offset(rs) # Sign extend to 32bits in rt
Load Byte Unsigned / Load Halfword Unsigned
lbu rt, offset(rs) / lhu rt, offset(rs). #Zero extend to 32bits in rt
Store Byte / Store Halfword
sb rt, offset(rs) / sh rt, offset(rs)
(4) Registers vs. Memory
레지스터가 메모리보다 접근하기 빠르다.
기본적으로 모든 데이터는 메모리에 저장되지만 데이터를 사용하기 위해서는 load해서 레지스터로 옮기고, 또 그 결과를 store해야 한다.
이 과정에서 명령어들이 사용된다. 컴파일러는 가능한한 레지스터를 변수를 위해 사용해야 한다. 레지스터에 옮겨진 데이터는 최대한 길게 남아져야 한다. 계속 메모리와 레지스터를 왔다갔다 하는 빈도가 늘어날수록 더 많은 명령어가 사용되고, 오래걸리기 때문이다. 그러나 레지스터 공간은 한계가 있기 때문에 다시 메모리에 값을 store해야 한다. 이러한 과정을 register spiling이라고 한다.
이 때, 적게 사용되는 variable들만 메모리에 spill해야 한다. 컴파일러는 적게 사용하는 데이터를 지정해 메모리에 store시켜야 한다. 이 결정들은 register optimization이라고 한다. 이는 매우 중요하다!!!
-4. Logical operation
-5. Conditional/Unconditional operation
-6. Number representation
-7. Instruction representation
-8. Addressing
3. Procedures in MIPS ISA
4. Translating and Starting a Program
5. Examples
[참고]
https://zion830.tistory.com/38 (리틀엔디언, 빅엔디언)