Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP3221: Microprocessors and Embedded Systems Lecture 11: Assembly Lecturer: Hui Wu Session 2, 2004.

Similar presentations


Presentation on theme: "COMP3221: Microprocessors and Embedded Systems Lecture 11: Assembly Lecturer: Hui Wu Session 2, 2004."— Presentation transcript:

1 COMP3221: Microprocessors and Embedded Systems Lecture 11: Assembly http://www.cse.unsw.edu.au/~cs3221 Lecturer: Hui Wu Session 2, 2004

2 COMP3221/9221: Microprocessors and Embedded Systems 2 Overview Pseudo Instructions Macro Assembly Process

3 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.

4 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

5 5 From AVR Studio Help These are for the AVR Studio Assembler DirectiveDescription BYTEReserve byte to a variable CSEGCode Segment CSEGSIZEProgram memory size DBDefine constant byte(s) DEFDefine a symbolic name on a register DEVICEDefine which device to assemble for DSEGData Segment DWDefine Constant word(s) ENDM, ENDMACROEnd macro EQUSet a symbol equal to an expression ESEGEEPROM Segment EXITExit from file INCLUDERead source from another file LISTTurn listfile generation on LISTMACTurn Macro expansion in list file on MACROBegin macro NOLISTTurn listfile generation off ORGSet program origin SETSet a symbol to an expression Pseudo Instructions

6 6.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

7 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.deviceAT90S8515 Prohibits use of non-implemented instructions.macro,.endm: Begin and end macro definition.include: Include a file.exit: Stop processing this file

8 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

9 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 operators

10 10 Operators Symb ol 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

11 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)

12 COMP3221/9221: Microprocessors and Embedded Systems 12 Functions (Cont.) Examples cp r0, low(-13167) cpc r1, high(-13167) brlt case1 case1: inc r10 …

13 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

14 COMP3221/9221: Microprocessors and Embedded Systems 14 Macros 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 With macro.macromyswap lds r2, p lds r3, q sts q, r2 sts p, r3.endmacro myswap

15 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

16 COMP3221/9221: Microprocessors and Embedded Systems 16 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.macrochange lds r2, @0 lds r3, @1 sts @1, r2 sts @0, r3.endmacro change p, q change r, s

17 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

18 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

19 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

20 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

21 21 An Example (Cont.) Pass 1: Lexical and syntax analysis Symbol Value bound 5 counter17 Cap_word0x0000 Low_word0x003E start0x0041 main0x0046 loop0x004c Symbol Table

22 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 …

23 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.

24 24 Absolute Assemblers (Cont.) Source file with location information (NAME.ASM) Absolute assembler Executable file (NAME.EXE) Loader Program Computer memory Absolute Assembler Operation

25 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

26 COMP3221/9221: Microprocessors and Embedded Systems 26 Relocatable Assemblers (Cont.) Source file 1 (MODULE1.ASM Source file 2 (MODULE2.ASM Relocatable assembler Object file1 (MODULE1.OBJ Object file2 (MODULE2.OBJ

27 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

28 28 Linker (Cont.) Source file 1 (MODULE1.ASM Source file 2 (MODULE1.ASM 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

29 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).

30 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.


Download ppt "COMP3221: Microprocessors and Embedded Systems Lecture 11: Assembly Lecturer: Hui Wu Session 2, 2004."

Similar presentations


Ads by Google