컴퓨터구조론-2(Language of the Computer)

dragonappear·2021년 3월 15일
0

Computer architecture

목록 보기
2/4

Instructions : Language of the computer

  • Instructions(32bit):
    - 기계어의 단어이다.
    - MIPS = opcode(6bits) + operand specifiers(26bits)로 이루어져있다.
    - OPcode의 6bit는 총 64개의 그룹(operation)을 표현할수있다.
  • Instruction set:
    - 컴퓨터가 이해할수있는 명령어의 집합
  1. Arithmatic Operations:

    • +,-,*,/,%
    • 산술연산의 Opcode 는 0000000 이다.
    • CPU에서 3개의 레지스터를 사용한다.
    • add a(one destination), b,c(two sources)
    • 모든 산술연산은 위 형태를 지닌다.

    • Register Operands:
      - 산술 명령어는 레지스터 연산자를 사용한다
      - MIPS는 CPU 안에 32개의 레지스터를 사용하고, 각 레지스터에 저장되는 명령어의 비트는 32bit이다.
      -각 레지스터의 index는 0 부터 31 까지이다.
      -t0,t0,t1, ... ,$t7 for temporary values and s0,s0,s1, ... ,$s7 for saved varialbes --> 총 18개의 레지스터는 Compiler가 사용하는 레지스터
      - 레지스터의 주소를 표현하는 비트는 5비트이다. 레지스터는 총 32개이기때문이다.
      - Smaller is faster => 레지스터가 많아질수록 레지스터 주소 비트수는 늘어난다 -> 주소찾는데 오래걸린다.
  2. Memory Operands:
    Main memory used for composite data
    Load values from memory into registers(load word)
    Store result from register to memory(store word)
    Memory is byte addressed
    레지스터는 메모리보다 접근이 빠르다.
    컴파일러는 가능한한 레지스터의 변수를 사용해야한다.
    spill to memory: 자주사용하지않는 변수들은 메모리에 저장해놓고 필요할때 메모리에서 가져온다.

C code:
g= h + A[8];
g in $s1, h in $s2 , base address of A in $s3

Compiled code:

lw $t0, 32($s3) # load word
add $s1 , $s2,$t0
C code:
A[12] = h + A[8];
h in $s2 , base address of A in $s3

Compiled code:
lw $t0, 32($s3) # load word
add $t0 , $s2,$t0
sw $t0, 48($s3) # store word
  1. Immediate Operands:
    Immediate operands avoids a load instruction
addi $s3 , $s3, 4 

The Constant zero:
- 32개의 레지스터 중 가장 왼쪽 레지스터에는 0만 저장되어있다. ($zero)

add $t2 , $s1 $zero // 0은 항상 자주 쓰이므로 레지스터에 박아놓았다.

Representing Instuctions:

  • 명령어들은 기계어로 인코딩된디.

  • MIPS 명령어들은 32비트 워드로 인코딩된다.

  • Register numbers: 총 32개이므로, 5비트로 표현가능하다.
    - t0 t0~t7 : 8~15

    • t8 t8~t9 : 45~25
    • s1 s1~s7 : 16~23

MIPS R-format instructions:

  • 레지스터를 3개 사용하는 명령어들

  • opcode: fucnt와 함께 명령어의 종류를 나타낸다.
  • rs = 첫번째 오퍼랜드(피연산자) 값이 들어가있는 레지스터의 주소를 담고 있는 부분
  • rt = 두번째 오퍼랜드(피연산자) 값이 들어가있는 레지스터의 주소를 담고 있는 부분
  • rd = 두개의 피연산자의 값을 연산하여 얻은 결과를 저장하기 위한 레지스터의 주소를 담고 있는 부분이다
  • shamt: shift 명령어에서 shift 양을 나타내는 부분이다(다른 명령어에서는 항상 0이다.)
  • funct = function code(opcode 코드와 함께 명령어의 종류를 나타낸다.)

R-format example

Shift operations:

rs는 사용X

MIPS I-format instructions:

  • 레지스터 2개 사용하는 명령어들(immediate arithmetic and load/store instructions)

  • lw,sw(메모리에 접근하는 명령어) + imm(자주 쓰는 상수를 명령어에 넣어서 레지스터에 접근없이 바로 연산하는 명령어)

  • 16비트로 만들수 있는 상수가 들어갈 수 있다.

  • 직접적으로 명령어에 상수가 들어가있으면 레지스터를 엑세스 하지 않아도 되니 명령어 처리 속도가 더 빨라진다.

  • I 타입이 쓰이는 가장 중요한 이유: 레지스터가 아닌 메모리에 접근하여 어떤 값을 가져오거나 저장할 때 반드시 필요하다

  • 즉, 직접적으로 주소값(=상수)이 immediate 부분에 와야한다.

  • 하지만 16비트만 가지고 메모리 주소를 다 표현할수없다

  • 여기서 사용하는 방식이 바로 displace addressing(변위 주소 지정) 방식이다.

  • rs가 가리키는 레지스터는 4바이트이기 때문에 4기가 바이트의 모든 주소에 접근할 수 가 있다.
    그렇기 때문에 rs가 가리키는 레지스터의 값(=주소)을 기준으로 해서 immediate의 16비트로 표현할 수 있는 주소만큼 증가하거나 감소하여 변화를 줄수있다.

여기서 16($r18)이 의미하는 것은 $r18에 있는 주소값을 기준으로 16바이트를 더한 주소 값을 말한다.

Condotional Operations:

Compiling If statements:

More Condotional Operations:

Compiling Loop Statements:

Addressing Mode Summary:

Branch Addressing:

  • beq, bne

  • opcode, two registers, target address

  • Most branch targets are near branch
    - forward or backward

Jump Addressing:

  • Jump(j and jal) targets could be anywhere in text segment
opaddress
6bits26bits
  • Direct jump addressing:
    - target address = PC31...28 + (address * 4) + '00'

Target Addressing Example:

Branching Far Away:

  • If branch target is too far to encode with 16-bit offset, assembler rewrites the code

ex:

beq $s0,$s1,L1
   // eotlsdp
bne $s0,$s1,L2
j L1

L2:
   

L1: // 16비트를 벗어날정도로 멀리있을때

Procedure calling:

왜 레지스터의 개수가 32개인지 학습한다.

Procedure Call Instructions

  • Procedure call: jump and link
    jal ProcedureLabel
    - address of following intruction put in $ra
    - junps to target address
  • Procedure return: jump register
    jr $ra
    - copies $ra to program counter
    - can also be used for computed jumps. ex) case/switch statements

Leaf Procedure Example

C code:

int leaf_example(int g,int h,int i,int j){
int f;
f= (g+h)-(i+j);
return f;
}
  • Arguments g,...,j in $a0 , ... , $a3
  • f in $s0(hence, need to save $s0 on stack)
  • Result in $v0

0개의 댓글