UART
Modularity and Comparisons
Christopher Mar
Pseudo Operations
Similar to macros
Instructions that can are assembled as one or more native instructions
Pseudo Operations
One reason to have pseudo-ops is that PLP instructions have fixed length of 32-bits
Load immediate (li) requires more than 32-bits of information
Composed of 2 I-type: lui and ori
Pseudo Operations
Another reason is to make instructions more readable
move $t2, $t1
Copies a value from $t1 into $t2
Assembles into the single instructions:
addu $t2, $0, $t1
Stack
Stack pointer register: $sp
2 pseudo-ops
Push
Pop
Stack
Stack grows upwards in memory (address gets smaller) and starts at the largest memory address used by the stack
Initialization:
li $sp, 0x10FFFFFC
0x10FFFFFC is the largest memory address mapped to RAM
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000000
0x10FFFFFC 0x00000000
$sp
li $sp, 0x10FFFFFC
li $t1, 0x59
li $t2, 0x23
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000000
0x10FFFFFC 0x00000000
$sp
push $t1
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000000
0x10FFFFFC 0x00000000
push $t1
$sp
addiu $sp, $sp, -4
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000000
0x10FFFFFC 0x00000059
push $t1
$t1
Copied
$sp
sw $t1, 4($sp)
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000000
0x10FFFFFC 0x00000059
push $t2 # Second push operation
$sp
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000000
0x10FFFFFC 0x00000059
push $t2 # Second push operation
$sp
addiu $sp, $sp, -4
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
push $t2 # Second push operation
Copied
$t2
$sp
sw $t2, 4($sp)
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
pop $t1
$sp
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
pop $t1
$t1
Copied
$sp
lw $t1, 4($sp)
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
$sp
pop $t1
addiu $sp, $sp, 4
Increments $sp
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
pop $t2 # Second pop operation
$sp
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
pop $t2 # Second pop operation
$t2
Copied
$sp
lw $t2, 4($sp)
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
pop $t2 # Second pop operation
$sp
addiu $sp, $sp, 4
Increments $sp
Stack
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
$sp
Notice that even though things have been popped, they are still in memory
Stack Underflow
Address Contents
0x10FFFFF0 0x00000000
0x10FFFFF4 0x00000000
0x10FFFFF8 0x00000023
0x10FFFFFC 0x00000059
$sp
An additional pop from the stack in this state will result in a stack underflow
Stack Underflow Error
Attempts to read from memory address 0x11000000
0x11000000 = 0x10FFFFFC + 0x4
Stack Overflow
Occurs when attempting to push a value onto a stack that is already full
Uninitialized Stack Pointer
If you forget to initialize $sp to a memory address pushing a second value onto the stack will cause a write error
The first push will write to the address zero
The second push will be at 0xFFFFFFFC
0xFFFFFFFC = 0x0 – 0x4
Jump-And-Link
jal label
Performs a jump to label just like j instruction
Stores address of next instruction in register $ra
Return address
Jump Register
jr $ra
Performs a jump j to the address stored in $ra
Typically used to return to where jal jumped from
Example Program Demo
Available in Video Lecture
Subroutines and Functions
Subroutines perform a set of instructions
A function is like a subroutine, but it has context
Context specific registers and a return address
Context Switching
The current value of registers that are part of the function’s context are saved on the stack
Upon returning the are restored to their previous value
Referred to as a context switch
Saved and Restored Registers
$a0 – $a3: Argument registers
$s0 – $s7: Saved registers
$t0 – $t9: Temporary registers (not saved by MIPS)
$ra: return address
Function Return Value
$v0 and $v1 are used to hold return values from a function
Not saved during function call or restored during return
Call Pseudo-Op
call label
Saves current registers to stack
Performs a jump-and-link to label
NOTE: the stack pointer must be initialized in order to use call without receiving an error
Return Pseudo-Op
return
Jumps to the return address
Restores registers from stack
Call and Return Usage
Call can directly replace all jal instructions to a specific label
A return should replace the jr at the end of the function
Both call and return include the a nop as their last native instruction so they use up their own branch delay slots
Call Stack
CPU Registers
Stack (RAM)
Inside main()
Function, foo(), is called
main values
Call Stack
CPU Registers
Stack (RAM)
Inside foo()
Function, bar(), is called
main values
foo values
Call Stack
CPU Registers
Stack (RAM)
Inside bar()
Function, snafu(), is called
main values
foo values
bar values
Call Stack
CPU Registers
Stack (RAM)
Inside snafu()
Return to bar()
main values
foo values
bar values
Call Stack
CPU Registers
Stack (RAM)
Inside bar()
Return to foo()
main values
foo values
Call Stack
CPU Registers
Stack (RAM)
Inside foo()
Return to main()
main values
Performance Tradeoff
Call is composed of 26 native instructions
A combination jal and push pseudo-ops will be more efficient at saving fewer than13 registers
Push pseudo-ops use 2 instructions each
Option of using individual lw/sw with the $sp and a single $sp increment/decrement
Registers to be saved and restored needs to include $ra for nested
Value Comparison (Inequality)
slt $t3, $t1, $t2
If $t1 < $t2 then $t3 set to 1 (true)
Else $t3 set to 0 (false)
Result often used in a branch (compared with zero register)
bne $t3, $0, was_less_than
Inequalities
Inequality Instruction R if inequality is true
A < B slt R, A, B 1
A >= B slt R, A, B 0
A > B slt R, B, A 1
A <= B slt R, B, A 0
Breakpoints
While in simulation mode, double-clicking to the right of a line number with an instruction adds a breakpoint
Project 3 Pre Quiz and
Demonstration
45