07 MoreAssemblyProgramming
07 MoreAssemblyProgramming
(Part 2)
1 / 37
Outline
Arrays
For-each loop
Switch statement
2 / 37
Reading a string from the user
3 / 37
Outline
Arrays
For-each loop
Switch statement
4 / 37
Control structures in assembly
5 / 37
Conditional branching
# Basic instructions
beq $t1, $t2, label # if ($t1 == $t2) goto label
bne $t1, $t2, label # if ($t1 != $t2) goto label
# Macro instructions
beqz $t1, label # if ($t1 == 0) goto label
bnez $t1, label # if ($t1 != 0) goto label
6 / 37
Outline
Arrays
For-each loop
Switch statement
7 / 37
If-then-else statement
Structure of an if-then-else statement
if (condition) {
then-block (execute if condition is true)
} else {
else-block (execute if condition is false)
}
8 / 37
If-then-else statement
Example
# Pseudocode:
# if (a < b + 3)
# a = a + 1
# else
# a = a + 2
# b = b + a
# Register mappings:
# a: $t0, b: $t1
9 / 37
If-then statement
10 / 37
If-then statement
11 / 37
Outline
Arrays
For-each loop
Switch statement
12 / 37
Do-while loop
13 / 37
Do-while loop
Example
# Pseudocode:
# do {
# a = a + 3
# } while (a < b*2);
# Register mappings:
# a: $t0, b: $t1
14 / 37
While loop
15 / 37
While loop
16 / 37
While loop
17 / 37
While loop
# Pseudocode: while (a <= c + 4) { a = a + 3 }
# b = b + a
# Registers: a: $t0, b: $t1, c: $t2
18 / 37
For loop
19 / 37
For loop
20 / 37
Exercise
# Pseudocode:
# sum = 0
# for (i = 0; i < n; i++) {
# sum = sum + i
# }
# Registers: n: $t0, i: $t1, sum: $t2
21 / 37
Break and continue
22 / 37
Translation of continue in for-loop
23 / 37
Translation of conditional break/continue
Common pattern: break/continue guarded by if-statement
• E.g. if (condition) break
# Pseudocode:
# while (true) {
# ...
# if (a < b) break
# ...
# }
# Register mappings: a = $t0, b = $t1
24 / 37
Translation of conditional break/continue
25 / 37
Indefinite loops
26 / 37
Exercise
# Pseudocode:
# total = 0
# for (i = 0; i < n; i++) {
# if (i % 5 > 2) continue
# total += i
# }
# Registers: total = $t0, i = $t1, n = $t2
# Note: rem $t3, $t1, 5 ==> $t3 = $t1 % 5
li $t1, 0 # (init) i = 0
loop: bge $t1, $t2, end # while (i < n)
rem $t3, $t1, 5 # tmp = i % 5
bgt $t3, 2, update # if (tmp > 2) continue
add $t0, $t0, $t1 # total += i
update: addi $t1, $t1, 1 # (update) i++
j loop # (end while)
end: # ...
27 / 37
Declaring arrays in the data segment (review)
28 / 37
Element addresses
Declaration in data segment
# 10 integer array or 39 character null-terminated string
array: .space 40
If we interpret as integers . . .
• array, array+4, array+8, array+12, . . . , array+36
• lw to move an integer from array (in memory) to a register
29 / 37
Basic addressing mode
All other data memory addressing modes are translated to this form!
30 / 37
Pseudo-addressing modes
31 / 37
For-each loop (arrays only)
32 / 37
For-each loop – enumerating the elements
.data
.text
li $t1, 0 # i = 0
loop: ... # (loop condition, TODO)
lw $t0, fibs($t1) # fib = fibs[i]
... # (loop body)
addi $t1, $t1, 4 # i++ <= +4
j loop # (end loop)
33 / 37
For-each loop – enumerating the elements
.data
.text
34 / 37
Switch statements
• n is an integer variable
• each k is an integer constant
• each k-block is a sequence of statements
• often ends in break
Execution rules
• if value of k=n, execute corresponding k-block
• keep executing subsequent blocks until break
• if no such k, execute default-block
35 / 37
Switch statements
Translation strategy
1. in text segment, implement and label each k-block and
the default-block, in order of switch statement
2. in data segment, declare array of addresses (jump table)
• in array at position i, label of case-block for i=k
• for “gaps” in cases, give label for default case
3. translate switch statement into an array lookup
• check bounds of n and jump to default case if out
• if in range, translate n to corresponding index (e.g. n*4)
4. use jr to jump to the address from array lookup
36 / 37
Switch statements
37 / 37