Microsoft PowerPoint – Week 11 2021
Week 11
Start at 12:05 as per usual
Outline
• Mid‐Sem run‐through
• Programming in assembly
• Labels/Subroutines/Stack Management policy
• Addressing Modes
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
Writing assembly programs
• How do you access data?
• Subject for addressing modes
• 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 )
Example
• How do you translate (written in some high‐level language) this to
assembly (start with .section .data):
• A = 10
• B = [1, 2, 4, 10]
• …
• P = A
• Q = P+1
• …
• B[0] = P+Q
• B[1] = Q‐P+1
Writing assembly programs: Labels and
Memory
• Uses:
1. Use 1: Data Memory (address for variables)
2. Use 2: Program Memory (destinations for JMP/Branch)
Labels
Use 1: Data Memory (address for variables)
• (appears both in memory and data section in code. Why?)
Labels
Use 1: 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
Use 1: 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
• Use 2: Program Memory
• Destinations for JMP/Branch
• Subroutine calls
Recap why use branch vs JMP?
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 if I want to use registers?
Activity 4
Addressing modes
Addressing Modes
• What are they?
• How microprocessor accesses operands for 1 instruction
• May contain additional features (pre/post increment)
• Why have pre‐post increment as special instructions?
Addressing Modes
• What are they?
• How microprocessor accesses operands for 1 instruction
• May contain additional features (pre/post increment)
• Why have pre‐post increment as special instructions?
• Every microprocessor may have slightly different addressing mode
• Computer architecture decision
• Standard modes
• Case study: AVR
• 7 Addressing Modes
Understanding addressing modes
Understanding addressing modes
Understanding addressing modes
Easy right?
Understanding addressing modes
• Where are my operands
• How do I find the address of my operands
Understanding addressing modes
• Where are my operands
• How do I find the address of my operands
• ADD, SUBI, STS (r, label), LD (R, X/Y/Z)
Understanding addressing modes
• All we care about:
Addressing Mode 1: Register Direct
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Addressing Mode 2: Immediate
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Addressing Mode 3: Data Direct
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Addressing Mode 4: Data Indirect
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
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
Addressing Mode 4: Data Indirect
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Ldi R27, hi8(label)
Ldi r26, lo8(label)
X/Y/Z must be pre‐loaded with
correct address
Addressing Mode 4: Data Indirect
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Ldi R27, hi8(label)
Ldi r26, lo8(label)
X/Y/Z must be pre‐loaded with
correct address
Does order matter? How do I remember X/Y/Z?
Addressing Mode 5: Data Indirect (with Post‐
increment)
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Addressing Mode 6: Data Indirect (with Pre‐
increment)
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Addressing Mode 7: Data Indirect (with
Displacement)
• Sample Instruction:
• Where is the operand?
• Where is the address of the operand?
Writing Assembly Code
(thinking about what a compiler
does)
Advice
• Make correct code first
• Make efficient code after
Approach
1. Understand Syntax
2. Understand Semantics (meaning)
3. (Use Patterns)
• If using patterns, you must memorise
4. Create Variation of Pattern
5. Optimise
Approach
1. Understand Syntax
2. Understand Semantics (meaning)
3. (Use Patterns)/Give it a go!
• If using patterns, you must memorise
4. Create Variation of Pattern
5. Optimise
Simplifications
• All variables are integers unless stated otherwise
• All integers are 8 bits
Simplifications
• All variables are integers unless stated otherwise
• All integers are 8 bits
• Variables in higher level program are assumed to be stored in
data memory
• Labels identical to the variable names:
if (a == 3)
Implies:
.section .data
Simplifications
• All variables are integers unless stated otherwise
• All integers are 8 bits
• Variables in higher level program are assumed to be stored in
data memory
• Labels identical to the variable names:
if (a == 3)
Implies:
.section .data
a: .space 1
Basic Assembly
•Week 10 Activity 2
If/Else
• Syntax:
if (condition) {
} else {
}
then block
else block
If/Else
• Semantics:
• What does this mean? if (condition) {
} else {
}
then block
else block
If/Else
• Give it a go:
• Make correct code first
• Make efficient code after
if (condition) {
} else {
}
then block
else block
Activity 1
• Make correct code first
• Make efficient code after
• Do you use BREQ or BRNE
if (a==3) {
a++;
} else {
a–;
}