0% found this document useful (0 votes)
24 views

ELEC1601 Week 9 2023

ELEC1601 Week 9 2023

Uploaded by

februarydtz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

ELEC1601 Week 9 2023

ELEC1601 Week 9 2023

Uploaded by

februarydtz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

ELEC1601 Week 9

Introduction to Computer Systems

(start at 14:05)

The University of Sydney


Outline
• The Project
• What you should be doing next week
• Video Demo

• Recap prior to break

• New top

• Mid-sem (hopefully run through next week)


The University of Sydney
Dealing with a corner

No sensor sees
obstacle –
accelerate until
speed =3
reached

The University of Sydney


Dealing with a corner

No sensor sees
obstacle –
accelerate until
speed =3
reached

The University of Sydney


Dealing with a corner

Sensor active Front sensor


sees wall –
!
slow down to
stop

The University of Sydney


Dealing with a corner

Sensor active Front sensor


sees wall –
!
slow down to
stop (has some
momentum)

The University of Sydney


Dealing with a corner

Sensor active Reverse a bit


until sensor
!
just sees wall,
stop

The University of Sydney


Dealing with a corner

Once stopped.
First rotate
clockwise unless
left sensor active
or 90 degrees

The University of Sydney


Dealing with a corner

Once stopped.
First rotate
clockwise unless
left sensor active
or 90 degrees

The University of Sydney


Dealing with a corner

90 degree Once stopped.


reached First rotate
clockwise unless
left sensor active
or 90 degrees

The University of Sydney


Dealing with a corner

Undo rotation

The University of Sydney


Dealing with a corner

Undo rotation

The University of Sydney


Dealing with a corner

Undo rotation

The University of Sydney


Dealing with a corner

Rotate
anticlockwise
unless right
sensor active or
90 degrees

The University of Sydney


Dealing with a corner

Rotate
anticlockwise
unless right
sensor active or
Sensor active
90 degrees
– wall is on
right
The University of Sydney
Dealing with a corner

Undo rotation

The University of Sydney


Dealing with a corner

Undo rotation

The University of Sydney


Dealing with a corner

Reverse a bit

The University of Sydney


Dealing with a corner

Reverse a bit

The University of Sydney


Dealing with a corner

Compare how
long turns took -
will tell if wall is
on left or right

The University of Sydney


Dealing with a corner

Wall is on right.
Rotate
anticlockwise

The University of Sydney


Dealing with a corner

Wall is on right.
Rotate
anticlockwise

The University of Sydney


Dealing with a corner

Wall is on right.
Rotate
anticlockwise

The University of Sydney


Dealing with a corner

Done. Move
forward

The University of Sydney


Recap with code

The University of Sydney


Recap with code

No sensor sees
obstacle –
accelerate until
speed =3
reached

The University of Sydney


Recap with code

Sensor active Front sensor


sees wall –
!
slow down to
stop

The University of Sydney


Recap with code

Slow down
Back off a bit
Return to a stop

The University of Sydney


Recap with code

Once stopped.
First rotate
clockwise unless
left sensor active
or 90 degrees

The University of Sydney


Recap with code

Undo rotation

The University of Sydney


Recap with code

Repeat same code


(anticlockwise and
undo rotation)

The University of Sydney


Recap with code

Reverse a bit

The University of Sydney


Recap with code

Wall is on right.
Rotate
anticlockwise

The University of Sydney


Outline
• The Project
• What you should be doing next week
• Video Demo

• Recap prior to break

• Examples/Addressing Modes/Subroutines

• Mid-sem (hopefully run through next week)


The University of Sydney
Lecture 9

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
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


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


Quick walk through of instructions
More complex arithmetic (involve registers only)
– MUL/MULS
• Can there be overflow?

The University of Sydney


Quick walk through of instructions
Memory:
– Load
• LDI
• LDS
• (LD)
– Store
• STS
• (SD/STD)

The University of Sydney


Quick walk through of instructions
LD/SD
– How do you store an array in memory?

The University of Sydney


Quick walk through of instructions
Memory:
– Push
– Pop

(we’ll return to these shortly)

The University of Sydney


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
Quick walk through of instructions
I/O:
– IN
– OUT

The University of Sydney


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
• Addressing Modes
• Subroutines

The University of Sydney


.section .data
.byte 0xAB, 12, 0b11010010
.space 5, 0
.space 5
.ascii “hello”
.asciz ‘hi’ (.string ‘hi’ )

Check it out in the simulator

The University of Sydney


Stack: Push & Pop
– Push – Pop

