Slide 4

누디·2022년 10월 23일
0

Computer Architecture

목록 보기
1/5

Slide 4

📌 Assembling Branches and Jumps

✔️ beq, bne (conditional branch)

beq $s1, $s2, L1 //$s1과 $s2의 값이 같으면 L1으로
 -> opcode = 4
bne $s1, $s2, L1 //$s1과 $s2의 값이 다르면 L1으로
 -> opcode = 5

Untitled

✔️ Jump (unconditional branch)

j Lb1 //Lb1으로 go
-> opcode = 2 

✔️ Branch

conditional branch 에서는 분기해야 할 offset이 16 bits
	beq $s0, $s1, L1

근데 16bits로 표현하지 못하는 큰 수면(너무 멀면)
	bne $s0, $s1, L2
	j L1 // jump instruction은 26 bits
L2:

📎 Compiling While Loops

while (i != k)
	i = i + j; // i는 $s0, j는 $s1, k는 $s2에 있다.

-> assembly code
Loop: beq $s0, $s2, Exit // 순환의 끝에서 처음 명령어로 되돌아 갈 수 있도록 Loop
			add $s0, $s2, $s1  // i = i + j
			j Loop // jump Loop
Exit:

📌 More Instructions for Making Decisions

  • 같은지 다른지 비교하는 것이 가장 흔한 검사지만, 경우에 따라서 두 변수 간의 대소 비교가 필요할 때도
  • 예를 들어, for 순환문에서 인덱스 변수 값이 0보다 작은지를 검사할 때가 있다.
  • MIPS에서는 두 레지스터의 값을 비교한 후 첫 번째 레지스터 값이 두 번째 레지스터의 값보다 작으면 세 번째 레지스터의 값을 1 아니면 0으로 하는 명령어로 이런 일을 처리
  • 이 명령어가 slt(set on less)

✔️ slt (set less than)

slt $t0, $s0, $s1

-> if $s0 < $s1
	  then $t0 = 1
		else $t0 = 0

✔️ slti (set on less than immediate) → 레지스터와 특정 상수 비교

slti $t0, $s0, 10

-> if $s0 < 10
		then $t0 = 1
		else $t0 = 0

✔️ Other Branch Instructions

분기 관한 instructions
slt, beq, bne로 만들어낼 수 있다.

blt $s1, $s2, Lb1 // less than, s1이 s2보다 작으면 Lb1으로
->	slt $at, $s1, $s2 // s1 < s2이면 at =1
	 	bne $at, $zero, Lb1 // at가 0이 아니면 Lb1으로
ble $s1, $s2, Lb1 // less than or equal to
bgt $s1, $s2, Lb1 // greater than
bge $s1, $s2, Lb1 // great than or equal to

📌 Another Instruction for Changing Flow

✔️ jr (jump register)

jr $t1 // go to address in $t1

✔️ Compiling a Case (Switch) Statement

switch (k) {
	case 0: h = i + j; break; //k = 0
	case 1: h = i + h; break; //k = 1
	case 2: h = i - j; break; //k = 2
};

$t4에 L0 시작주소, k는 $s2

		add $t1, $s2, $s2 //$t1 = 2*k
		add $t1, $t1, $t1 //$t1 = 4*k
		add $t1, $t1, $t4 //$t1 = add of JumpT[k]
		lw $t0, 0($tq)    //$t0 = JumpT[k]
		jr $t0            //jump based on $t0
L0: add $s3, $s0, $s1 //k = 0 so h = i + j
		j Exit
L1: add $s3,          //k = 1 so h = i + h
		j Exit
L2: sub $s3,          //k = 2 so h = i - j
Exit:

📌 MIPS Data Types

  • Bit : 0, 1
  • Bit String
    • 8 bits is a byte
    • 16 bits is a half-word
    • 32 bits is a word
    • 64 bits is a double-word
  • Character : ASCII 7 bit code
  • Decimal : digits 0-9 encoded as 0000₂ thru 1001₂ two decimal digits packed per 8 bit (byte)
  • Integers : 2’s complement
  • Floating Point

✔️ Byte Addresses : 단어의 메모리 주소는 4의 배수여야 한다.

📌 Addressing Objects : Endianess and Alignment

📎 Example : Little endian / Big endian

  • Little Endian : leftmost byte is word address
  • Big Endian : rightmost byte is word address

📌 Loading and Storing Bytes

lb $t0, 2($s3) // load byte(8 bits) from memory
sb $t0, 6($s3) // store byte from memory

스크린샷 2022-06-10 오전 12.04.06.png

📎 Example of Loading and Storing Bytes - under big endian

add $s3, $zero, $zero
lb $t0, 1($s3) //$s3를 base register로 사용한 다음 1byte 이동해서 해당하는 byte 값을 $t0의 가장 오른 쪽에 1 byte 값 저장
sb $t0, 6($s3) //$t0의 가장 오른쪽에 있는 값을 해당 base register offset을 사용해서 memory 내에 저장

✔️ Loading and Storing Half Words

lh $t0, 2($s3) //load half word(16 bits) from memory
sh $t0, 6($s3) //store half word to memory

📌 Shift Operations

  • 8비트 문자를 32비트 단어로 포장 및 개봉하는 작업 필요
sll $t2, $s0, 8 //$t2 = $s0 << 8 bits
srl $t2, $s0, 8 //$t2 = $s >> 8bits

✔️ More Shift Operations

📌 Compiling Another While Loop

📌 Logical Operations

✔️ Logic Operations

📌 How About Larger Constants?

0개의 댓글