Lecture 4,5 - Processor Architecture Overview - Part 2
Lecture 4,5 - Processor Architecture Overview - Part 2
Computer Architecture
Sarah Harris & David Harris
Chapter 6:
Architecture
Chapter 6 :: Topics
• Introduction
• Assembly Language
• Programming
• Machine Language
• Addressing Modes
• Lights, Camera, Action:
Compiling, Assembly, & Loading
• Odds & Ends
Logical / Shift
Instructions
Programming
• High-level languages:
– e.g., C, Java, Python
– Written at higher level of abstraction
• High-level constructs: loops, conditional
statements, arrays, function calls
• First, introduce instructions that support
these:
– Logical operations
– Shift instructions
– Multiplication & division
– Branches & Jumps
Multiplication and
Division
Multiplication
32 × 32 multiplication → 64 bit result
mul s3, s1, s2
s3 = lower 32 bits of result
mulh s4, s1, s2
s4 = upper 32 bits of result, treats operands as signed
{s4, s3} = s1 x s2
Example: s1 = 0x40000000 = 230; s2 = 0x80000000 = -231
s1 x s2 = -261 = 0xE0000000 00000000
s4 = 0xE0000000; s3 = 0x00000000
target: # label
add s1, s1, s0 # s1 = 4 + 4 = 8
target:
add s1, s1, s0 # s1 = 1 + 4 = 5
target:
add s1, s1, s0 # s1 = 1 + 4 = 5
Conditional
Statements & Loops
Conditional Statements & Loops
• Conditional Statements
– if statements
– if/else statements
• Loops
– while loops
– for loops
L1:
f = f – i; sub s0, s0, s3
else L1:
f = f – i; sub s0, s0, s3
done:
Arrays
Arrays
• Access large amounts of similar data
• Index: access each element
• Size: number of elements
123B4790 array[4]
123B478C array[3]
123B4788 array[2]
123B4784 array[1]
123B4780 array[0]
Main Memory
loop:
bge s1, t2, done # if not then done
slli t0, s1, 2 # t0 = i * 4 (byte offset)
add t0, t0, s0 # address of array[i]
lw t1, 0(t0) # t1 = array[i]
slli t1, t1, 3 # t1 = array[i] * 8
sw t1, 0(t0) # array[i] = array[i] * 8
addi s1, s1, 1 # i = i + 1
j loop # repeat
done:
Function Calls
Function Calls
• Caller: calling function (in this case, main)
• Callee: called function (in this case, sum)
C Code
void main()
{
int y;
y = sum(42, 7);
...
}
void simple() {
return; 0x0000051c simple: jr ra # return
}
void means that simple doesn’t return a value
jal simple:
ra = PC + 4 (0x00000304)
jumps to simple label (PC = 0x0000051c)
jr ra:
PC = ra (0x00000304)
The Stack
The Stack
• Memory used to temporarily
save variables
• Like stack of dishes, last-in-
first-out (LIFO) queue
• Expands: uses more memory
when more space needed
• Contracts: uses less memory
when the space is no longer
needed
Preserved Nonpreserved
Callee-Saved Caller-Saved
s0-s11 t0-t6
sp a0-a7
ra
stack above sp stack below sp
f2's stack
BEF7FEF4 BEF7FEF4 BEF7FEF4 s4 sp
•Callee
– Save registers that might be disturbed (s0-s11)
– Perform function
– Put result in a0
– Restore registers
– Return: jr ra