# MIPS Machine Code --- CS 130 // 2025-09-24 # Review ## Two's Complement - Most computers use **two's complement** to encode signed integer values - What is it exactly? + Non-negative numbers are represented as usual
$0000$, $0011$, $0110$ + To negate a number, you do the following: 1. Flip all the bits: $0\rightarrow 1$ and $1\rightarrow 0$ 2. Add 1 to the result + $0000$, $1101$, $1010$ ## Exercise ### Two's Complement Practice - Convert each of the following numbers to decimal: --- - $1111\\;1111\\;1111\\;1111\\;1111\\;1111\\;1111\\;1111$ - $1111\\;1111\\;1111\\;1111\\;1111\\;1111\\;1110\\;0101$ - $0000\\;0000\\;0000\\;0000\\;0000\\;0000\\;0000\\;0101$ ## Demo: Adding two binary numbers * Let's use some 8-bit binary numbers
0010 0011 0001 1000 + 1111 0100 - 0011 0110 ------------ ------------
## Exercise * Using 8-bit binary numbers, show how to compute - 68 - 55 - 20 + 40 - 30 - 127 - -10 + -3 # MIPS Machine Code ## MIPS Machine Code - Each MIPS instruction is encoded in 32-bits + `add` `$t0`, `$s1`, `$s2` + 000000 10001 10010 01000 00000 100000 ---  ## R-Type Instructions  --- 1. `op` (6 bits): Opcode 2. `rs` (5 bits): First operand register 3. `rt` (5 bits): Second operand register 4. `rd` (5 bits): Destination register (result) 5. `shamt` (5 bits): Shift amount 6. `funct` (6 bits): Function code ## I-Type Instructions  --- 1. `op` (6 bits): Opcode 2. `rs` (5 bits): First operand register 3. `rt` (5 bits): Second operand register 4. `data` (16 bits): Constant or address ## Exercise - Use Mars to figure out how each of the following is represented in bits (convert from hex!). ```mips add $t2, $t1, $t0 sub $t2, $t1, $t0 addi $t0, $t0, 3 ``` - What are the opcodes for `add`, `sub`, and `addi`? ## R-Type VS I-Type - Why do we need the I-type? Why not just implement `addi`, `lw`, and `sw` using the R-type format? + Allows us to specify larger addresses and constants + $2^5 = 32$ and $2^{16} = 65536$ # Shift Instructions ## Shifting investigation
Execute it: what do `sll` and `srl` do? What happens if you shift an odd number? Are these R-Type or I-Type instructions?
```mips .text li $s0, 16 #shift left logical sll $t0, $s0, 1 sll $t1, $s0, 2 #shift right logical srl $t2, $s0, 2 ```