본문 바로가기
CS/3-1 컴구

[컴퓨터구조] #6. MIPS 명령어(6)

by 이지이즤 2022. 3. 26.

 

Lec 6. MIPS Instructions_6 (Language of the Computer)

 

- Why Branch?
  : non-sequential flow, condition에 따른 decision 가능, 함수 호출/리턴 가능, loop (if-else, case, for, while)
    -> 조건에 따라, 순차적으로 갈 지/분기할 지 결정할 수 있음.

    명령어의 길이가 4byte 이므로, 순차적으로 진행할 때는 PC값이 4씩 증가함.
    하지만 branch를 하면 PC가 skip over sections of code 하거나 go back to repeat the previous code 함.

- Branch 의 종류
  Conditional branch : branch only if the test is true (beq, bne)
  Unconditional branch : always branch (j, jal, jr)

- beq(branch if equal), bne(branch if not equal)
  : I-format instruction, conditional branch
   beq(bne) rs, rt, label
   -> rs와 rt가 같으면 특정 레이블로 분기. 뺄셈해서 0인지 확인해서 비교함.

- Branch Destination Address
  : beq와 bne는 I-type이므로 16bit immediate를 가짐.
    branch 명령어는 immediate field를 offest(점프할 거리)로 사용함. 
    이때 offset명령어(word) 단위임!!!
    그리고 immediate field가 16bit 이므로 -2^15 ~ (2^15-1)개의 명령어만큼 branch가능. 

    근데 PC byte 단위임!!! 그래서 imm도 word->byte 단위로 바꿔주자.
     destination = (PC+4) + (imm << 2

 

- bne Example

   고수준언어랑 조건 반대로 씀. ('같으면' 실행 <-> '다르면' 실행 안함)

 


 

- In Support of Branch
  : help you set the conditions
    slt, slti : for signed numbers
    sltu, sltiu : for unsigned numbers

    slt rd, rs, rt // set on less than (R format) : rs가 rt보다 작으면 rd를 1로 세팅, 그렇지 않으면 0으로 세팅
    sltu rd, rs, rt // set on less than unsigned (R format) 
    slti rt, rs, imm // set on less than immediate (I format)
    sltiu rt, rs, imm // set on less than unsigned immediate (I format) 



 

- Branch Pseudo Instructions
    blt, ble, bgt, bge : for signed numbers
    bltu, bleu, bgtu : for unsigned numbers
    -> assembler (reserved register $at 사용)
    -> slt, slti, beq, bne (진짜 명령어)

    blt $s1, $s2, Label  // less than 
    ble $s1, $s2, Label  // less than or equal to
    bgt $s1, $s2, Label  // greater than
    bge $s1, $s2, Label  // greater than or equal to

    ex) blt $s1, $s2, label (s1<s2 이면 label로 분기)
         -> slt $at, $s1, $s2 (s1<s2 이면 set $at to 1)
         -> bne $at, $zero, Label (s1<s2 이면 0이 아니므로 Label로 분기)

 


 

- Bounds Check Shortcut
  : signed number 인 x0<=x<y 인지 빠르게 알 수 있는 방법!!!
    -> x를 unsigned 라고 가정하고 x<y 인지만 체크하면 됨!!
 
  증명)
  0 <= x < y 에서 0은 MSB가 0이고 y는 양수니까 MSB가 0이다.
  x를 unsigned라고 가정하고 x < y 라는 것을 알아냈다고 한다면,
  y는 MSB가 0인데 x < y 이니까 x의 MSB도 무조건 0이다.
  x의 MSB가 0이므로 0 <= x 이다.
  따라서 x를 unsigned라고 가정하고 x < y 인 것만 확인하면 자동으로 0 <= x 이다.

  만약 x 가 음수였다면 MSB=1이고 unsigned 취급했을 때 MSB=0인 y와 비교하면
  x > y 가 되므로 0 <= x < y 는 성립하지 않는다.


  

 


 

 

- j, jr, jal
  : unconditional branch (beq, bne 등의 conditional branch 방법보다 더 멀리 점프 가능)
    j target // jump (J format)
    jal target // jump and link (J format)
    jr rs // jump register (R format)


   PC+4 가 포함되어있는 임의의 256MB 안에서 어디로든 점프 가능!

- Branching Far Away
  : conditional branch (beq, bne) 는 명령어가 2^15 범위 내에 있을 때만 분기 가능(2^15 * 4byte),
    unconditional branch (j, jal, jr) 은 2^28 byte, 즉 256MB 만큼 점프 가능



 


 

 

- While in C


- for in C


- Comparision in C

 

댓글