300 likes | 424 Views
COMP3221: Microprocessors and Embedded Systems. Lecture 11: Assembly http://www.cse.unsw.edu.au/~cs3221 Lecturer: Hui Wu Session 2, 2005. Overview. Pseudo Instructions Macro Assembly Process. Assembly Language Format. An input line takes one of the following forms :
E N D
COMP3221: Microprocessors and Embedded Systems Lecture 11: Assembly http://www.cse.unsw.edu.au/~cs3221 Lecturer: Hui Wu Session 2, 2005
Overview • Pseudo Instructions • Macro • Assembly Process COMP3221/9221: Microprocessors and Embedded Systems
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. COMP3221/9221: Microprocessors and Embedded Systems
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 COMP3221/9221: Microprocessors and Embedded Systems
Pseudo Instructions • From AVRStudio Help • These arefor the AVRStudioAssembler
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
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
Expressions • Expressions can consist of operands,operators and functions. All expressions are internally 32 bits. • Example: • ldi r26, low(label + 0xff0) • Function Operands Operator
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. COMP3221/9221: Microprocessors and Embedded Systems
Symbol 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 Operators Same meanings as in c
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) COMP3221/9221: Microprocessors and Embedded Systems
Functions (Cont.) • Examples • cp r0, low(-13167) • cpc r1, high(-13167) • brlt case1 • … • case1: inc r10 • … COMP3221/9221: Microprocessors and Embedded Systems
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 COMP3221/9221: Microprocessors and Embedded Systems
Macros • With macro • .macro myswap • lds r2, p • lds r3, q • sts q, r2 • sts p, r3 • .endmacro • myswap • myswap • Swapping p and q twice • Without macro • lds r2, p • lds r3, q • sts q, r2 • sts p, r3 • lds r2, p • lds r3, q • sts q, r2 • sts p, r3 COMP3221/9221: Microprocessors and Embedded Systems
AVR Macro Parameters • 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 COMP3221/9221: Microprocessors and Embedded Systems
AVR Parameterised Macro • Without macro • lds r2, p • lds r3, q • sts q, r2 • sts p, r3 • lds r2, r • lds r3, s • sts s, r2 • sts 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 COMP3221/9221: Microprocessors and Embedded Systems
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 COMP3221/9221: Microprocessors and Embedded Systems
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 COMP3221/9221: Microprocessors and Embedded Systems
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
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 COMP3221/9221: Microprocessors and Embedded Systems
An Example (Cont.) • Pass 1: Lexical and syntax analysis Symbol Table
An Example (Cont.) • Pass 2: code generation. • Program address Machine code Assembly code • 0x00000000: C040rjmp 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 • …
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. COMP3221/9221: Microprocessors and Embedded Systems
Absolute Assemblers (Cont.) Source file with location information (NAME.ASM) Absolute assembler Absolute Assembler Operation Executable file (NAME.EXE) Loader Program Computer memory
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 COMP3221/9221: Microprocessors and Embedded Systems
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 COMP3221/9221: Microprocessors and Embedded Systems
Linker • Takes all object files and links them together and locates all addresses • Works together with relocatable assembler COMP3221/9221: Microprocessors and Embedded Systems
Linker (Cont.) Source file 1 (MODULE1.ASM Source file 2 (MODULE1.ASM Relocatable assembler Relocatable assembler Object file1 (MODULE1.OBJ Library of object files (FILE.LIB) Object file2 (MODULE2.OBJ Code and data location information Linker program Executable file (NAME.EXE)
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). COMP3221/9221: Microprocessors and Embedded Systems
Reading • Chap. 5. Microcontrollers and Microcomputers • 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. COMP3221/9221: Microprocessors and Embedded Systems