I-Type
- Immediate type이라고도 불림
- 3개의 operands를 수반함:
- rs, rt: register operands(각각 source, target의 의미)
- imm.: 16-bit two's complement(2의 보수) immediate
- Other fields:
- op: the opcode
- Simplicity favors regularity: all instructions have opcode
- Operation is completely determined by opcode(6 bit)
I-Type Examples
assembly code와 field value의 register 위치가 다름에 유의해야 한다.(destination의 위치가 code로 나타낼 때는 맨 처음이다.)
J-Type
- Jump-type
- 26-bit address operand(addr)
- Used for jump instructions(j)
Review: Instruction Formats
Power of the Stored Program
- 32-bit instructions & data stored in memory
- Sequence of instructions: only difference between two applications
- To run a new program:
- No rewiring required
- Simply store new program in memory
- Program Execution:
- Processor fetches (reads) instructions from memory in sequence
- Processor performs the specified operation
The Stored Program
Program Counter가 실행할 instruction을 차례로 읽으면서 fetch하는 형식이다.
Interpreting Machine Code
- Start with opcode: tells how to parse rest
- If opcode all 0's
- R-Type instruction
- Function bits tell operation
- Otherwise
- opcode tells operation (see the table appendix B.1 ~ B.3)
MIPS opcodes
Programming
- High-level languages:
- ex) C, Java, Python
- Written at higher level of abstraction
- Common high-level software constructs:
- if/else statements
- for loops
- while loops
- arrays
- function calls
Logical Instructions(논리 명령어)
- and, or, xor, nor
- and: useful for masking bits
- Masking all but the least significant byte of a value:
- 0xF234012F AND 0x000000FF = 0x0000002F
- or: useful for combining bit fields
- Combine 0xF2340000 with 0x000012BC:
- 0xF2340000 OR 0x000012BC = 0xF23412BC
- nor: useful for inverting bits:
- andi, ori, xori
- 16-bit immediate is zero-extended (not sign-extended)
- nori not needed
각 example 참고
Shift Instructions
5가 들어가는 위치는 shamt이기 때문에 register가 들어가는 자리가 하나가 남는데 그 자리는 그냥 쓰지 않는 자리이다.
- sll: shift left logical
- Example: sll $t0, $t1, 5 # $t0 <= $t1 << 5
- srl: shift right logical
- Example: srl $t0, $t1, 5 # $t0 <= $t1 >> 5
- sra: shift right arithmetic
- Example: sra $t0, $t1, 5 # $t0 <= $t1 >>> 5
shift right 할 때는 sign extension으로 앞 부분의 새로 만들어지는 bit를 채운다.
Variable Shift Instructions
- sllv: shift left logical variable
- Example: sllv $t0, $t1, $t2 # $t0 <= $t1 << $t2
- srlv: shift right logical variable
- Example: srlv $t0, $t1, $t2 # $t0 <= $t1 >> $t2
- srav: shift right arithmetic variable
- Example: srav $t0, $t1, $t2 # $t0 <= $t1 >>> $t2
Shift Instructions
Generating Constants
16-bit constant using addi:
32-bit constants using load upper immediate(lui) and ori:
- 32 bit인 경우 16bit의 제약 조건에서 사용할 수 없기 때문에 두 개로 나눠서 사용한다.
- C Code
- int a = 0xFEDC8765; //32bit
Multiplication, Division
- Special register: lo, hi
- 32 x 32 multiplication, 64 bit result
- mult $s0, $s1
- Result in {hi,lo}
- 32-bit division, 32-bit quotient, remainder
- div $s0, $s1
- Quotient(몫) in lo
- Remainder(나머지) in hi
- Moves from lo/hi special registers
- mflo $s2
- $s2 <- $lo : lo(몫, 32bit)으로부터 데이터를 가져와 $2에 저장한다.
- mfhi $s3
Branching
- Execute instructions out of sequence(순서를 벗어난; 즉 점프)
- Types of branches:
- Conditional
- branch if equal (beq)
- branch if not equal (bne)
- Unconditional
- Jump (j)
- jump register (jr) : register로 jump해라
- jump and link (jal)
Review: The Stored Program
PC가 가리킨 곳부터 시작
Conditional Branching (beq)
MIPS assembly
Labels indicate instruction location. They can't be reserved words and must be followed by colon (:)
- sll는 x2의 효과가 있기 때문에 2만큼 shift left를 하면 x2 (2) 이기 때문에 $s1에는 4가 저장된다.
The Branch Not Taken (bne)
Unconditional Branching(j)
Unconditional Branching(jr)
jr은 R-type instruction이다.
High-level Code Constructs
- if statements
- if/else statements
- while loops
- for loops
If Statement
Assembly tests opposite case (i != j) of high-level code(i == j)
high level 언어에서는 ==이면 안의 구문을 실행하는 것이니까 assembly 언어에서는 !=이면 그 다음을 생략 하고 L1으로 점프하게 한 것이다.
If/Else Statement
While Loops
Assembly tests for the opposite case (pow == 128) of the C code (pow != 128).
For Loops
- initialization: executes before the loop begins
- condition: is tested at the beginning of each iteration
- loop operation: executes at the end of each iteration
- statement: executes each time the condition is met
Less Then Comparison
$t1=1 if i<101
$t1=0 else if i > 101
그래서 slt와 beq를 합쳐서 bgt를 사용하면 이를 알아서 slt+beq로 컴파일 해 준다.
- beq에서는 101보다 작은지 큰지에 대한 정보인 t1의 정보와 0을 비교하여 loop를 빠져나가는 것이다.