Computing Integer Division
// C code
int a = 12;
int b = 4;
int result = 0;
main(){
while (a >= b){
a = a - b;
result++;
}
}
// MIPS Assembly Language
.data
x: .word 12
y: .word 4
res: .word 0
.global main
.text
main: la $s0, x // load_address x in $s0 (memory --> register)
lw $s1, 0($s0) // load_word $s0 in $s1
lw $s2, 4($s0) // load_word ($s0 + 4) in $s2
lw $s3, 8($s0) // load_word ($s0 + 8) in $s3
while: bgt $s2, $s1, endwhile // if($s2 > $s1), endwhile
sub $s1, $s1, $s2 // x = x - y;
addi $s3, $s3, 1 // result++
j while // jump to while:
endwhile: // 끝내기
la $s0, x
sw $s1, 0($s0) // register --> memory
sw $s2, 4($s0)
sw $s3, 8($s0)
Q. la $s0, x
.text의 가장 처음에 x의 주소를 $s0에 저장했고, $s0는 중간에 register들의 연산에서 값이 변하지 않은 것 같은데, sw를 하기 전에 왜 다시 주소를 $s0에 저장해야 하나요?
A. $s0가 사용되지 않았고 바뀐 것이 없어서, la $s0, x 를 하지 않아도 상관없습니다. 그냥 수업 상 갑자기 $s0가 등장하면 상기하기 어려울 수 있어서 그렇게 쓴 것뿐입니다.
Simple One
// C code
int a = 12;
int b = 4;
int result = 0;
main(){
while(a >= b) {
a = a - b;
result++;
}
printf("%d%d%d, a, b, res);
}
// MIPS Assembly Language
# $t0 = a
# $t1 = b
# $tr = res
.text
main:
li $t0, 12
li $t1, 4
li $t2, 0
while: bgt $t1, $t0, endwhile # if($t1 > $t0), endwhile
sub $t0, $t0, $t1 # a = a - b
addi $t3, $t3, 1 # res++;
j while
endwhile:
move $a0, $t0 # move from $t0 to $a0
li $v0, 1
syscall # syscall 1: print_int from $a0
move $a0, $t1
li $v0, 1
syscall
move $a0, $t2
li $v0, 1
syscall
li $v0, 10
syscall # syscall 10: Exit
More Conditional Operations
: Set result to 1 if a condition is true. Otherwise, set to 0.
slt rd, rs, rt // if(rs < rt), rd = 1; else, rd = 0;
slti rt, rs, constant // if(rs < constant), rt = 1; else, rt = 0;
▶ Use in combination with beq, bne
slt $t0, $s1, $s2 // if ($s1 < $s2), $t0 = 1; else, $t0 = 0;
bne $t0, $zero, L // if ($t0 != 0), branch to L
// case 1) $s1 < $s2, $t0 = 1, L // if ($s1 < $s2), branch to L
// case 2) $s1 >= $s2, $t0 = 0
// bgt로 바꾸기
bgt $s2, $s1, L // if($s2 > $s1), branch to L
Branch Instruction Design
▶ Why not blt, bge, etc?
: Hardware for <, <=, ... slower than =, !=.
- Combining with branch involves more work per instruction, requiring a slower clock.
Jump Instructions
j L // jump to label L
jr src1 // jump to location src1
jal L // jump to label L, and store the address of the next instruction in $ra
jalr src1 // jump to location src1, and stroe the address of the next instruction in $ra
Comparision Instructions
- native instruction: slt
// slt
slt rd, rs, rt // if(rs < rt), rd = 1; else, rd = 0;

Signed vs. Unsigned
- Signed comparison: slt, slti
- Unsigned comparision: sltu, sltui
--> Signed/Unsigned: instruction에 의해 결정된다. (레지스터 값에 의해 결정x)
The Address Mode

Subroutine (Procedure, Function, Method)
: Logical division of the code that may be regarded as a self-contained operation.
- A subroutine might be executed several times with different data as the program executes.
Callers and Callees
- Subroutine call: Main routine이 Subroutine을 call한다.
- Caller: Main routine
- Callee: Subroutine
--> A return from a subroutine is when a subroutine passes control back to its Caller.
Procedure Calling
- Place parameters in registers.
- Transfer control to procedure.
- Acquire storage for procedure.
- Perform procedure's operations.
- Place result in register for caller.
- Return to place of call. (by. $ra)
Register Usage

Procedure Call Instructions
▶ Procedure call: jal (jump and link)
jal ProcedureLabel
// Address of following instruction put in $ra
// Jumps to target address(ProcedureLabel)
▶ Procedure return: jr (jump register)
jr $ra
// PC = $ra(PC + 4)
The jal Instruction
jal ProcedureLabel
- $ra ($31): Provides the hardware support necessary to elegantly implement subroutines.
- Jumps to target address (ProcedureLabel).
jal sub
// $ra <- PC + 4
// $ra <- address 8 bytes away from jal (PC + 4: 자동, PC + 4: 수동 --> PC + 8)
// PC <- sub
// load the PC with the subroutine entry point

SBY 4-2
1. Can you convert the following MIPS code to the MIPS code with beq and bne?
bgt $s1, $s2, my_label
: if ($s1 > $s2), branch to my_label
- beq: branch if equal
- bne: branch if not equal
- slt rd, rs, rt: if(rs<rt), rd = 1; else, rd = 0;
slt $t0, $s2, $s1 // if($s2 < $s1), $t0 = 1; else, $t0 = 0;
bnq $t0, $zero, my_label // if($t0 != 0), branch to my_label
// if ($t0 == 1) 즉, ($s2 < $s1), branch to my_label
'Computer Architecture > 컴퓨터구조[01]' 카테고리의 다른 글
| [컴퓨터구조] 3. Arithmetic for Computers (1) (0) | 2023.10.09 |
|---|---|
| [컴퓨터구조] 2. Instructions: Language of the Computer (6) (0) | 2023.10.08 |
| [컴퓨터구조] 2. Instructions: Language of the Computer (4) (1) | 2023.09.25 |
| [컴퓨터구조] 2. Instructions: Language of the Computer (3) (0) | 2023.09.25 |
| [컴퓨터구조] 2. Instructions: Language of the Computer (2) (0) | 2023.09.18 |