Load Word and
R-Type Instructions
By Christopher Mar
Announcements
Module 2 now available
Project 2 Pre Quiz due 9/12
Project 2 Program and Post Quiz due 9/19
Project 2 Demo
Load Word Instruction
Load Word Instruction
Load word copies value from memory to a register
lw $t3, 0($t4)
Memory Address
Destination
Lecture Question
A load word instruction can be used to…
Read a value from an I/O device
Write a value to an I/O device
Not enough information is given to answer this question
I/O Device: Switches
I/O Device: Switches
Each switch corresponds to a specific bit of a single value
Value located at the address, 0xf0100000
I/O Device: Switches
Each switch controls a bit position that is a representation of a power of 2
Switch 0 represents 20 (= 1)
Switch 1 represents 21 (= 2)
Switch 2 represents 22 (= 4)
Continues to switch 7 (27 = 128)
I/O Device: Switches
For example, what will the value at the switch address be if switches 0, 3, and 5 are set?
20 + 23 + 25 =
1 + 8 + 32 =
Lecture Question
Suppose switches 1, 2, and 6 are set. What will be the value at the switch address?
Lecture Question
If $t5 contains 0xf0100000, which instruction would copy the current value of the switches into $t6 (which currently contains zero)?
sw $t5, 0($t6)
sw $t6, 0($t5)
lw $t5, 0($t6)
lw $t6, 0($t5)
I-Type vs. R-Type Instructions
I-Type Instructions
Output destination:
1 register
1 immediate value
R-Type Instructions
Output destination:
2 register
Exception:
Shift instructions use one register and a shift amount (shamt)
I-Type Add
Add immediate instruction
addiu $t3, $t2, 29
Equivalent to the assignment statement:
$t3 = $t2 + 29
R-Type Add
Add instruction
addu $t6, $t4, $t5
Equivalent to the assignment statement:
$t6 = $t4 + $t5
Logical Shift Left
Shift value left by an indicated number of binary digits (bits)
sll $t2, $t3, 2
Equivalent to the assignment statement:
$t2 = $t3 << 2
Logical Shift Left
For the previous instruction, suppose $t3 contained 0b00110101 (== 0x35 == 53)
sll $t2, $t3, 2
0 0 1 1 0 1 0 1
Logical Shift Left
First shift left by 1 bit
0 0 1 1 0 1 0 1
Logical Shift Left
New least significant bit is a zero
Old most significant bit is lost
0 1 1 0 1 0 1 0
0 0 1 1 0 1 0 1
Logical Shift Left
Second shift left by 1 bit
0 1 1 0 1 0 1 0
Logical Shift Left
Once again:
New least significant bit is a zero
Old most significant bit is lost
1 1 0 1 0 1 0 0
0 1 1 0 1 0 1 0
Shifts as Mathematical Operators
A binary shift of a number left by x bits is the same as multiplying the number by 2x
Just like multiplying a decimal number by 10 shifts the digits one position to the left
Shifting right is equivalent to integer division by the same power of 2
Lecture Question
If $t3 contains 7, what value will $t4 have after the instruction, sll $t4, $t3, 3?
Switch Reading Example