Embedded web technology makes it possibl

nazirloan 12 views 54 slides Sep 04, 2024
Slide 1
Slide 1 of 54
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54

About This Presentation

very good tutorial


Slide Content

School of Electrical, Computer and Telecommunications Engineering
University of Wollongong
Australia
ECTE333
Lecture 09:
Assembly Language Programming
Lam Phung, Joshua Thompson, Montse Ros

2/54ECTE333
Lecture 9’s sequence
9.1 Introduction
9.2 Atmel Studio for Assembly Programming
9.3 Elements of an Assembly Program
9.4 Example Assembly Program
Appendix: Addressing Schemes in AVR

3/54ECTE333
9.1 Introduction
Overview
Explore why assembly is useful compared to high-level languages.
Create an example assembly program.
Link AVR instruction set to AVR assembly language.
Learn the principles of assemblyprogramming.
Studyanexample assemblyprogram.

4/54ECTE333
Why Learn Assembler?
Assembly is a low-level programming language: Assembly allows for
instruction-level code optimization. Very useful for resource
constrained situations.
Required for the development of a new micro-chip:All chips need an
assembly instruction set for C code or higher to compile to.
Enables writing of a custom C compiler: Often required when a new
microcontroller chip is released.
Assembly is the bridge between C instructions and hardware
implementations: Maps C instructions such as for-loop and if-else to
the control-unit’s instructions.

5/54ECTE333
Assembly in AVR Programing
C code is compiled into
assembly code.
Assembly is converted to binary
commands.
Control unit converts these
commands into signals.
These signals tell microcontroller
components what to do.

6/54ECTE333
Lecture 9’s sequence
9.1 Introduction
9.2 Atmel Studio for Assembly Programming
9.3 Elements of an Assembly Program
9.4 Example Assembly Program
Appendix: Addressing Schemes in AVR

7/54ECTE333
9.2 Creating an Assembly Program
Step 1: Creating Atmel Studio project
Choose project name
Choose project type: Assembler
Select device
Step 2: Entering an Assembly program.
Step 3: Compiling the Assembly program to produce a HEX file.
Step 4: Downloading/testing the HEX file on Atmel AVR microcontroller.
We’ll illustrate these steps using an example.

8/54ECTE333
Example Program Definition
Read the switches on PORT A and output to the LEDs on PORT B.

9/54ECTE333
Step 1: Creating Atmel Studio Project
Start the Atmel Studio 7.
Select menu File| New Project…
project type: Assembler -Assembler Project
project name: Led
project location: /your directory
option ‘Create directory for solution’
Click ‘OK’.

10/54ECTE333
Creating Atmel Studio Project

11/54ECTE333
Creating Atmel Studio Project

12/54ECTE333
Creating Atmel Studio Project

13/54ECTE333
Step 2: Entering an Assembler Program
program
main.asm
Status
messages
Project
files

14/54ECTE333
Entering an Assembly Program
; File: Led.asm
.include"m16def.inc"; include the ATmega16 definition file.
.deftemp = r16 ; define temporary register
; Run reset to initialise the ports (good practice)
RESET:
lditemp, 0x00 ; Initialise our temporary register to all 0’s.
outDDRA, temp ; Set Port A to inputs using the temp register.
lditemp, 0xFF ; Set the temp register to all 1’s.
outDDRB, temp ; Set Port B to output.
; This is the main loop
LOOP:
intemp, PINA ; Read in from PORTA's input pins
comtemp ; Invert value for active -low LEDs
outPORTB, temp ; Write out to PORTB
rjmpLOOP ; Jump back to LOOP

15/54ECTE333
Step 3: Compiling the Assembly Program

16/54ECTE333
Step 4: Downloading/Testing on Microcontroller
Hardware setup for example program.
Connections to PORTA & PORTB are only for this example.
PORTB to LEDs
PORTA to
switches
ATmega16
chip
Power switch
12-V power
supply
Serial cable
to PC
Programming
mode

17/54ECTE333
Downloading/Testing on Microcontroller
Select menu Tools| Add target.
Select the correct serial port.
Click ‘Apply’.

