ELEC1601 Week 8
Introduction to Computer Systems
(start at 14:05)
The University of Sydney
Lecture 8
Transistors RAMs/
Gates ROMs Actuators
ALUs
Flip-Flops/ Computer Sensors
Compilers
Registers Architecture Bus
Real-world
Ones and Zeros Words Instructions Programs
interactions
FSMs Pipelining Software Calibration
Floating
Combinational Point Memory/
Digital Logic Fixed Point Stack Subroutines
Addressing High-level
Propositional Binary Modes Constructs
Logic ISA/
Assembly
Machine
Code Simulation
Code
The University of Sydney
Memory visualisation
Output can be M bits (often described as A cells of B
bytes such that A*B/8=M)
N
Address Array
Data
The University of Sydney
Memory visualisation
N
Address Array
Data
How many cells in a 2GB memory, where each cell is 1
Byte.
– 2^30 *2 = 2^31
The University of Sydney
Memory visualisation
N
Address Array
Data
How many cells in a 2GB memory, where each cell is 2
Bytes.
– 2^30 *2 = 2^30
The University of Sydney
Memory visualisation
N
Address Array
Data
How much data can a memory with 10 address bits, 1
byte cells store?
– 2^10
The University of Sydney
Memory visualisation
N
Address Array
Data
10 bit address, 2-byte cells. How many cells are there in
the memory?
The University of Sydney
Memory Read/Write
Write 4 bits into 8-bit cell. What happens?
The University of Sydney
How do we store things in memory
The University of Sydney
How do we store things in memory?
What if I want to store a big thing in memory
The University of Sydney
How do we store things in memory?
Can a big endian architecture share memory with a little
endian memory?
The University of Sydney
Computer Architecture
The University of Sydney Page 12
Draw me a microprocessor
The University of Sydney Page 13
Draw me a microprocessor
The University of Sydney Page 14
AVR Microprocessor walk through
The University of Sydney Page 15
Program memory
– What does it do?
– Store instructions
– What operations does it perform?
– Read /write
– What are it’s properties?
– Each cell is ?-bit.
• Read/write is always ? bits
– Different to data memory
The University of Sydney Page 16
Program counter
– What does it do?
– Stores address of next instruction
– Increments when move to next instruction
– What type of digital circuit is it?
– Register
– Adder (conditional)
– Multiplexer
The University of Sydney Page 18
Register File
– What is it?
– Tiny, local memory
• Temporary storage
– What digital circuit is this?
– 32 8-bit Registers (r0 to r31)
– How many bits do you need to encode a register?
– What are X,Y,Z?
– Design decision
– Enable 26,27 (x), 28,29 (y), 30,31 (z) to be 16-bit manipulations
• Useful for certain instructions
– Ld R2 X
The University of Sydney Page 19
ALU
– What does it do?
– Calculations
– Add/Sub/Conjunction/Negation…
– How many bits can it add?
– Tied to a register
The University of Sydney Page 20
Data memory
– What does it do?
– Stores integers Arrays…
– 2KB. How many address bits?
– Needs 11.
– Actually has 12
• Pretend larger memory
• Gives 256 additional fake address
• Allows some faster operations.
The University of Sydney Page 21
Instruction register
– What does it do?
– Remembers current instruction
The University of Sydney Page 22
Status Register
– What does it do?
– Stores flags of special conditions
– Zero/overflow
– What does it listen to?
– ALU
– Where is this information used?
– Subsequent instruction
The University of Sydney Page 23
Instruction decoder
– What does it do
– Open instruction, decide what to do
– Controls rest of circuit
• Operation: Adding/subtracting
• Operands
• Where to write result
– Is it combinational/sequential circuit?
The University of Sydney Page 24
An example instruction:
The University of Sydney Page 25
An example instruction 2:
The University of Sydney Page 26
Computer Architecture Overview
– What do you need to know?
– What are design trade-offs with computer architecture
• RISC vs CISC
– What are the standard parts of a computer architecture
• PC, Memory, Register File, ALU, Status Register, Control Circuitry
• How they relate to what you’ve learnt so far.
• How they interact.
– What does the most basic assembly program look like (in AVR)
– Stack
The University of Sydney Page 27
Assembly Programs
All of your data
To be stored where?
Your program
To be stored where? Why global
Your first Go back
label to from
jump to subroutine
The University of Sydney Page 28
.section .data
– How do you translate (written in some high-level language) this to
assembly:
– A = 10 AVR Instruction Set Manual ([Link])
– B = [1, 2, 4, 10]
–…
–P=A
– Q = P+1
–…
– B[0] = P+Q
– B[1] = Q-P+1
The University of Sydney Page 29
Computer Architecture Overview
– What do you need to know?
– What are design trade-offs with computer architecture
• RISC vs CISC
– What are the standard parts of a computer architecture
• PC, Memory, Register File, ALU, Status Register, Control Circuitry
• How they relate to what you’ve learnt so far.
• How they interact.
– What does the most basic assembly program look like (in AVR)
– Stack
The University of Sydney Page 30
Stack
– What is the stack?
– Area in data memory
– Special position: stack pointer
• Top of stack
The University of Sydney Page 35
Push operation
– Read or write?
The University of Sydney Page 36
Push operation
– Increase or decrease stack pointer?
The University of Sydney Page 37
Push operation
– Increase or decrease stack pointer?
The University of Sydney Page 38
Pop
– Get data
– Take off stack
The University of Sydney Page 39
Stack
– push r1
– push r2
– push r3
– Do some other work with r1 to r3
– How do you get data back into r1 to r3?
The University of Sydney Page 40
Quick walk through of instructions
– Basic arithmetic (involve registers only)
– Mov
– Arithmetic
• What happens with overflow?
– Logical
• Two 8-bit AND. Meaning?
– LSL/LSR
The University of Sydney Page 41
Quick walk through of instructions
– More complex arithmetic (involve registers only)
– MUL/MULS
• Can there be overflow?
The University of Sydney Page 42
Quick walk through of instructions
– Memory:
– Load
• LDI
• LDS
• (LD)
– Store
• STS
• (SD/STD)
The University of Sydney Page 43
Quick walk through of instructions
– LD/SD
– How do you store an array in memory?
The University of Sydney Page 44
Quick walk through of instructions
– Memory:
– Push
– Pop
– (we’ll return to these shortly)
The University of Sydney Page 45
Quick walk through of instructions
– Flow control:
– JMP
– Compare
• CP
• CPI
– Branch
• BRSH/BRLT/BRGE/BRLO
• What are the differences branch vs jump?
– Call/Ret
• (We’ll discuss these in more detail later)
The University of Sydney Page 46
Quick walk through of instructions
– I/O:
– IN
– OUT
The University of Sydney Page 47
Computer Architecture Overview
– What do you need to know?
– What are design trade-offs with computer architecture
• RISC vs CISC
– What are the standard parts of a computer architecture
• PC, Memory, Register File, ALU, Status Register, Control Circuitry
• How they relate to what you’ve learnt so far.
• How they interact.
– What are the basic AVR instructions
• How do they run on the hardware
– What does the most basic assembly program look like (in AVR)
• Data Storage
• Stack
• Subroutines
The University of Sydney Page 48
Today you forgot your compiler
The University of Sydney Page 49
Assembly Programs
All of your data
To be stored where?
Your program
To be stored where? Why global
Your first Go back
label to from
jump to subroutine
The University of Sydney Page 50
Assembly programs
– Remove all conventional program structures/concepts
– Class
– Methods
– Public
– Private
– Inheritance
–…
– Why?
The University of Sydney Page 51
Writing assembly code
– Create assembly program using some IDE
– Give to assembler
– (form of compiler)
– Much simpler
– Take instructions, covert to machine code
– Generate executable (or run simulation)
– Send to USB
– Run on board
The University of Sydney Page 52
Computer Architecture Overview
– What do you need to know?
– What are design trade-offs with computer architecture
• RISC vs CISC
– What are the standard parts of a computer architecture
• PC, Memory, Register File, ALU, Status Register, Control Circuitry
• How they relate to what you’ve learnt so far.
• How they interact.
– What are the basic AVR instructions
• How do they run on the hardware
– What does the most basic assembly program look like (in AVR)
• Data Storage
• Stack
• Subroutines
The University of Sydney Page 53
.section .data
– .byte 0xAB, 12, 0b11010010
– .space 5, 0
– .space 5
– .ascii hello’
– .asciz ‘hi’ (.string ‘hi’ )
The University of Sydney Page 54
.section .data
– .section .data
– a: .byte 3, 1, 2, 3
– What is this?
– 4 byte array, values 3,1,2,3
– 3 byte array, with values 1,2,3
The University of Sydney Page 55
Labels
– Uses:
1. Use 1: Program Memory (destinations for JMP/Branch)
2. Use 2: Data Memory (address for variables)
• Appears both in memory and data section in code. Why?
• Often access using hi8/lo8
The University of Sydney Page 56
Computer Architecture Overview
– What do you need to know?
– What are design trade-offs with computer architecture
• RISC vs CISC
– What are the standard parts of a computer architecture
• PC, Memory, Register File, ALU, Status Register, Control Circuitry
• How they relate to what you’ve learnt so far.
• How they interact.
– What does the most basic assembly program look like (in AVR)
• What do basic instructions look like
– Stack
The University of Sydney Page 57
Computer Architecture Overview
– What do you need to know?
– What are design trade-offs with computer architecture
• RISC vs CISC
– What are the standard parts of a computer architecture
• PC, Memory, Register File, ALU, Status Register, Control Circuitry
• How they relate to what you’ve learnt so far.
• How they interact.
– What are the basic AVR instructions
• How do they run on the hardware
– What does the most basic assembly program look like (in AVR)
• Data Storage
• Stack
• Subroutines
The University of Sydney Page 58
Stack
– What is the stack?
– Area in data memory
– Special position: stack pointer
• Top of stack
The University of Sydney Page 59
Push operation
The University of Sydney Page 60
Pop
– Get data
– Take off stack
The University of Sydney Page 61
Stack
– push r1
– push r2
– push r3
– Do some other work with r1 to r3
– How do you get data back into r1 to r3?
The University of Sydney Page 62
Some practice – reading assembly
– What does this code do?
– LDI R16, 1
– MOV R1, R16
– LDI R17, 2
– MOV R2, R17
– ADD R1, R2
– LDI R18, 3
– MOV R3, R18
– ADD R1, R3
– LDI R19, 4
– MOV R4, R19
– ADD R1, R4
– PUSH R1
– Can you re-write using fewer instructions?
The University of Sydney Page 63
Some practice – reading assembly
– What does this code do?
LDI R21, 1
LDI R22, 2
LDI R23, 3
LDI R24, 4
JMP e3
e1: ADD R21, R22
JMP e4
e2: ADD R21, R23
JMP e1
e3: ADD R21, R24
JMP e2
e4:
The University of Sydney Page 64
Some practice – writing assembly
– How do you translate (written in some high-level language)
this to assembly:
– A = 10
AVR Instruction Set Manual ([Link])
– B = [1, 2, 4, 10]
–P = A
– Q = P+1
– B[0] = P+Q
– B[1] = Q-P+1
The University of Sydney Page 65
Some practice – stack
– What code would swap the position of registers R1 and R2
using the stack and no other registers?
The University of Sydney Page 66
Some practice – stack
– What does this code do?
LDS R1, n1 ; Loads n1 in R1
LDS R2, n2 ; Loads n2 in R2
MUL R1, R2 ; R1 = R1 * R2
MOV R3, R1 ; R3 = R1
LDS R1, n3 ; Loads n3 in R1
LDS R2, n4 ; Loads n4 in R2
MUL R1, R2 ; R1 = R1 * R2
ADD R1, R3 ; R1 = R1 + R3
Can you avoid using R3 by using the stack?
The University of Sydney Page 67
Some practice – writing memory items in AVR
– How do you store this data structure in AVR assembly?
int array1[][] = {{11, 12}, {21, 22}, {31, 32}};
int array2[2][3];
Can you avoid using R3 by using the stack?
– How do you access array1[0][1] and array2[0][1]
The University of Sydney Page 68
Some practice – writing AVR programs
– Write an assembly program that:
– declares three 8-bit numbers as data
– selects the first with value zero (if it exists).
– Return in registers R25:R24 (that is, the 16 bit data obtained
concatenating the two registers)
• 3 if none of the three numbers is zero
• 0, 1 or 2 if the first, second or third number are zero
respectively
The University of Sydney Page 69
Computer Architecture Overview
– What do you need to know?
– What are design trade-offs with computer architecture
• RISC vs CISC
– What are the standard parts of a computer architecture
• PC, Memory, Register File, ALU, Status Register, Control Circuitry
• How they relate to what you’ve learnt so far.
• How they interact.
– What are the basic AVR instructions
• How do they run on the hardware
– What does the most basic assembly program look like (in AVR)
• Data Storage
• Stack
• Subroutines
The University of Sydney Page 70
Subroutine
– What is a subroutine?
– A method to run methods/functions/procedures on a CPU
– How do subroutines work?
– Get parameters/return space ready
– Call
– Do subroutine
– Return
– Obtain result
The University of Sydney Page 71
Example
Main program Subroutine (called)
(caller)
. label: .
. .
. .
CALL label .
.(a) RET
.
.
.
.
.
.
The University of Sydney Page 72
Example
Main program Subroutine (called)
(caller)
. label: .
. .
. .
CALL label .
.(a) RET
.
.
. What is CALL?
. (why not JMP?)
.
.
The University of Sydney Page 73
Example
Main program Subroutine (called)
(caller)
. label: .
. .
. .
CALL label .
.(a) RET
.
.
.
.
CALL label
. (b)
.
.
The University of Sydney Page 74
Example 2
Main program Subroutine (called) sub2: .
(caller) .
. label: . .
. . .
. CALL sub2 .
CALL label . RET
.(a) RET
.
.
.
.
CALL label
. (b)
.
.
How many return addresses
do we need to remember
here?
The University of Sydney Page 75
Example 3
Main program Subroutine (called) sub2: .
(caller) .
. label: . .
. . if …
. CALL sub2 sub2
CALL label . .
.(a) RET RET
.
.
.
.
CALL label
. (b)
.
.
How many return addresses
do we need to remember
here?
The University of Sydney Page 76
Example 3
Main program Subroutine (called) sub2: .
(caller) .
. label: . .
. . if …
. CALL sub2 sub2
CALL label . .
.(a) RET RET
.
.
.
.
CALL label Does a subroutine always have to come back (RET)
. (b) (can you jump elsewhere)
.
.
How many return addresses
do we need to remember
here?
The University of Sydney Page 77
Let’s consider a program
void main (….) { function (int x, int y) {
int j, k , result …
… x++;
j = 10; y++;
k = 20; …
… return (x+y)*(x-y)
result = [Link](j,k) }
…
Programming questions: Assembly questions:
What is the value of x, y after the function call? How do I pass these parameters?
Can I use the variable j? How do I return result?
How do I enforce scope?
The University of Sydney Page 78
Subroutines – the conflict
– To run, subroutines need registers/stack:
– To make life easy for the calling program
• Any data held in registers/stack in calling program should
be the same before/after
• (except the result)
– To make life easy for the subroutine
• It should be free to use all registers/stack
– Who should be in charge?
The University of Sydney Page 79
Subroutines
– Who should be in charge?
– AVR compiler has some arbitrary rules:
– Registers must obey following policy:
[Link]
stack-and-register-management?module_item_id=577848
– Stack must be unchanged.
• Can you still use it?
The University of Sydney Page 80
Passing data (in general)
.
Copy
. label: .
parameters
. . Use
CALL label parameters
.
Parameters
. Retrieve and produce
. results . results
. RET
.
.
CALL label
.
. Results
.
The University of Sydney Page 81
Passing data (in general)
.
Copy
. label: .
parameters
. . Use
CALL label parameters
.
Parameters
. Retrieve and produce
. results . results
. RET
.
.
CALL label
.
. Easiest way to do this
Results is with the stack
.
The University of Sydney Page 82
Passing data (in general)
– Is passing parameters by pushing/popping efficient?
– How else could we do it?
• Need to understand the stack pointer
The University of Sydney Page 83
Studying the stack (general function call)
(a)
label: (d)
. .
.
(b) .
CALL label .
(c)
. .
. SP Initial RET
.
.
CALL label
.
.
.
The University of Sydney Page 84
Studying the stack (general function call)
(a)
label: (d)
. .
.
(b) .
CALL label .
(c)
. .
. SP Initial RET
.
.
CALL label
.
.
.
If you issue CALL label, how does the stack/stack pointer change
The University of Sydney Page 85
Studying the stack (general function call)
(a)
label: (d)
. .
.
(b) .
CALL label .
(c)
. .
. SP Initial RET
.
.
CALL label
.
.
.
If you want results, how do you send/retrieve them through the sta
The University of Sydney Page 86
Studying the stack (general function call)
(a)
Push to stack label: (d)
. space for .
results .
(b) .
CALL label .
(c) Return result
. Retrieve parameters
.
. SP Initial RET
results
.
.
CALL label
.
.
.
How does the stack/stack pointer change?
The University of Sydney Page 87
Studying the stack (general function call)
(a)
Push to stack label: (d)
. space for .
results .
(b) .
CALL label .
(c) Return result
. Retrieve parameters
.
. SP Initial RET
results
.
.
CALL label
.
.
. How do you pass parameters & results through the stack
The University of Sydney Page 88
Studying the stack (general function call)
(a)
Push to stack label: (d)
. space for .
results .
(b) .
CALL label .
(c) Return result
. Retrieve parameters
.
. SP Initial RET
results
.
.
CALL label
.
.
. Suppose you want to use the stack to store additional temporary variab
within your subroutine. Where are they stored?
The University of Sydney Page 89
Studying the stack (general function call)
(a)
Push to stack label: (d)
. space for .
results .
(b) .
CALL label .
(c) Return result
. Retrieve parameters
.
. SP Initial RET
results
.
.
CALL label
.
.
. Suppose you want to use the stack to store additional temporary variab
within your subroutine. Where are they stored?
How do you return in this case?
The University of Sydney Page 90
Studying the stack (general function call)
(a)
Push to stack label: (d)
. (param and .Access
results) .
parameters
(b) .
CALL label .
(c) Return result
. Retrieve parameters
.
. SP Initial RET
results
.
.
CALL label
.
.
. How do you get the results back?
The University of Sydney Page 91
Accessing parameters on demand
– Where is the stack?
– Data Memory
– What do you need to access parameters on demand?
– Play around with the stack pointer
The University of Sydney Page 92
Stack pointer
– How many bits?
– 16
– Special register (I/O…)
– Stored in 0x3E:0x3D
– Access with ‘IN’ Command
• IN R28, 0x3D (like MOV…can’t use MOV because…)
• IN R27, 0x3E
– Modify with ‘OUT’ Command
• OUT 0x3D, R30 ;
• OUT 0x3E, R31
The University of Sydney Page 93