# Chapter 3

### Addition Instructions

Operation Meaning
add Rd,Rs,Rt Rd = Rs + Rt
addi Rt,Rs,Immediate Rt = Rs + Immediate
addu Rd,Rs,Rt Rd = Rs + Rt (unsigned)
addiu Rt, Rs, Immediate Rt = Rs + Immediate (unsigned)

### Subtraction Instructions

Operation Meaning
sub Rd,Rs,Rt Rd = Rs - Rt
subu Rd,Rs,Rt Rd = Rs - Rt (unsigned)

### Multiplication Instructions

The results of mult ops are stored in special hi,lo registers for the high 32 bits of the result and the low 32 bits of the result.

These values can be then extracted into local regs via 'mflo <reg>' and 'mfhi <reg>' operations

Operation Meaning
mult Rs, Rt [hi,lo] = Rs * Rt
mult Rd, Rs, Rt Rd = Rs * Rt

### Boolean Instructions

Operation Meaning
and Rd, Rs, Rt Rd = Rs & Rt
andi Rt, Rs, Immediate Rt = Rs & Immediate
or Rd, Rs, Rt Rd = Rs | Rt
ori Rt, Rs, Immediate Rt = Rs | Immediate
xor Rd, Rs, Rt Rd = Rs ^ Rt
xori Rt, Rs, Immediate Rt = Rs ^ Immediate
nor Rd, Rs, Rt Rd = ~(Rs | Rt)

### Division Instructions

For division, the result is stored in the 'lo' register and the remainder is stored in the 'hi' register (for integers, at least). These can also be extracted with mflo and mfhi like for multiplication.

Operation Meaning
div Rs,Rt [hi,lo] = Rs/Rt

### Shift Instructions

Operation Meaning
sll Rd, Rt, Immediate Rd = Rt << Immediate (use zeros to fill in (logical shift))
sllv Rd, Rt, Rs Rd = Rt << Rs (use zeros to fill in (logical shift))
srl Rd, Rt, Immediate Rd = Rt >> Immediate (use zeros to fill in (logical shift))
srlv Rd, Rt, Rs Rd = Rt >> Rs (use zeros to fill in (logical shift))
sra Rd, Rt, Immediate Rd = Rt >> Immediate (use sign bit to fill in (arithmatic shift; preserves sign))
srav Rd, Rt, Rs Rd = Rt >> Rs (use sign bit to fill in (arithmatic shift; preserves sign))
rol Rd, Rt, Immediate Rd = Rt << Immediate (circular shift; fill in with shifted out bits)
ror Rd, Rt, Immediate Rd = Rt >> Immediate (circular shift; fill in with shifted out bits)

### Addition Pseudo Instructions

Pseudo Operation Translation
add Rt,Rs,Immediate (16 bit value) addi Rt,Rs,Immediate
[add|addi|addiu] Rt,Rs,Immediate (32 bit value) lui \$at, Immediate (upper 16 bits)
ori \$at, Immediate (lower 16 bits)
add Rt, Rs, \$at

### Subtraction Pseudo Instructions

Pseudo Operation Translation
sub Rt,Rs,Immediate (16 bit value) addi \$at,\$zero,Immediate
sub Rt, Rs, \$at
[subi|subiu] Rt,Rs,Immediate (16 bit value) addi \$at,\$zero,Immediate (signed or unsigned)
[sub|subu] Rt, Rs, \$at
[sub|subi|subiu] Rt,Rs,Immediate (32 bit value) lui \$at, Immediate (upper 16 bits)
ori \$at, Immediate (lower 16 bits)
sub Rt, Rs, \$at

### Multiplication Pseudo Instructions

Pseudo Operation Translation
mult Rd,Rs,Immediate addi \$Rt, \$zero, Immediate
mult Rd, Rs, Rt
mulo Rd,Rs,Immediate addi \$Rt, \$zero, Immediate
mulo Rd, Rs, Rt
mulo Rd, Rs, Rt Rd = Rs * Rt; throws exception if overflow occurs

### Division Pseudo Instructions

Pseudo Operation Translation
div Rd, Rs, Rt if Rt != 0; Rd = Rs / Rt
else break
div Rd,Rs,Immediate addi \$Rt, \$zero, Immediate
div Rs, Rt
mflo Rd
rem Rd, Rs, Rt if Rt != 0; Rd = Rs % Rt
else break
rem Rd, Rs, Immediate addi \$Rt, \$zero, Immediate
div Rs, Rt
mfhi Rd

### Boolean Pseudo Instructions

Pseudo Operation Translation
[and|or|xor] Rt, Rs, Immediate [andi|ori|xori] Rt, Rs, Immediate
[and|or|xor] Rs,Immediate [and|or|xor] Rs, Rs, Immediate
not Rs, Rt nor Rs, Rt, \$zero (Rs = !Rt)

Also, the auto handling of 16bit vs 32bit immediates, same as done for addition,etc

### Addition Sample Prog

``````
.text

main:

# add 3 + 5 into \$t0
li \$t0, 3
addi \$t0, \$t0, 5

# print out result
li \$v0, 1 # 1 = print int
move \$a0, \$t0
syscall

# exit
li \$v0, 10
syscall

.data
```
```

### IsEven: Division/Remainder Sample Prog

``````
# Is Odd/Even checker
# shows division and remainder

.text

main:
# Get user input number, stored in \$t0
li \$v0, 4 # print string
la \$a0, prompt
syscall
li \$v0, 5 # get integer
syscall
move \$t0, \$v0

# Divide by 2 so we get the remainder (stored in hi)
div \$t1, \$t0, 2 # t1 = t0 / 2

# print out the division result
li \$v0, 4 # print string
la \$a0, divresstr
syscall
li \$v0, 1 # print int
move \$a0, \$t1
syscall

# print out remainder (is even?)
li \$v0, 4
la \$a0, isevenstr
syscall
li \$v0, 1
mfhi \$a0
syscall

# exit
li \$v0, 10
syscall

.data
prompt:       .asciiz "enter a number to check if odd or even:"
divresstr:    .ascii "Input / 2: \0" # \0 so the string is null terminated
isevenstr:    .ascii "\nIs Even: \0"
```
```

### XOR Bit Flipper: Bit flips users input using XOR instead of nor or not

``````
# Program's logical not (bitwise flip) using xor
# assumes using 32bit integer

.text

main:

# prompt for a number, storing in \$t0
li \$v0, 4 # print string
la \$a0, prompt
syscall
li \$v0, 5 # get int
syscall
move \$t0, \$v0

# bit flip it using (a XOR 0b11111111... = ~a), storing in \$t1
xori \$t1, \$t0, 0xffffffff

# print out the result
li \$v0, 1 # print int
move \$a0, \$t1
syscall

# exit
li \$v0, 10
syscall

.data:
prompt: .asciiz "Enter a number to bit flip: "

```
```