18/54ECTE333
Downloading/Testing on Microcontroller
Select ‘Tool’ = STK500, ‘Device’ = ATmega16, and ‘Interface’= ISP

19/54ECTE333
Downloading/Testing on Microcontroller
Select HEX
file
In ‘Memories’ tab, select HEX file (created in Step 3) & click ‘Program’.
After this, the program is run on the STK500 board.
The program remains even after power-off. To erase, click ‘Erase Now’.

20/54ECTE333
Lecture 9’s sequence
9.1 Introduction
9.2 Atmel Studio for Assembly Programming
9.3 Elements of an Assembly Program
9.4 Example Assembly Program
Appendix: Addressing Schemes in AVR

21/54ECTE333
9.3 Elements of an Assembly Program
An Assembly program is different to a C program! Its structure
resembles the machine code executed by the AVR control unit.
A typical assembly program has the following structure:
.include to add any header files or chip definitions.
.def to define any “global variables”.
RESET: to initialise registers and ports.
(can have any name, e.g. ‘SETUP:’).
LOOP: to create the main loop (any name).
FUNC: Any function or subroutine that you define.

22/54ECTE333
Anatomy of an Assembly Program
; Your comments here.
.include"m16def.inc“
.deftemp = r16
RESET:
lditemp, 0x00 ; Initialise our temporary register.
outDDRA, temp ; Set Port A to inputs using temp.
lditemp, 0xFF ; Set the temp register to all 1’s.
outDDRB, temp ; Set Port B to output.
; This is the main loop
LOOP:
intemp, PINA ; Read in from PORTA's input pins
comtemp ; Invert value for active -low LEDs
outPORTB, temp ; Write out to PORTB
rjmpLOOP ; Jump back to LOOP
Your top comments.
Include the AVR definition file.
Initialise all your
registers and ports.
“Declaring your
variables in C”
Create your main
loop to run your
program in.
Define your global variables.

23/54ECTE333
AVR Assembly Instructions
Tells the control unit what operation to perform: Add, subtract,
load from a register, move data from one location to another, etc.
Selects registers or memory locations to load or save to.
Sends the ALU its opcode describing the operation to perform –
Add, Sub, And, Or, etc.
Points to different locations in your compiled code.
ldi, out, in, com, rjmpare all AVR assembly instructions.
What does an AVR Assembly instruction do?

24/54ECTE333
AVR Instruction Set Information
AVR datasheet: Example C and Assembly code for various ports
and registers.
http://ww1.microchip.com/downloads/en/devicedoc/doc2466.pdf
AVR instruction set manual: Various instructions and addressing
modes in AVR chips.
http://ww1.microchip.com/downloads/en/devicedoc/atmel-0856-
avr-instruction-set-manual.pdf
Example instruction from instruction set manual: Add

25/54ECTE333
Instruction Format
The general format is:
Label: opcode operand
1, operand
2, … ;comment
Label: A position in the program memory.
Opcode: The name of the operation/instruction to perform.
Operands: The registers, ports, etc., to perform the operation on.
Comment: Optional explanation about the code.
Operand order for AVR:
opCoderDestination, rSource; data flows r1←r2

26/54ECTE333
AVR Instruction Types
The AVR instruction set has 4 broad categories:
Data transfer: Loading/storage of data between registers/memory.
Data Processing: Arithmetic and Logical.
Input/Output (I/O): Writing data to and from various ports or pins.
Program flow: Function calls, return statements, stack operation.

27/54ECTE333
AVR Operand Types
Operand values can come from anywhere in the data path.
They may be register values, immediate values, or memory locations.
Register: General purpose register or special register.
addr16, r17
Memory Reference: Location in external memory.
sts0x0060, r16; 0x0060 is a 16-bit memory location
Immediate: Constant value.
lditemp, 0x00; 0x00 is an 8-bit constant
Word value: 16-bit value (AVR is 8-bit)
adiwr25:24, 1; [r25:r24] is two 8-bit registers
; to form a 16-bit value

28/54ECTE333
Data Transfer Instructions
Move data around the data path, to and from memory and between
registers.
Specify the source, destination, and amount of data.
Load a value into a register to initialise a variable.
Move data between registers, including copying data.
Table 1:Example data transfer instructions in AVR Atmega16.