The University of Sydney Page 46


Some practice – Examples - 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 47


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 48
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 49


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 50


Some practice – writing assembly
– How do you translate (written in some high-level language)
this to assembly:
– A = 10
AVR Instruction Set Manual (microchip.com)

– B = [1, 2, 4, 10]
–P = A
– Q = P+1
– B[0] = P+Q
– B[1] = Q-P+1

The University of Sydney Page 51


Some practice – writing assembly
– How do you translate (written in some high-level language)
this to assembly:
– A = 10
AVR Instruction Set Manual (microchip.com)

– B = [1, 2, 4, 10]
–P = A
– Q = P+1
– B[0] = P+Q
– B[1] = Q-P+1
Is there a nice way to address B[1]?
The University of Sydney Page 52
Recap Week 8
– 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
- Examples
• Addressing Modes
• Subroutines
The University of Sydney Page 53
Addressing Mode 1: Register Direct
– Sample Instruction: (MOV)
– Where is the operand?
– Where is the address of the operand?

The University of Sydney Page 54


Addressing Mode 2: Immediate
– Sample Instruction: (SUBI)
– Where is the operand?
– Where is the address of the operand?

The University of Sydney Page 55


Addressing Mode 3: Data Direct
– Sample Instruction: (LDS)
– Where is the operand?
– Where is the address of the operand?

The University of Sydney Page 56


Addressing Mode 4: Data Indirect
– Sample Instruction: (LD)
– Where is the operand?
– Where is the address of the operand?

The University of Sydney Page 57


Addressing Mode 4: Data Indirect
– Sample Instruction: Where is the operand?
– Where is the address of the operand?
X/Y/Z must be pre-loaded with
correct address

The University of Sydney Page 58


Addressing Mode 4: Data Indirect
– Sample Instruction:
Ldi R27, hi8(label)
– Where is the operand? Ldi r26, lo8(label)

– Where is the address of the operand?


X/Y/Z must be pre-loaded with
correct address

The University of Sydney Page 59


Addressing Mode 4: Data Indirect
– Sample Instruction:
Ldi R27, hi8(label)
– Where is the operand? Ldi r26, lo8(label)

– Where is the address of the operand?


X/Y/Z must be pre-loaded with
correct address

The University of Sydney Does order matter?How do I remember X/Y/Z? Page 60


Addressing Mode 5: Data Indirect (with Post-increment)
– Sample Instruction: (LD R10 X+)
– Where is the operand?
– Where is the address of the operand?

The University of Sydney Page 61


Addressing Mode 6: Data Indirect (with Pre-increment)
– Sample Instruction: (LD R10 -X)
– Where is the operand?
– Where is the address of the operand?

The University of Sydney Page 62


Addressing Mode 7: Data Indirect (with Displacement)
– Sample Instruction: (LD R10 Y+3)
– Where is the operand?
– Where is the address of the operand?

The University of Sydney Page 63


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
• Addressing Modes
• Subroutines

The University of Sydney Page 66


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 67


Example
Main program Subroutine (called)
(caller)
. label: .
. .
. .
CALL label .
.(a) RET
.
.
.
.
.
.

The University of Sydney Page 68


Example
Main program Subroutine (called)
(caller)
. label: .
. .
. .
CALL label .
.(a) RET
.
.
. What is CALL?
. (why not JMP?)
.
.

The University of Sydney Page 69


Example
Main program Subroutine (called)
(caller)
. label: .
. .
. .
CALL label .
.(a) RET
.
.
.
.
CALL label
. (b)
.
.

The University of Sydney Page 70


Ex_sub.py

The University of Sydney Page 71


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 72
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 73
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 74
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 = obj.function(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 75


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 76


Subroutines
– Who should be in charge?

– AVR compiler has some arbitrary rules:


– Registers must obey following policy:
https://canvas.sydney.edu.au/courses/17907/pages/8-dot-4-
stack-and-register-management?module_item_id=577848
– Stack must be unchanged.
• Can you still use it?

The University of Sydney Page 77


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 78


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 79


Ex_sub.py
– Modify this to pass via the stack?

The University of Sydney Page 80


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 81


Studying the stack (general function call)
(a)
label: (d)
. .
.
(b) .
CALL label .
(c)
. .
. SP Initial RET
.
.
CALL label
.
.
.

The University of Sydney Page 82


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 83


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 84


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 85


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 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
.
.
. Suppose you want to use the stack to store additional temporary variab
within your subroutine. Where are they stored?

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
.
.
. 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 88
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 89


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 90


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 91

You might also like