## Assignment
- [Assignment 2](../../assignments/assignment-2/)
- Translate a Python program that has both an if statement and a loop
# Arrays
## What is an array?
An array is a series of data items stored in consecutive memory locations
- Python: list
- Java: array, ArrayList
- C/C++: array, Vector
## Example array program
```mips
.data
#arrays get stored in consecutive 32-bit memory addresses
my_array: 1, 2, 3, 4
.text
la $s0, my_array #load the base address of the array
#Addresses go up by 4 because each byte has its own address
#So a word takes up 4 addresses
lw $t0, 0($s0) #load my_array[0]
lw $t1, 4($s0) #load my_array[1]
lw $t0, 8($s0) #load my_array[2]
lw $t0, 12($s0) #load my_array[3]
```
### New flavor of `lw`
```mips
lw $t1, 4($s0) #load my_array[1]
```
- Meaning
- load the 32-bit value stored at the address `$s0`+4
- Good for accessing items in an array
## Arrays in memory
```mips
.data
#arrays get stored in consecutive 32-bit memory addresses
my_array: 1, 2, 3, 4
```

### Translating a program that uses an array
```python
my_array = [10,6,0,27,92,18,42]
i = 0
while i < 7:
print(my_array[i])
i = i + 1
```
```mips
.data
my_array: 10, 6, 0, 27, 92, 18, 42
newline: .asciiz "\n"
.text
la $s0, my_array #load the base address of the array
li $t0, 0 #loop counter
loop:
slti $t1, $t0, 7 #check if loop counter < 7
beq $t1, $zero, end #if not < 7, branch to end
#print the item from the array
lw $a0, 0($s0) #load next value from array
li $v0, 1 #print integer
syscall
#print a newline
la $a0, newline
li $v0, 4 #print string
syscall
addi $t0, $t0, 1 #increment the loop counter
addi $s0, $s0, 4 #move to the next word in the array
b loop
end:
```
## Exercise
- edit the above MIPS program to also find the sum of all the items in the array
# Review
## Base 2 (AKA Binary)
$1101_\text{two}$ means
`$$(1\cdot 8)+(1\cdot 4)+(0\cdot 2)+(1\cdot 1)$$`
or
`$$(1\cdot 2^3)+(1\cdot 2^2)+(0\cdot 2^1)+(1\cdot 2^0)$$`