Computers/Computer Architecture

lecture3. Machine instructions

emzei 2014. 1. 29. 15:47


REPRESENTING INSTRUCTIONS IN THE COMPUTER

★MIPS fields 

- 타입에 따라 bit 나눠쓰는게 다르다

- 한 명령어당 32bit를 필요로 하기 때문에 32bit를 분할하여 씀

- 어떻게 변환되는지가 중요하다!


▶R-type(Register Type)



* op : basic operation of the instruction (opcode)

* rs : the first register source operand

* rt : the second register source operand

* rd : the register destination operand / it gets the result of the operation

* shamt : shift amount

* funct(function) : this field selects the specific variant of the operation in the op field, and is sometimes called the function codecf. opcode extension

** 쓰지 않는 field는 default value = 0

** rt,rs,rd ~ 5 bits 이유 :

 레지스터에는 32bit의 메모리가 있지! 0~31을 표현할 때 5bit면 되지!



▶I-type(Immediate Type)




> Comparison table


Instruction

Format

op

rs

rt

rd

shamt

funct

address

 R

add

R

0

reg

reg

reg

0

32

n.a

sub

R

0

reg

reg

reg

0

34

n.a

 I

addi

I

8

reg

reg

n.a

n.a

n.a

constant

lw

I

35

reg

reg

n.a

n.a

n.a

address

sw

I

43

reg

reg

n.a

n.a

n.a

address


* funct : R-type 명령어 구분 

* op : I-type 명령어 구분

* constant : 16bit 표현 가능 범위 : 

unsigned ; 0 ~ (2^16) -1

signed ; -(2^15) ~ (2^15)-1


* ex. A[300] = h+A[300];

lw     $t01200($t1)       // temp reg. $t0 gets A[300]

add  $t0,  $s2,  $t0         // temp reg. $t0 gets h+A[300]

sw    $t0, 1200($t1)       // stores h+A[300] back into A[300]


??? A[300]에 접근하는데 offset은 1200인 이유는 워드단위가 4바잇뜨인지라 4배를 해줘야 정상적인 접근이!!!

!!! 위 예제에서 

lw/sw 명령에서의 $t0 : source

add 명령에서의 $t0 : destination




LOGICAL OPERATIONS


Operation

C

Java

MIPS

shift left

<<

<<

sll

shift right

>>

>>

srl, sra

bit-by-bit AND

&

&

and, andi

bit-by-bit OR

|

|

or, ori

bit-by-bit NOT

~

~

nor


shift left/ shift right ~ 시프트 하고 자리에서 벗어나는 bit는 빠빠이!!! cf. 로테이션


Shift Operation (★ emzei's difficulty)

EX) a b c d (a,b,c,d는 각각의 비트자리)

1비트만 이동한다하자!


1. Logical shift

logical shift right : 0 a b c

logical shift left   : b c d 0

2. Arithmetic shift (for 2's complement)

Arithmetic shift right : a a b c

Arithmetic shift left   : a b c 0

3. Circular shift (=rotate)

Circular shift right : d a b c

Circular shift left   : b c d a


** shift right ~ ÷2

** shift left   ~ ×2


MIPS Shift Instructions

 sll (shift left logical)

 srl (shift right logical) 

: a b c d >> 1 = 0 a b c

: unsigned인 경우에 쓴다 

ex. unsigned 1110 sra -> 1111 (값이 절반이 되질 않아!!! 이건 잘못됨!!!)

signed 1110 srl -> 0111 ( -2 ☞ -1 )

▶ sra (shift right arithmetic)

: a b c  d >> 1 = a a b c

: signed인 경우에 쓴다

: 부호비트를 신경쓴다



INSTRUCTIONS FOR MAKING DECISIONS

* beq reg1, reg2 , L1 // branch if reg1 == reg2

* bne reg1, reg2 , L2 // branch if reg1  != reg2

(beq, bne - I Type)


Loops

ex) while(save[i] == k) i +=1;


ans)

Loop :s l l   $t1,  $s3, 2     // temp reg $t1 = 4*i

add  $t1,  $t1, $s6  // $t1 = address of save[i]

l w    $t0,  0($t1)     // temp reg $t0 = save[i]

bne  $t0, $s5, Exit   // go to Exit if save[i] != k

addi $s3, $s3, 1      // i = i + 1

j        Loop               // go to Loop

Exit:


slt(set on less than) Instruction

slt  $t0, $s3, $s4    // if($s3 < $s4) than $t0 = 1

 //                      else  $t0 = 0

slti $t0, $s2, 10      // if $s2<10  ~$t0 = 1 ; else $t0 = 0


※Pseudo instruction

blt  $s0, $s1, Less    // branch on less than


Other Jump Instruction

jr (jump register) instruction

jr   $t0    // jump to the address specified in a register ($t0)

    // i.e. PC ← $t0

jal (jump-and-link) instruction

jal  ProcedureAddress     // $ra ← PC + 4

// PC ← ProcedureAddress

return from procedure

jr $ra // ra ; return address




supplement 

spilling register

- argument의 개수

> 4개 이하 : 레지스터 $a0 ~ $a3 에 저장하여 사용

> 4개 초과 : 레지스터에 저장하지 못하는 나머지는 stack에 저장 후 불러 씀 

//인자가 4개가 넘어가면 비효율적이다



'Computers > Computer Architecture' 카테고리의 다른 글

lecture 5. History of ISA  (0) 2014.01.29
lecture 4. Addressing Modes  (0) 2014.01.29
lecture2. MIPS Instruction  (0) 2014.01.29