학부 수업/컴퓨터구조

4. MIPS의 명령어 구조와 명령어들

백지오 2020. 10. 12. 19:46
반응형

MIPS는 R, I, J의 세 가지 명령어 구조를 갖는다.

R-구조 명령어

op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
  • op: operation code (opcode)
  • rs: first source register number
  • rt: second source register number
  • rd: destination register number
  • shamt: shift amount (00000 for default)
  • funct: function code (extends opcode)

add 명령어가 대표적인 R-구조 명령어이다.

add rs rt rd shamt funct
32 17 18 8 0 0 for now
100000 10001 10010 01000 00000 00000

add 명령어는 1000001000110010010000000000000 으로 번역된다.

MIPS의 논리 연산

MIPS의 bitwise 연산자는 다음과 같다.

Operation C MIPS
Shift left << sll
Shift right >> srl
Bitwise AND & and, andi
Bitwise OR | or, ori
Bitwise NOT ~ nor

Shift 연산은 R-형식으로, shamt가 Shift를 얼마나 진행할지 나타내는 역할을 한다.

MIPS에서의 NOT 연산은 zero와의 NOR 연산을 통해 할 수 있다.

nor $t0, $t1, $zero # $zero는 언제나 0을 담고 있다.

I-구조 명령어

op rs rt constance or address
6 bits 5 bits 5 bits 16 bits
  • rt: destination or source register number
  • constance: $-2^{15} \text{to} + 2^{15} - 1$
  • address: offset added to base address in rs

I-구조는 직접연산이나 load/store 연산에 사용된다.

J-구조 명령어

op address
6 bits 26 bits

J-구조인 j(jump) 명령어는 unconditional branch 명령어로, address로 이동하는 역할을 한다.

Branch 명령어

beq와 bnq는 conditional branch 명령어이다.

beq/bnq rs rt rd 구조를 갖는데, beq는 rs와 rt가 같을 때, bnq는 다를 때 rd로 이동한다.

slt 명령어는 rd, rs, rt를 받고, 아래 c 코드와 같은 연산을 수행한다.

if (rs < rt) rd = 1; else rd = 0;

slti 명령어를 활용하여, rt 대신 상수를 쓸 수도 있다. slt, slti와 유사하지만 unsigned인 값의 비교를 위해 사용하는 sltu, sltui 명령어도 있다.

한편, j는 조건 없이 address로 이동한다.

Branch 예시

C 코드

if (a==b) f=g+h;
else f=g-h;

MIPS 코드

bnq $s3, $s4, Else # Else의 주소는 어셈블러가 계산
add $s0, $s1, $s2
j Exit #jump to exit
Else: sub $s0, $s1, $s2
Exit: ..

C 코드

while (save[i] == k) i+=1; // i는 $s3, k는 $s5, save 배열은 $s6에 저장

MIPS 코드

Loop:	sll  $t1, $s3, 2    # t1에 i를 왼쪽으로 2bit shift 하여 저장 (2bit shift == x4)
        add  $t1, $t1, $s6  # t1에 save + i*4 를 저장 (즉, save[i]의 주소)
        lw   $t0, 0($t1)    # t0에 save[i]값 저장
        bne $t0, $s5, Exit # save[i]!=k 면 break
        addi $s3, $s3, 1    # i+=1
        j    Loop
Exit:

 

반응형