29/54ECTE333
Data Transfer Instructions: LDI
Example: ldir16, 0x54
The Load-Immediate instruction loads a constant value into a register.
Opcode is 1110.
The first operand uses register-directaddressing:
Register Rd is specified as r16 = 1 0000
The second operand uses immediateaddressing:
0x54 = 01010100in binary
Full machine code: 11100101 -0000 0100(binary),
E5 -04 (hex)

30/54ECTE333
Data Transfer Instructions: STS
Example: sts0x0204, r16
The Store Direct to SRAM instruction stores a value in a register to a
location in the internal SRAM.
Opcode is 1001-001.
First operand uses register-directaddressing (r16 = 1-0000)
The second operand uses data-directaddressing
SRAM address 0x0204 is 0000-0010-0000-0100in binary.
Full machine code: 1001-0011-0000-0000
0000-0010-0000-0100

31/54ECTE333
Data Processing Instructions
Two main categories of data processing in AVR
Arithmetic:
Add, Subtract, Multiply, Divide
Signed Integer
Floating point
Increment, Decrement, Negate
Logical:
Bitwise operations
AND, OR, NOT
Shift and rotate operations: (left/right)

32/54ECTE333
Data Processing Instructions: ADD
Example: add r16, r17
ADD instruction adds two-operands.
Opcode is 0000-11.
Both operands use register-directaddressing.
Register Rddefined as r16 = 1-0000.
Register Rrdefined as r17 = 1-0001.
Machine code: 0000-1111-0000-0001(binary), or 0F-01 (hex).

33/54ECTE333
Input/Output Instructions
IO instructions specify reading or writing operations to IO ports.
Specific instructions: in/ out.
Data movement instructions (memory mapped).
Instructions for a separate controller (DMA).

34/54ECTE333
Input/Output Instructions: IN
Example: in r16, pina
Reads from an I/O port and stores result in a register.
Opcode is 1011-0.
Register Rddefined as r16 = 1-0000
Register Rris PINA defined in the definition file "m16def.inc"

35/54ECTE333
Program Flow Instructions
Branch/jump to a new point in the program, e.g.
Branch to x if the result is zero.
Skip an instruction or go to a specific instruction, e.g.
Increment and skip if zero,
Branch always to x.
Subroutine calls, including
Interrupt service routine call,
Function call.

36/54ECTE333
Program Flow Instructions: RJMP
Example: rjmpLOOP
Relative Jump instruction jumps to the location called LOOP in the program
memory.
Opcode is 1100.
This is an unconditional jump.
The operand is a displacement in: [+2047 (forward), -2048 (backwards)].
E.g.,LOOP is located 1000 instructions backwards.

37/54ECTE333
Lecture 9’s sequence
9.1 Introduction
9.2 Atmel Studio for Assembly Programming
9.3 Elements of an Assembly Program
9.4 Example Assembly Program
Appendix: Addressing Schemes in AVR

38/54ECTE333
9.4 Example Assembly Program
; Read from switches on PORT A and output to the LEDs on PORT B .
.include"m16def.inc“ ; ATmega16 definition file.
.deftemp = r16 ; Define temporary register
RESET:
lditemp, 0x00 ; Initialise our temporary register.
outDDRA, temp ; Set Port A to inputs using temp.
lditemp, 0xFF ; Set the temp register to all 1’s.
outDDRB, temp ; Set Port B to output.
LOOP:
intemp, PINA ; Read in from PORTA's input pins
comtemp ; Invert the value for active -low LEDs.
outPORTB, temp ; Write out to PORTB
rjmpLOOP ; Jump back to LOOP
Our example program

39/54ECTE333
Atmel Studio version 7 onwards automatically includes the correct
header file.
This header file maps register names to addresses, e.g. r16 to $10.
We will use register r16 to store temporary values.
Program Header and Global Variables
.include"m16def.inc"; header file for ATmega16 inserted by Studio 7.
.deftemp = r16 ; define temporary register

