MIPS Instructions #6

charrrming·2022년 4월 10일

Computer Architecture

목록 보기
6/17

1. Why Branch?

연속적으로 명령어를 실행하기 위해선, pc를 4 증가시켜야 함.
(MIPS에서 각 명령어의 크기가 4바이트니까)

분기 명령어는 pc를 수정해서 코드를 건너뛰거나 반복한다.
- conditional (테스트를 통과하면 분기) / unconditional (항상 분기)

2. Branch Instructions in MIPS

Conditional branch (조건 분기, I-format)

	- beq (branch if equal)
    - bne (branch if not equal)
    
    I-format: beq(bne) rs, rt, label
    
    bne $s0, $s1, skip	// 같지 않으면 go to "skip" 
    beq $s0, $s1, skip	// 같으면 go to "skip"
    skip: add $t0, $t1, $t2
    
    skip을 어셈블러가 계산해서 immdeiate 필드에 넣어줌 
    -> branch 목적지 주소를 어떻게 계산?
    
    beq, bne는 16비트 상수 필드를 가진 I-format 명령어
    분기 명령어는 상수 필드를 offset으로 사용, offset은 PC와 관련 있음
    
    • branch 목적어 주소 계산 방법
    - pc는 fetch cycle 동안 pc를 pc+4로 업데이트 함 
      (그래서 다음 명령어의 주소를 hold)
    - 어셈블러가 목적지가 pc+4로부터 얼마나 떨어져있는지 계산해서 
      명령어 단위(4byte)로 imm 필드에 넣어줌
	- branch 거리는 -2^15 ~ 2^15 -1로 제한
    - 따라서 destination = (pc+4) + (imm << 2) 
      명령어 단위니까 곱하기 4 해줘야 byte 단위가 된다!
• 조건 설정을 도와주는 4가지 명령어 (slt, sltu, slti, sltiu)

	R) slt rd, rs, rt		// set on less than (rs < rt 이면 rd=1, 아니면 rd=0)
    R) sltu rd, rs, rt		// set on less than unsigned
    I) slti rt, rs, imm	// set on less than immediate
    I) sltiu rt, rs, imm	// set on less than unsigned immediate
    
    slt $t0, $s0, $s1
    sltiu $t0, %s0, 25
• Branch Pseudo Instructions 
- 수도 명령어 사용 시 어셈블러는 $at 이라는 예약된 레지스터를 사용

	blt, ble, bgt, bge는 signed number를 위한 수도 명령어
    bltu, bleu, bgtu, bgeu는 unsigned number를 위한 수도 명령어
    
    blt $s1, $s2, Label == less than
    
    slt $at, $s1, $s2
    bne $at, $zero, Label 랑 같은 뜻임
    
    ble: less than or equal to
    bgt: greater than
    bge: great than or equal to

blt (branch less than)

blt RS, RT, Label

slt $at, RS, RT
bne $at, $zero, Label

ble (branch less than or equal)

ble RS, RT, Label

slt $at, RS, RT
beq $at, $zero, Label

bgt (branch greater than)

bgt RS, RT, Label

slt $at, RT, RS
bne $at, $zero, Label

bge (branch greater than or eqaul)

bge RS, RT, Label

slt $at, RT, RS
beq $at, $zero, Label

Unconditional branch (무조건 분기)

	J) j (jump)
    J) jal (jump and link)
    R) jr (jump register)
    
    jal, jr은 함수 호출 한다!

	j target 	// jump (J-format)
    jal target	// jump and link (J-format)
    jr rs	// jump register (R-format)
    
    j LLL // j -> opcode(6비트), LLL -> jump target(26비트)
    		 pc+4를 포함한 256MB 블록 안의 어디로든지 점프 가능!
             
    destination = pc+4의 상위 4비트 / jump target 26비트 / 명령어 단위니까 <<2 하는 2비트
				-> 총 32비트

3. Branching Far Away

만약 beq의 16비트 상수 필드보다 더 멀리 분기하고 싶을 땐?
-> 어셈블러가 (무조건 분기) j를 집어넣는다!

0개의 댓글