Before executing: guess what you think `$t0`-`$t5` will be
Execute it: were you right?
Is this how `and` and `or` behave in Python?
```mips
.text
li $s0, 0
li $s1, 1
li $s2, 2
li $s3, 3
and $t0, $s0, $s1
or $t1, $s0, $s1
andi $t2, $s1, 1
ori $t3, $s1, 0
or $t4, $s1, $s2
and $t5, $s2, $s3
```
# Binary Numbers
# CS Jokes

Source: https://www.amazon.com/Types-People-understand-Binary-T-Shirt/dp/B07PSPLSC9
## Let's talk about how counting works

How do you count if you only have two digits?
## Counting in Binary
- CPUs compute in **binary** using the contrast of low/high voltages to mean 0 and 1 - the two _binary digits_ or **bits**
- So how do we encode **numbers** in binary?
## Base 10 (AKA Decimal)
- When we write 437 we usually mean base 10 - the number system with 10 digits
- Can also write it as $437_\text{ten}$
- $437_\text{ten}$ means
`$$(4\cdot 100)+(3\cdot 10)+(7\cdot 1)$$`
or
`$$(4\cdot 10^2)+(3\cdot 10^1)+(7\cdot 10^0)$$`
## 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)$$`
### Demo: Let's convert numbers to different bases
- $110110101_\text{two}$
- $437_\text{ten}$
### Exercise: Practice with Binary
- Convert the following number into decimal:
+ $1011010_\text{two}$
- Convert the following decimal number into binary:
+ $277_\text{ten}$
### Base 16 (AKA Hexadecimal)
- Hexadecimal is base 16
- digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- 5C means
`$$(5\cdot 16)+(12\cdot 1)$$`
- Easy to convert back and forth from binary
### Counting in Binary/Hexadecimal
| Decimal | Binary | Hex |
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| Decimal | Binary | Hex |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
| 16 | 0001 0000 | 10 |
| 17 | 0001 0001 | 11 |
### Exercise: Exploring in Mars
- Open up Mars and create a `.asm` file
- Put the number 302 in your data section
- How does Mars display that in Hex?
- What is the Binary equivalent?
### Exercise: Convert back and forth
- Convert the following binary number into hex
- $\text{1111 1010 0001 1011 0100 1110 0010 0011}_\text{two}$
- Convert the following hexadecimal number into binary
- $\text{00FF33AA}_\text{hex}$
# Conditionals
## Branching
- **Branching** allows MIPS programs to skip around to different parts of the program
- Useful for
- conditional statements (if, if-else, etc.)
- loops
- continue executing at the line labeled `my_label`
```mips
b my_label
#...
my_label:
```
-
continue executing at the line labeled `my_label` if `$s0` and `$s1` are equal
```mips
beq $s0, $s1, my_label
```
-
continue executing at the line labeled `my_label` if `$s0` and `$s1` are _not_ equal
```mips
bne $s0, $s1, my_label
```
## Compiling an `if` Statement
```python
if i == j:
k = 1
print("The value of k is",k)
```
```mips
.data
i: 5
j: 8
k: 0
message: .asciiz "The value of k is "
.text
lw $s0, i
lw $s1, j
bne $s0, $s1, disp_msg
li $s2, 1
sw $s2, k
disp_msg:
li $v0, 4 #4 is the code for printing a string
la $a0, message #the argument for the syscall
syscall
li $v0, 1 #1 is the code for printing an int
lw $a0, k #the argument for the syscall
syscall
```
## Exercise
- Run the above program in Mars, observe the output
- Change it so `i` and `j` are equal, run again
- Translate the following Python program into MIPS
```python
if i == j:
k = 1
else:
k = 2
print("The value of k is",k)
```