📌 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
✔️ 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
✔️ 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
✔️ Byte Addresses : 단어의 메모리 주소는 4의 배수여야 한다.
📌 Addressing Objects : Endianess and Alignment
📎 Example : Little endian / Big endian
📌 Loading and Storing Bytes
lb $t0, 2($s3) // load byte(8 bits) from memory
sb $t0, 6($s3) // store byte from memory
📎 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
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?