새소식

반응형
CS 지식/컴퓨터구조

[컴퓨터구조] 6. R-Type, J-Type, I-Type

2023.10.10
  • -
반응형

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:
      • A NOR $0 = NOT A
  • 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:

  • C Code
// int is a 32-bit signed word 
int a = 0x4f3c;

 

  • MIPS assembly code
# $s0 = a
addi $s0, $0, 0x4f3c

 

32-bit constants using load upper immediate(lui) and ori:

  • 32 bit인 경우 16bit의 제약 조건에서 사용할 수 없기 때문에 두 개로 나눠서 사용한다.
    • lui / ori
  • C Code
    • int a = 0xFEDC8765; //32bit
int a = 0xFEDC8765; //32bit

 

  • MIPS assembly code
# $s0 = a
lui $s0, 0xFEDC
ori $s0, $s0, 0x8765

 

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

  • C Code
if (i == j)
    f = g + h;

f = f - i;

 

  • MIPS assembly code
# $s0 = f, $s1 = g, $s2 = h
# $s3 = i, $s4 = j
	bne $s3, $s4, L1
	add $s0, $s1, $s2
	
L1 sub $s0, $s0, $s3

 

 

Assembly tests opposite case (i != j) of high-level code(i == j)

high level 언어에서는 ==이면 안의 구문을 실행하는 것이니까 assembly 언어에서는 !=이면 그 다음을 생략 하고 L1으로 점프하게 한 것이다.

 

If/Else Statement

  • C Code
if (i == j)
    f = g + h;
else
    f = f - i;

 

  • MIPS assembly code
# $s0 = f, $s1 = g, $s2 = h
# $s3 = i, $s4 = j
    bne $s3, $s4, L1
    add $s0, $s1, $s2
    j done
L1: sub $s0, $s0, $s3
done:

 

While Loops

  • C Code
// determines the power
// of x such that 2x = 128
int pow = 1;
int x = 0;
while (pow != 128) {
    pow = pow * 2;
    x = x + 1;
}

 

  • MIPS assembly code
# $s0 = pow, $s1 = x
    addi $s0, $0, 1
    add $s1, $0, $0
    addi $t0, $0, 128
while: beq $s0, $t0, done
    sll $s0, $s0, 1
    addi $s1, $s1, 1
    j while
done:

 

Assembly tests for the opposite case (pow == 128) of the C code (pow != 128).

 

For Loops

for (initialization; condition; loop operation)
    statement
  • 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

 

  • High level code
// add the numbers from 0 to 9
int sum = 0;
int i;
for (i=0; i!=10; i = i+1) {
	sum = sum + i;
}

 

  • MIPS assembly code
# $s0 = i, $s1 = sum
    addi $s1, $0, 0
    add $s0, $0, $0
    addi $t0, $0, 10
for: beq $s0, $t0, done
    add $s1, $s1, $s0
    addi $s0, $s0, 1
    j for
done:

 

 

Less Then Comparison

  • C Code
// add the powers of 2 from 1 
// to 100
int sum = 0;
int i;
for (i=1; i < 101; i = i*2) {
	sum = sum + i;
}

 

  • MIPS assebly code
# $s0 = i, $s1 = sum
    addi $s1, $0, 0
    addi $s0, $0, 1
    addi $t0, $0, 101
loop: slt $t1, $s0, $t0
      beq $t1, $0, done
      add $s1, $s1, $s0
      sll $s0, $s0, 1
      j loop
done:

 

$t1=1 if i<101

$t1=0 else if i > 101

그래서 slt와 beq를 합쳐서 bgt를 사용하면 이를 알아서 slt+beq로 컴파일 해 준다.

  • beq에서는 101보다 작은지 큰지에 대한 정보인 t1의 정보와 0을 비교하여 loop를 빠져나가는 것이다.
반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.