Microsoft PowerPoint – Week 9 2021
Week 9
Start at 12:05 as per usual
Outline
• Open project discussion
• Special thanks:
• Tim Huxley
• Christopher Polak
• Lindsay Bath
• Quick recap:
• Stack
• Discuss instructions on computer architectures
• RISC vs CISC
• Running on AVR hardware
• Programming in assembly
Stack
• What is the stack?
Stack
• What is the stack?
• Area in data memory
• Special position: stack pointer
• Top of stack
Stack
• Reminder:
• push r1
• push r2
• push r3
• Do some other work with r1 to r3
• How do you get data back into r1 to r3?
Activity 3
You have to design a microprocessor.
What operations should it include?
https://padlet.com/davidboland/t4utdcwqzd05hdz8
Typical instructions
• Add/subtract
• Mov
• Load
• Store
• Compare
• Branch (conditional)
• Jump
• I/O
Common instructions
• Multiply
• Divide Boolean operations
Common instructions?
• Fused Multiply Add
• Floating‐point
More instructions
• SIMD
• Accumulate (parallel)
• Trigonometric
• Function evaluation
RISC vs CISC
• Add/subtract
• Mov
• Load
• Store
• Compare
• Branch (conditional)
• Jump
• I/O
• Multiply
• Divide
• Boolean operations
• Fused Multiply Add
• Floating‐point
• SIMD
• Accumulate (parallel)
• Trigonometric
• Function evaluation
Why RISC vs CISC
• RISC can do everything that CISC can do. Why not use sequence of
instructions?
CISC vs RISC
• Time to decode
• Succinct code
• Instruction Faster
• Bits/instruction
CISC vs RISC
• Time to decode
• Succinct code
• Instruction Faster
• Bits/instruction
• Faster code execution?
• Simpler compiler
Other differences? CISC OR RISC
• ADD R1 R2
• Vs
• ADD R1 R2 R3
Activity 1
Activity 2
AVR Instruction set:
• Full instruction set:
(http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel‐0856‐
AVR‐Instruction‐Set‐Manual.pdf)
• Do not memorise!
• Know how to use
• Summary document
(https://canvas.sydney.edu.au/courses/17907/pages/week‐
8?module_item_id=577689)
• You get this in exam
Quick walk through of instructions
• EASY:
• Mov
• Arithmetic
• What happens with overflow?
• Logical
• Two 8‐bit AND. Meaning?
• LDI/STS
• PUSH/POP
• Easy‐ish:
• MUL/MULS
• Can there be overflow?
Reading the manual & understanding how it
runs
• ADD
• CPI
• (JUMP/Branch later)
Quick walk through of instructions
• Medium:
• LD/ST
Quick walk through of instructions
• LDI vs LDS how many memory accesses
Quick walk through of instructions
• LD
Quick walk through of instructions
• Harder:
• LSL/LSR
• ASR
• Why no ASL?
Jump/Branch
• Why need to JUMP/Branch?
Jump/Branch
• Why need to JUMP/Branch?
• What does it do?
Jump
• How to specify in assembly:
Label_location: Add R8 R9
…
JMP label_location
Jump
• How to specify address:
• In data memory or program memory?
Program basics
• What is starting point of JAVA/C program?
• Main method
• Can be member of class…
• How does assembly code start your program?
Activity 4
Branch
• Branch:
• JUMP only if condition
• Why need it?
Branch
• What instruction needs to be executed before a branch?
• How far can you branch?
• What does it do?
Normal programming procedure
• On Computer:
• Start IDE (integrated development environment)
• Write program in Arduino C
• Compile
• Send executable to board via USB
Today you forgot your compiler
Writing assembly code
• Need to know architecture
• Need to know ISA
• Create assembly program using same IDE
• Give to assembler
• (form of compiler)
• Much simpler
• Take instructions, covert to machine code
• Generate executable
• Send to USB
• Run on board
Assembly programs
• Remove all conventional program structures/concepts
• Class
• Methods
• Public
• Private
• Inheritance
• …
• Why?
Assembly Programs
Assembly Programs
All of your data
To be stored where?
Assembly Programs
Your program
To be stored where?
Assembly Programs
Why global
Assembly Programs
Is main the only global we might want?
Assembly Programs
Your first
label to
jump to
Assembly Programs
Go back
from
subroutine
.section .data
• .byte 0xAB, 12, 0b11010010, ‘a’
• How does this look in program memory?
• Where does it store it in program memory?
.section .data
• .byte 0xAB, 12, 0b11010010, ‘a’
• .space 5, 0
What does it look like?
.section .data
• .byte 0xAB, 12, 0b11010010, ‘a’
• .space 5,0
• .byte 0x01, 0x02
• .byte 0xAB, 12, 0b11010010, ‘a’
• .space 5
• .byte 0x01, 0x02
What is the difference?
.section .data
• .byte 0 vs .space 0
• Any difference?
.section .data
• .byte 0 vs .space 0
• Any difference?
• No
.section .data
• .byte vs .space
• Any difference?
.section .data
• .ascii ‘hello’
• What does it look like?
• .asciz ‘hi’ (.string ‘hi’ )
• What does it look like?
.section .data
• .asciz “0”
• How do you differentiate between 0 and end of string
Activity 1
.section .data
• .section .data
• a: .byte 3, 1, 2, 3
• What is this?
.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
Activity 2
Activity 2
• Row major vs column major
Writing assembly programs
• Hard!
• You need to know/choose underlying data structure
• Not clear just looking at memory what is happening
• Give you some idea:
1. Understanding Assembly: Activity 3 week 8
2. Writing Assembly
.section .data
• How do you translate (written in some high‐level language) this to
assembly:
• A = 10
• B = [1, 2, 4, 10]
• …
• P = A
• Q = P+1
• …
• B[0] = P+Q
• B[1] = Q‐P+1
Writing assembly programs
• How do you access data?
• Subject for addressing modes (next week)
• But give short intro here:
• Look up AVR code
• (http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel‐0856‐ AVR‐Instruction‐Set‐
Manual.pdf )
• (https://canvas.sydney.edu.au/courses/17907/pages/week‐8?module_item_id=577689 )
Choose your instruction
• Look up AVR code
• (http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel‐0856‐ AVR‐
Instruction‐Set‐Manual.pdf )
• (https://canvas.sydney.edu.au/courses/17907/pages/week‐
8?module_item_id=577689 )
Labels
• Uses:
1. Use 1: Program Memory (destinations for JMP/Branch)
2. Use 2: Data Memory (address for variables)
Labels
• Uses:
1. Use 1: Program Memory (destinations for JMP/Branch)
Recap why use branch vs JMP?
2. Use 2: Data Memory (address for variables)
Labels
• Uses:
1. Use 2: Data Memory (address for variables)
• (appears both in memory and data section in code. Why?)
Labels
• Uses:
1. Use 2: Data Memory (address for variables)
• (appears both in memory and data section in code. Why?)
• Used in LDS, SDS (describe, same instruction)
• Can access with lo, hi
Labels
• Uses:
1. Use 2: Data Memory (address for variables)
• (can appear in memory/data section)
• Used in LDS, SDS (describe, same instruction)
• Can access with lo, hi
• If have following code:
l1:
l2: .byte 1
What do l1/l2 point to?
Labels
• Uses:
1. Use 2: Data Memory (address for variables)
• (can appear in memory/data section)
• Used in LDS, SDS (describe, same instruction)
• Can access with lo, hi
• If have following code:
l1:
l2: .byte 1
What do l1/l2 point to?
Same location
Activity 3
Subroutines
• How do I pass parameters to a function?
Subroutines
• To run a subroutine, you need to know:
• It’s address
• What variables are passed in
• What variables/results are returned
Subroutines
• To run a subroutine, you need to know:
• It’s address
• What variables are passed in
• What variables/results are returned
• To run, subroutines need
Subroutines
• To run a subroutine, you need to know:
• It’s address
• What variables are passed in
• What variables/results are returned
• To run, subroutines need registers/stack:
Subroutines
• To run a subroutine, you need to know:
• It’s address
• What variables are passed in
• What variables/results are returned
• 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?
Subroutines
• Who should be in charge?
• AVR compiler has some arbitrary rules:
https://canvas.sydney.edu.au/courses/17907/pages/8‐dot‐4‐stack‐and‐
register‐management?module_item_id=577848
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?
Subroutines
• Calling function:
• What do I need to do to any values in registers I want to retain?
• Subroutine:
• What do I need to do I want to use registers?
Activity 4