본문 바로가기
Computer Architecture/컴퓨터구조[01]

[컴퓨터구조] 2. Instructions: Language of the Computer (4)

by leziwn.cs 2023. 9. 25.
Representing Instructions

▶ Instructions are encodede in binary. (machine code)
▶ MIPS instructions: Encoded as 32-bit instruction words.
 
▶ Register numbers
: Each register has its unique number. 
--> Binary number --> Hexadecimal number

Register numbers

 

MIPS R-format Instructions

R-format

  • add  rd, rs, rt
  • sub  rd, rs, rt
  • rs: source 1, rt: source 2, rd: destination
  • shamt: shift amount
  • op: operation code (opcode), funct: function code (extends opcode)

 

R-format Example

R-format Example

 

Hexadecimal

Hexadecimal

 


Logical Operations

Logical Operations

  • &, |: 비트 단위 연산
  • &&, ||: 논리 연산

 

Shift Operations

Shift Operations

▶ sll (shift left logical)
: Shift left and fill with 0 bits. (예: 00000010 --> 00000100)
--> sll by i bits = * 2^i (곱하기)
 
▶ srl (shift right logical)
: Shift right and fill with 0 bits. (예: 00000010 --> 00000001)
--> srl by i bits = / 2^i (나누기)

  • srl: unsigned only

 

AND Operations

AND Operations

 

OR Operations

OR Operations

 

NOT Operations

▶ a NOR b == NOT (a OR b)

  • nor  $t0, $t1, $zero

NOT Operations

 


Conditional Operations

: Branch to a labeled instruction if a condition is true. Otherwise, continue sequentially.

beq  rs, rt, L1   // if (rs == rt), branch to instruction labeled L1;

bne  rs, rt, L1   // if (rs != rt), branch to instruction labeled L1;

j    L1           // unconditional jump to instruction labeled L1;

 

Compiling If Statements
// C code

if (i == j)
   f = g + h;
else
   f = g - h;
   
// f, g, h, i, j in $s0, $s1, $s2, $s3, $s4
// Compiled MIPS code

      bne  $s3, $s4, Else   // if(i != j), Else
      add  $s0, $s1, $s2    // f = g + h
      j    Exit
Else: sub  $s0, $s1, $s2   // f = g - h
Exit: ...

 

Compiling Loop Statements
// C code

while (save[i] == k)   // save[i] --> sll (lw: constant만 가능)
   i += 1;

// i in $s3, k in $s5, address of save in $s6
// Compiled MIPS code

Loop: sll  $t1, $s3, 2     // $t1 = i * 2^2
      add  $t1, $t1, $s6   // $t1 = 4*i + save(base register)
      lw   $t0, 0($t1)   
      bne  $t0, $s5, Exit  // if (save[i] != k), Exit
      addi $s3, $s3, 1     // i += 1
      j    Loop            // uncoditional jump to Loop
Exit:...

lw $t0, 0($t1)을 하지 않고 $t1과 k를 비교하면 안되니?

lw $t0, 0($t1) 명령을 생략하고 $t1과 k를 직접 비교하는 대신에 $t1과 k를 직접 비교할 수도 있지만, 이렇게 하려면 몇 가지 고려해야 할 사항이 있습니다.

  1. 메모리 액세스: lw $t0, 0($t1)를 생략하고 $t1과 k를 직접 비교하면, 메모리에서 데이터를 로드하는 추가적인 명령이 필요하지 않습니다. 이로 인해 프로그램의 실행 속도가 향상될 수 있습니다. 그러나 메모리 액세스를 줄이려면 $t1과 k의 비교 결과를 어딘가에 저장해야 하므로 다른 레지스터 또는 메모리 위치를 사용해야 할 수도 있습니다.
  2. 가독성과 유지보수성: 코드를 작성할 때 가독성과 유지보수성은 중요한 고려 사항입니다. 코드가 길어지고 복잡해질수록, 변수와 데이터의 처리를 명확하게 표현하는 것이 중요합니다. lw $t0, 0($t1)를 사용하면 코드가 더 명확하게 표현됩니다. 따라서 코드를 이해하고 유지보수하는 데 도움이 됩니다.
  3. 재사용성: lw $t0, 0($t1)을 사용하면 $t0에 데이터를 로드하고 나중에 코드에서 $t0를 다른 목적으로 재사용할 수 있습니다. 데이터를 비교한 후에도 $t0를 사용하여 다른 작업을 수행할 수 있습니다.

결론적으로, $t1과 k를 직접 비교하는 것이 가능하지만, 코드의 가독성과 유지보수성을 고려하여 lw $t0, 0($t1)와 같이 데이터를 명시적으로 로드하는 것이 더 나은 접근 방식일 수 있습니다.


 

Branch Instructions

Branch Instructions

 

bgt and b statement

▶ bgt, src1, src2, label
: If (src1 > src2), go to <label>. Otherwise, go next.
 
▶ b, label
: Unconditional branch

  • j: Unconditional jump

 

Example 4 - Larger Number

Example 4 - Larger Number

 


SBY 4-1