40/54ECTE333
The AVR definition file also defines Data Direction Registers,
DDRA and DDRB.
We useldito load the temporary register to 0’s for inputs, and
1’s for outputs,
We then send temp to DDRA and DDRB using out.
Initialising AVR Ports
; Run reset to initialise the ports.
RESET:
; Set Data Direction Registers DDRA & DDRB: 0 = input, 1 = output
lditemp, 0x00 ; Initialise our temp register to all 0's
outDDRA, temp ; Set Port A to inputs
lditemp, 0xFF ; Initialise our temp register to all 1's
outDDRB, temp ; Set Port B to outputs .

41/54ECTE333
Use into read the port values on PINA.
Invert the result using comfor ‘complement’.
Use outto output to PORTB.
Use rjmpto jump back to the instruction pointed to by the
“LOOP” label. This is your first loop in Assembly!
Reading and Writing Between Ports
; This is our continuous loop similar to while (1){…} in C.
LOOP:
intemp, PINA ; Read in from PORTA's input pins
comtemp ; Complement for active low LEDs
outPORTB, temp; Write data to PORTB
rjmpLOOP ; Jump back to LOOP

42/54ECTE333
Lecture 9’s summary
Introduced assembly and its purpose.
Demonstrated the creation of an assembly project.
Learnt the anatomy of a basic assembly program.
Explored AVR instructions and looked at some relevant examples.
Looked into AVR operand types.
Studied in depth an example program.
Additional information provided in the appendix.

43/54ECTE333
Lecture 9’s sequence
9.1 Introduction
9.2 Atmel Studio for Assembly Programming
9.3 Elements of an Assembly Program
9.4 Example Assembly Program
Appendix: Addressing Schemes in AVR

44/54ECTE333
Appendix: Addressing Schemes in AVR
The General-Purpose Register File in AVR
The general-purpose registers from the basic RISC structure.
Storage for “in use” data. Faster than loading from external memory.

45/54ECTE333
Data and Register Addressing in AVR
Register Direct Addressing: with 1 or 2 registers
I/O Direct Addressing
Immediate Addressing
Data Direct Addressing
Data Indirect Addressing
with Post-increment
with Pre-decrement
with Displacement
Program Memory Constant Addressing
with Post-Increment

46/54ECTE333
Register Direct Addressing
Directly access a register in the 32-byte on-chip Register File.
5-bit field Rd contains the register ‘number’.
Example: incr5

47/54ECTE333
Register-Direct (two registers)
Using a source and destination register to access two registers
Example: addr16, r17

48/54ECTE333
I/O Direct Addressing
Similar to Register-direct, however the memory space used is the
I/O space not the register file.
Example: out PORTB, r9

49/54ECTE333
Immediate Addressing
The value in the field of the instruction isthe operand value. No
need to seek the value from registers or memory or elsewhere.
Example: ldir16, 0x34

50/54ECTE333
Data Direct Addressing
When exact memory address is known.
Instruction uses two 16-bit words, the second word is the data
address.
Example: sts0x0060, r16

51/54ECTE333
Data Indirect Addressing
Based on index registers X, Y or Z.
16-bit register contains the memory location of the actual operand.
Example: ldr0, X

52/54ECTE333
Data Indirect with Post-Increment
Based on index registers X, Y or Z.
16-bit register contains the memory location of the actual operand
which is incremented by 1.
Example: ldr0, X+
Also available in a Pre-Decrement version
+1

53/54ECTE333
Data Indirect with Displacement
Based on index registers Y or Z.
16-bit register contains the memory location of the actual operand
onto which an offset value is added.
Commonly known as indexed addressing.
Very useful for accessing arrays.
Example: lddr0, Y+q
q = offset

54/54ECTE333
Program Memory Addressing
Program Memory Constant Addressing
Using the Z-register to point to the Program Address
LPM, ELPM & SPM instructions
Program Memory Addressing with Post-increment
Similar to previous example, with post increment
LPM Z+ and ELPM Z+ instructions
Direct Program Addressing
32-bit instruction to select a specific program memory address
JMP & CALL instructions
Indirect Program Addressing
Z-register is copied to the PC, and execution begins from there
IJMP & ICALL instructions
Tags