Loops
Loop 1 (increment)
for(int i = 12; i<34; i++){
// do A
}
// do B
addi $t0, $0, 12
addi $t1, $0, 34
L1:
beq $t0, $t1, exit
// do A
addi $t0, $t0, 1
j L1
exit:
// do B
Loop 2 (decrement)
for(int i = 34; i>12; i--){
// do A
}
// do B
addi $t0, $0, 34
addi $t1, $0, 12
L1:
beq $t0, $t1, exit
// do A
addi $t0, $t0, -1
j L1
exit:
// do B
Loop 3 (while loop blocking)
while(a == b){
// do A
}
// do B
L1:
bne $t0, $t1, exit
// do A
exit:
// do B
Loop 4: execute body `counter` times
# while loop body executed 100 times (counter)
i = 0;
j = 0;
counter = 100;
do{
i += 4;
j += 8;
counter--;
}while(counter != 0)
// finally
# t0 (counter), t1 (i), t2(j)
addi $t0, $0, 100
addi $t1, $0, 0
addi $t2, $0, 0
L1:
// do something, starting with i = 0, j = 0
addi $t1, $t1, 4
addi $t2, $t2, 8
addi $t0, $t0, -1
bne $t0, $0, L1
// finally
Loop 5: 2D array
# a0: array address, a1: #rows, a2: #columns
# loop j*i times
# t0 = j*i, t1 = starts from a0 (then will go up in 4's
mult $a1, $a2
mflo $t0
addi $t1, $a0, 0
L1:
# load element
lw $t2, 0($t1)
# t2 now contains the element under consideration
addi $t1, $t1, 4
addi $t0, $t0, -1
bne $t0, $0, L1
Conditionals
Conditional 1
if(a == b){
// do A
}
// do B
# say $t0 = a, $t1 = b
bne $t0, $t1, B
A:
// do A
B:
// do B
Conditional 2
if(a != b){
// do A
}
// do B
# say $t0 = a, $t1 = b
beq $t0, $t1, B
A:
// do A
B:
// do B
Memory access
Access 1: Access variable X, given its address:
# Given X address ($t0), access X and put it into $t2 or $f12
# Case 1: X is integer
lw $t2, 0($t0)
# Case 2: X is single precision float
lwc1 $f12, 0($t0)
# Case 3: X is double precision float (little endian)
lwc1 $f12, 0($t0)
lwc1 $f13, 4($t0)
# Case 3-a: X is double precision float (big endian)
lwc1 $f12, 4($t0)
lwc1 $f13, 0($t0)
Access 2: Access W[i], given address of W
# Given W ($t0), i ($t1), access W[i] and store it to $t2 or $f12 (if float)
# Here, we are effectively using $t2 as a temporary var
# Case 1: W is integer
addi $t2, $t2, 4
mult $t1, $t2
mflo $t2
add $t2, $t0, $t2 # t2 = 4*i + W
lw $t2, 0($t2) # t2 = W[i]
# Case 2: W is single precision float
addi $t2, $t2, 8
mult $t1, $t2
mflo $t2
add $t2, $t0, $t2 # t2 = 4*8 + W
lwc1 $f12, 0($t2)
# Case 3: W is double precision float (little endian)
addi $t2, $t2, 8
mult $t1, $t2
mflo $t2
add $t2, $t0, $t2 # t2 = 4*8 + W
lwc1 $f12, 0($t2)
lwc1 $f13, 4($t2)
Function
Function 1: Declare v0 function(a0, a1)
# Function 1: Declare `v0 function(a0, a1)`
# where a0, a1 are parameters, and v0 is return value (set to 5)
Func1:
add $t0, $0, $a0
add $t1, $0, $a1
# Do stuff
addi $v0, $0, 5
jr $ra
Calling Function 1, not caring about overriden $ra
main:
add $a0, $t0, $0
add $a1, $t1, $0
jal Func1
# now v0 is populated with 5
Function 2: using stack pointer to pass >4 variables to a function as argument.
Refer to Lecture 4-6A, slide 38
Appendices
Appendix 1: Main CPU registers alias
Appendix 2: Crossing register values between processors
Appendix 3: defining literal values
Appendix 4: Conversion (fd = destination register, fs = source register)
Appendix 5: Register Conventions
Appendix 6: compare operations
Compare floating point then jump
note: bc1t is used like `bc1t <label>`