COMP3221: Microprocessors and Embedded Systems
description
Transcript of COMP3221: Microprocessors and Embedded Systems
![Page 1: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/1.jpg)
COMP3221: Microprocessors and Embedded Systems
Lecture 11: Assembly
http://www.cse.unsw.edu.au/~cs3221
Lecturer: Hui Wu
Session 2, 2005
![Page 2: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/2.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
2
Overview
• Pseudo Instructions
• Macro
• Assembly Process
![Page 3: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/3.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
3
Assembly Language Format
An input line takes one of the following forms :
[label:] directive [operands] [Comment] [label:] instruction [operands] [Comment] Comment Empty line
A comment has the following form:
; [Text]
Items placed in braces are optional. The text between the comment-delimiter (;) and the end of line (EOL) is ignored by the Assembler.
![Page 4: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/4.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
4
Memory Segments
• Different types of memory are known as segments to the assembler
• Assembler directives enable code/data to be placed into different segments
• AVR has Data segment (SRAM)
Can’t place data here, just reserve space (for variables)
Code segment (Flash) Can place code or constant data here
EEPROM Segment Can place constants here
![Page 5: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/5.jpg)
5
• From AVRStudio Help
• These arefor the AVRStudioAssembler
Directive Description
BYTE Reserve byte to a variable
CSEG Code Segment
CSEGSIZE Program memory size
DB Define constant byte(s)
DEF Define a symbolic name on a register
DEVICE Define which device to assemble for
DSEG Data Segment
DW Define Constant word(s)
ENDM, ENDMACRO End macro
EQU Set a symbol equal to an expression
ESEG EEPROM Segment
EXIT Exit from file
INCLUDE Read source from another file
LIST Turn listfile generation on
LISTMAC Turn Macro expansion in list file on
MACRO Begin macro
NOLIST Turn listfile generation off
ORG Set program origin
SET Set a symbol to an expression
Pseudo Instructions
![Page 6: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/6.jpg)
6
Pseudo Instructions
• .byte: Reserve space; only allowed in dseg
• Segment directives .cseg and .dseg allow the text and data segments to be built up in pieces:
.dseg
amount: .byte 2
.cseg
formula: inc r0
.dseg
count: .byte 2
.db: Initialize constant in code or EEPROM segment
.dw: As above but defines a 16-bit word
![Page 7: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/7.jpg)
7
Pseudo Instructions
• .def: Make a definition for registers only.def ZH=r31.def ZL=r30
• .device: Specify the exact processor that this program is designed for.device AT90S8515
Prohibits use of non-implemented instructions
• .macro, .endm: Begin and end macro definition• .include: Include a file• .exit: Stop processing this file
![Page 8: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/8.jpg)
8
Expressions
• Expressions can consist of operands, operators and functions. All expressions are internally 32 bits.
• Example: ldi r26, low(label + 0xff0)
Function Operands Operator
![Page 9: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/9.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
9
Operands
• User defined labels which are given the value of the location counter at the place they appear. • User defined variables defined by the SET directive • User defined constants defined by the EQU directive • Integer constants: constants can be given in several formats, including
Decimal (default): 10, 255 Hexadecimal (two notations): 0x0a, $0a, 0xff, $ff Binary: 0b00001010, 0b11111111 Octal (leading zero): 010, 077
• PC - the current value of the Program memory location counter.
![Page 10: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/10.jpg)
10
OperatorsSymbol Description! Logical Not~ Bitwise Not - Unary Minus* Multiplication/ Division+ Addition- Subtraction<< Shift left>> Shift right< Less than<= Less than or equal> Greater than>= Greater than or equal== Equal!= Not equal& Bitwise And^ Bitwise Xor| Bitwise Or&& Logical And|| Logical Or
Same meanings
as in c
![Page 11: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/11.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
11
Functions
• LOW(expression): Returns the low byte of an expression • HIGH(expression): Returns the second byte of an expression • BYTE2(expression): The same function as HIGH • BYTE3(expression): Returns the third byte of an expression • BYTE4(expression): Returns the fourth byte of an expression • LWRD(expression): Returns bits 0-15 of an expression • HWRD(expression): Returns bits 16-31 of an expression • PAGE(expression): Returns bits 16-21 of an expression • EXP2(expression): Returns 2 to the power of expression • LOG2(expression): Returns the integer part of log2(expression)
![Page 12: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/12.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
12
Functions (Cont.)
• Examples
cp r0, low(-13167) cpc r1, high(-13167) brlt case1
… case1: inc r10
…
![Page 13: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/13.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
13
Macros
• Assembler programmers often need to repeat sequences of instructions several times• Could just type them out – tedious• Could just copy and paste - then the specializations are often forgotten or wrong• Could use a subroutine, but then there is the overhead of the call and return instructions• Macros solve this problem• Consider code to swap two bytes in memory: lds r2, p lds r3, q sts q, r2 sts p, r3
![Page 14: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/14.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
14
Macros • Swapping p and q twice • Without macro
lds r2, plds r3, qsts q, r2sts p, r3
lds r2, plds r3, qsts q, r2sts p, r3
• With macro.macro myswap
lds r2, plds r3, qsts q, r2sts p, r3
.endmacro
myswapmyswap
![Page 15: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/15.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
15
• There are up to 10 parameters
Indicated by @0 to @9 in the macro body
@0 is the first parameter, @1 the second, and so on
• Other assemblers let you give meaningful names to parameters
AVR Macro Parameters
![Page 16: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/16.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
16
AVR Parameterised Macro
• Without macro lds r2, p
lds r3, qsts q, r2sts p, r3
lds r2, rlds r3, ssts s, r2sts r, r3
• With macro
.macro change
lds r2, @0
lds r3, @1
sts @1, r2
sts @0, r3
.endmacro
change p, q
change r, s
![Page 17: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/17.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
17
Another Example
• Subtract 16-bit immediate value from 16 bit number stored in two registers
.MACRO SUBI16 ; Start macro definition subi @1,low(@0) ; Subtract low byte sbci @2,high(@0) ; Subtract high byte .ENDMACRO ; End macro definition
.CSEG ; Start code segment SUBI16 0x1234,r16,r17 ; Sub.0x1234 from
; r17:r16• Useful for other 16-bit operations on an 8-bit processor
![Page 18: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/18.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
18
Two Pass Assembly Process
• We need to process the file twice
• Pass One
– Lexical and syntax analysis: checking for syntax errors
– Record all the symbols (labels etc) in a symbol table
– Expand macro calls
• Pass Two
– Use the symbol table to substitute the values for the symbols and evaluate functions.
– Assemble each instruction
• i.e. generate machine code
![Page 19: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/19.jpg)
19
An Example.include "m64def.inc"
.equ bound =5
.def counter =r17
.dseg
Cap_word:.byte 5
.cseg
rjmp start ; Interrupt vector tables starts at 0x00
.org 0x003E ; Program starts at 0x003E
Low_word: .db "hello“
start:
ldi zl, low(Low_word<<1) ; Get the low byte of the address of "h"
ldi zh, high(Low_word<<1) ; Get the high byte of the address of "h"
ldi yh, high(Cap_word)
ldi yl, low(Cap_word)
clr counter ; counter=0
![Page 20: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/20.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
20
An Example (Cont.)
main:
lpm r20, z+ ; Load a letter from flash memory
subi r20, 32 ; Convert it to the capital letter
st y+, r20 ; Store the capital letter in SRAM
inc counter
cpi counter, bound
brlt main
loop: nop
rjmp loop
![Page 21: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/21.jpg)
21
An Example (Cont.)• Pass 1: Lexical and syntax analysis
Symbol Value
bound 5
counter 17
Cap_word 0x0000
Low_word 0x003E
start 0x0041
main 0x0046
loop 0x004c
Symbol Table
![Page 22: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/22.jpg)
22
An Example (Cont.)
• Pass 2: code generation.
Program address Machine code Assembly code
0x00000000: C040 rjmp start …
0x0000003E: 6568 “he” ; Little endian 0x0000003F: 6C6C “ll” 0x00000040: 006F “o” 0x00000041: E7EC ldi zl, low(Low_word<<1) 0x00000042: E0F0 ldi zh, high(Low_word<<1) 0x00000043: E0D0 ldi yh, high(Cap_word) 0x00000044: E6C0 ldi yl, low(Cap_word) 0x00000045: 2711 clr counter
…
![Page 23: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/23.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
23
Absolute Assemblers
• The only source file contains all the source code of the program
• Programmers use .org to tell the assembler the starting address of a segment (data segment or code segment)
• Whenever any change is made in the source program, all code must be assembled.
• A downloader transfers an executable file (machine code) to the target system.
![Page 24: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/24.jpg)
24
Absolute Assemblers (Cont.)
Source file with location information (NAME.ASM)
Absolute assembler
Executable file (NAME.EXE)
Loader Program
Computer memory
Absolute Assembler Operation
![Page 25: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/25.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
25
Relocatable Assemblers
• The program may be split into multiple source files
• Each source file can be assembled separately
• Each file is assembled into an object file where some addresses may not be resolved
• A linker program is needed to resolve all unresolved addresses and make all object files into a single executable file
![Page 26: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/26.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
26
Relocatable Assemblers (Cont.)
Source file 1 (MODULE1.ASM
Source file 2 (MODULE2.ASM
Relocatable assembler
Relocatable assembler
Object file1 (MODULE1.OBJ
Object file2 (MODULE2.OBJ
![Page 27: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/27.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
27
Linker
• Takes all object files and links them together and locates all addresses
• Works together with relocatable assembler
![Page 28: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/28.jpg)
28
Linker (Cont.)
Source file 1 (MODULE1.ASM
Source file 2 (MODULE1.ASM
Relocatable assembler
Relocatable assembler
Object file1 (MODULE1.OBJ
Object file2 (MODULE2.OBJ
Linker program
Library of object files (FILE.LIB)
Executable file (NAME.EXE)
Code and data location
information
![Page 29: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/29.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
29
Loader
• Puts an executable file into the memory of the computer.
• May take many forms.
Part of an operating system.
A downloader program that takes an executable file created on one computer and puts it into the target system.
A system that burns a programmable read-only memory (ROM).
![Page 30: COMP3221: Microprocessors and Embedded Systems](https://reader035.fdocuments.us/reader035/viewer/2022081513/56814e2f550346895dbb9487/html5/thumbnails/30.jpg)
COMP3221/9221: Microprocessors and Embedded Systems
30
Reading
1. Chap. 5. Microcontrollers and Microcomputers
2. User’s guide to AVR assembler
– This guide is a part of the on-line documentations accompanied with AVR Studio. Click help in AVR Studio.