P. 1
doc1022.pdf

doc1022.pdf

|Views: 2|Likes:
Published by Juanma Becerra

More info:

Published by: Juanma Becerra on Jul 08, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

07/08/2013

pdf

text

original

Section 4

AVR Assembler User Guide

4.1

Introduction

Welcome to the Atmel AVR Assembler. This manual describes the usage of the Assembler. The Assembler covers the whole range of microcontrollers in the AT90S family. The Assembler translates assembly source code into object code. The generated object code can be used as input to a simulator or an emulator such as the Atmel AVR In-Circuit Emulator. The Assembler also generates a PROMable code and an optional EEPROM file which can be programmed directly into the program memory and EEPROM memory of an AVR microcontroller. The Assembler generates fixed code allocations, consequently no linking is necessary. The Assembler runs under Microsoft Windows 3.11, Microsoft Windows95 and Microsoft Windows NT. In addition, there is an MS-DOS command line version. The Windows version of the program contains an on-line help function covering most of this document. The instruction set of the AVR family of microcontrollers is only briefly described, refer to the AVR Data Book (also available on CD-ROM) in order to get more detailed knowledge of the instruction set for the different microcontrollers. To get quickly started, the Quick-Start Tutorial is an easy way to get familiar with the Atmel AVR Assembler.

Development Tools User Guide

4-1
Rev. 1022A-A–01/98

AVR Assembler User Guide

4.2
4.2.1

Assembler Quick This tutorial assumes that the AVR Assembler and all program files that come with it are properly installed on your computer. Please refer to the installation instructions Start Tutorial
Getting Started Start the AVR Assembler. By selecting “File → Open” from the menu or by clicking on the toolbar, open the file “tutor1.asm”. This loads the assembly file into the Editor window. Read the program header and take a look at the program but do not make any changes yet. Once you have had a look at the program, select Assemble from the menu. A second window (the Message window) appears, containing a lot of error messages. This window will overlap the editor window, so it is a good idea to clean up your work space on the screen. Select the Editor window containing the program code, and select “Window → Tile Horizontal” from the menu. It is useful to have the Editor window larger than the Message window, so move the top of the Message window down a bit, and follow with the bottom of the Editor window. Your screen should look like this:

4.2.2

Assembling Your First File

4-2

Development Tools User Guide

which happens to be the variable we just corrected. Notice that in the Editor window.asm” tries to assign a name to register 32. tells that something is wrong with the arguments following the compare (“cp”) instruction. To get any further. One down.AVR Assembler User Guide 4. Now click on the next error in the list.2. By clicking along on the remaining errors. Notice that the register named “BH” is one of the arguments. Development Tools User Guide 4-3 . the errors must be found and corrected. a red vertical bar is displayed all over line 54. five to go. double click on any error (to activate the Editor window) or click inside the Editor window before you assemble once more. it appears that the first error generated all the messages. it seems that you have attempted to assemble a program with lots of bugs.2. and “tutor1.4 Reassembling To find out whether all errors have been corrected.3 Finding and Correcting Errors From the looks of the Message window. Point to the first error message in the Message window (the one reported to be on line 54) and press the left mouse button. Double click on the error message in the Message window and observe that the Editor window becomes active while the cursor is positioned at the start of the line containing the error. the Message window will tell that you are crowned with success. That is true since the AVR has exactly 32 General Purpose working registers numbered R0-R31. See the figure below. The error message says that only registers R0 to R31 can be assigned variable names. The message “Illegal argument type or count”. If you have done it all right up till now. 4. Correct the mistake by changing “r32” to “r19” in the Editor window.

instructions and directives are described in more detail later. Code lines should be limited to 120 characters. Set var1 to 100 (Directive) . Every input line can be preceded by a label.AVR Assembler User Guide 4. Labels are used as targets for jump and branch instructions and as variable names in Program memory and RAM. The text between the comment-delimiter (.) and the end of line (EOL) is ignored by the Assembler. comments or instructions.EQU var1=100 . [Text] Items placed in braces are optional. Infinite loop (Instruction) . Empty line A comment has the following form: . Another comment line Note: There are no restrictions with respect to column placement of labels. directives. labels and directives. Examples: label: test: . Comment 4.3 Assembler source The Assembler works on source files containing instruction mnemonics. [label:] instruction [operands] [Comment] 3. Set var2 to 200 . The instruction mnemonics and the directives often take operands. [label:] directive [operands] [Comment] 2. Pure comment line . 4-4 Development Tools User Guide . An input line may take one of the four following forms: 1. Labels. which is an alphanumeric string terminated by a colon.EQU var2=200 rjmp test .

H Z.K Rd ← Rd .V Z.V. refer to the AVR Data Book.V.V Z.Rr .V Z.N.V.V Z. Rr Rd. K Rd.H Z. K Rd.N.N.C.V. Not available in base-line microcontrollers Development Tools User Guide 4-5 .C.V Z.H Z.V Z. A summary of the instruction set mnemonics and their parameters is given here.N.N.K Rd.N.N. K Rd.Rr Add without Carry Add with Carry Add Immediate to Word Subtract without Carry Subtract Immediate Subtract with Carry Subtract Immediate with Carry Subtract Immediate from Word Logical AND Logical AND with Immediate Logical OR Logical OR with Immediate Exclusive OR One's Complement Two's Complement Set Bit(s) in Register Clear Bit(s) in Register Increment Decrement Test for Zero or Minus Clear Register Set Register Multiply Unsigned Rd ← Rd + Rr Rd ← Rd + Rr + C Rd+1:Rd ← Rd+1:Rd + K Rd ← Rd .N. K Rd.C.C.H Z.C. K Rd.V Z.K Rd Rd Rd Rd Rd Rd.1 Rd ← Rd • Rd Rd ← Rd ⊕ Rd Rd ← $FF R1. Rr Rd.C.V Z.H Z. K Rd.V Z.Rr Rd ← Rd .N.V. #Clock Note Mnemonics Operands Description Operation Flags ARITHMETIC AND LOGIC INSTRUCTIONS ADD ADC ADIW SUB SUBI SBC SBCI SBIW AND ANDI OR ORI EOR COM NEG SBR CBR INC DEC TST CLR SER MUL Rd.V Z.AVR Assembler User Guide 4.N.C. Rr Rd.N.N.C Rd ← Rd .C.H Z.V Z. Rr Rd.H Z.C.V Z.V None C 1 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 (1) Note: 1.N. R0 ← Rd × Rr Z.V. Rr Rd.C Rd+1:Rd ← Rd+1:Rd .N.N.Rd Rd ← $00 .K Rd ← Rd • Rr Rd ← Rd • K Rd ← Rd v Rr Rd ← Rd v K Rd ← Rd ⊕ Rr Rd ← $FF . For a detailed description of the Instruction set.N.N. Rr Rd.N.K) Rd ← Rd + 1 Rd ← Rd .N.V.4 Instruction mnemonics The Assembler accepts mnemonic instructions from the instruction set.N.C.Rd Rd ← Rd v K Rd ← Rd • ($FFh .K .N.V Z. Rr Rd Rd Rd.

V.H None None None None None None None None None None None None None None None None None None None None None None None None 2 2 3 3 3 4 4 4 1/2/3 1 1 1 1/2/3 1/2/3 1/2/3 1/2/3 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 4-6 Development Tools User Guide .Rr Rd.Rr . b P.V.N.Rr Rd.N. Signed Branch if Half Carry Flag Set Branch if Half Carry Flag Cleared Branch if T Flag Set Branch if T Flag Cleared Branch if Overflow Flag is Set Branch if Overflow Flag is Cleared Branch if Interrupt Enabled Branch if Interrupt Disabled PC ← PC + k + 1 PC ← Z PC ← k PC ← PC + k + 1 PC ← Z PC ← k PC ← STACK PC ← STACK if (Rd = Rr) PC ← PC + 2 or 3 Rd . k k k k k k k k k k k k k k k k k k k k k k k Relative Jump Indirect Jump to (Z) Jump Relative Call Subroutine Indirect Call to (Z) Call Subroutine Subroutine Return Interrupt Return Compare.C. k s.C.Rr Rd.b)=1) PC ← PC + 2 or 3 if (SREG(s) = 1) then PC ← PC+k + 1 if (SREG(s) = 0) then PC ← PC+k + 1 if (Z = 1) then PC ← PC + k + 1 if (Z = 0) then PC ← PC + k + 1 if (C = 1) then PC ← PC + k + 1 if (C = 0) then PC ← PC + k + 1 if (C = 0) then PC ← PC + k + 1 if (C = 1) then PC ← PC + k + 1 if (N = 1) then PC ← PC + k + 1 if (N = 0) then PC ← PC + k + 1 if (N ⊕ V= 0) then PC ← PC+ k + 1 if (N ⊕ V= 1) then PC ← PC + k + 1 if (H = 1) then PC ← PC + k + 1 if (H = 0) then PC ← PC + k + 1 if (T = 1) then PC ← PC + k + 1 if (T = 0) then PC ← PC + k + 1 if (V = 1) then PC ← PC + k + 1 if (V = 0) then PC ← PC + k + 1 if (I = 1) then PC ← PC + k + 1 if (I = 0) then PC ← PC + k + 1 None None None None None None None I None Z.V. b P.H Z.H Z.b)=0) PC ← PC + 2 or 3 if(I/O(P. b s.K if (Rr(b)=0) PC ← PC + 2 or 3 if (Rr(b)=1) PC ← PC + 2 or 3 if(I/O(P.Rr Rd . Skip if Equal Compare Compare with Carry Compare with Immediate Skip if Bit in Register Cleared Skip if Bit in Register Set Skip if Bit in I/O Register Cleared Skip if Bit in I/O Register Set Branch if Status Flag Set Branch if Status Flag Cleared Branch if Equal Branch if Not Equal Branch if Carry Set Branch if Carry Cleared Branch if Same or Higher Branch if Lower Branch if Minus Branch if Plus Branch if Greater or Equal.N.C.AVR Assembler User Guide Mnemonics Operands Description Operation Flags #Clock Note BRANCH INSTRUCTIONS RJMP IJMP JMP RCALL ICALL CALL RET RETI CPSE CP CPC CPI SBRC SBRS SBIC SBIS BRBS BRBC BREQ BRNE BRCS BRCC BRSH BRLO BRMI BRPL BRGE BRLT BRHS BRHC BRTS BRTC BRVS BRVC BRIE BRID Rd. b Rr. Signed Branch if Less Than.K Rr.C Rd .

Rr -X. Z+q k. Rd ← (Z) Rd ← (Z + q) (k) ← Rr (X) ← Rr (X) ← Rr. Z ← Z + 1 Z ← Z . Z ← Z+1 Z ← Z . Rr X+. X Rd.1. X ← X + 1 X ← X .AVR Assembler User Guide #Clock Note Mnemonics Operands Description Operation Flags DATA TRANSFER INSTRUCTIONS MOV LDI LDS LD LD LD LD LD LD LDD LD LD LD LDD STS ST ST ST ST ST ST STD ST ST ST STD LPM IN OUT PUSH POP Rd.1.1. K Rd. Rr -Y.Rr Copy Register Load Immediate Load Direct from SRAM Load Indirect Load Indirect and Post-Increment Load Indirect and Pre-Decrement Load Indirect Load Indirect and Post-Increment Load Indirect and Pre-Decrement Load Indirect with Displacement Load Indirect Load Indirect and Post-Increment Load Indirect and Pre-Decrement Load Indirect with Displacement Store Direct to SRAM Store Indirect Store Indirect and Post-Increment Store Indirect and Pre-Decrement Store Indirect Store Indirect and Post-Increment Store Indirect and Pre-Decrement Store Indirect with Displacement Store Indirect Store Indirect and Post-Increment Store Indirect and Pre-Decrement Store Indirect with Displacement Load Program Memory In Port Out Port Push Register on Stack Pop Register from Stack Rd ← Rr Rd ← K Rd ← (k) Rd ← (X) Rd ← (X). Y ← Y + 1 Y ← Y . Rr Z+q.Rr Z. Rd ← (X) Rd ← (Y) Rd ← (Y). Rr -Z. Rd ← (Y) Rd ← (Y + q) Rd ← (Z) Rd ← (Z). Y Rd. -Y Rd.1. Rr Rr Rd Rd. Rr X. Y ← Y + 1 Y ← Y . Rr Y+q. (Y) ← Rr (Y + q) ← Rr (Z) ← Rr (Z)← Rr.1. X ← X + 1 X ← X . Z+ Rd. -X Rd. Y+ Rd.1. (X) ← Rr (Y) ← Rr (Y) ← Rr. X+ Rd. k Rd. -Z Rd. Z Rd. (Z) ← Rr (Z + q)← Rr R0 ← (Z) Rd ← P P ← Rr STACK ← Rr Rd ← STACK None None None None None None None None None None None None None None None None None None None None None None None None None None None None None None None 1 1 3 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 3 1 1 2 2 Development Tools User Guide 4-7 . P P. Rr Z+. Rr Y+.Y+q Rd. Rr Y. Rr Rd.

b) ← 1 I/O(P .V Z.C.C. n=0.C ← Rd(0) Rd(0) ← C.C ← Rd(7) Rd(n) ← Rd(n+1).C.N.4) SREG(s) ← 1 SREG(s) ← 0 I/O(P . b) ← 0 T ← Rr(b) Rd(b) ← T C←1 C←0 N←1 N←0 Z←1 Z←0 I←1 I←0 S←1 S←0 V←1 V←0 T←1 T←0 H ←1 H←0 Z.H Z.Rd(n+1) ← Rd(n).0) ↔ Rd(7. b Logical Shift Left Logical Shift Right Rotate Left Through Carry Rotate Right Through Carry Arithmetic Shift Right Swap Nibbles Flag Set Flag Clear Set Bit in I/O Register Clear Bit in I/O Register Bit Store from Register to T Bit load from T to Register Set Carry Clear Carry Set Negative Flag Clear Negative Flag Set Zero Flag Clear Zero Flag Global Interrupt Enable Global Interrupt Disable Set Signed Test Flag Clear Signed Test Flag Set Two's Complement Overflow Clear Two's Complement Overflow Set T in SREG Clear T in SREG Set Half Carry Flag in SREG Clear Half Carry Flag in SREG No Operation Sleep Watchdog Reset Rd(n+1) ← Rd(n).N.N.C..b Rr.V.N.Rd(7) ← 0.b P .C ← Rd(0) Rd(n) ← Rd(n+1).C.H Z.AVR Assembler User Guide Mnemonics Operands Description Operation Flags #Clock Note BIT AND BIT-TEST INSTRUCTIONS LSL LSR ROL ROR ASR SWAP BSET BCLR SBI CBI BST BLD SEC CLC SEN CLN SEZ CLZ SEI CLI SES CLS SEV CLV SET CLT SEH CLH NOP SLEEP WDR Rd Rd Rd Rd Rd Rd s s P ..Rd(n) ← Rd(n+1).Rd(0) ← 0.V.V None SREG(s) SREG(s) None None T None C C N N Z Z I I S S V V T T H H None None None 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4-8 Development Tools User Guide .V Z..C ← Rd(7) Rd(7) ← C.6 Rd(3.N. b Rd.

can be a constant expression Constant. The operands have the following forms: Rd: Rr: b: s: P: K: k: q: R0-R31 or R16-R31 (depending on instruction) R0-R31 Constant (0-7).AVR Assembler User Guide The Assembler is not case sensitive. Can be a constant expression. Constant (0-63). Summary of directives: Directive BYTE CSEG DB DEF DEVICE DSEG DW ENDMACRO EQU ESEG EXIT INCLUDE LIST LISTMAC MACRO NOLIST ORG SET Description Reserve byte to a variable Code Segment Define constant byte(s) Define a symbolic name on a register Define which device to assemble for Data Segment Define constant word(s) End macro Set a symbol equal to an expression EEPROM Segment Exit from file Read source from another file Turn listfile generation on Turn macro expansion on Begin macro Turn listfile generation off Set program origin Set a symbol to an expression Note: All directives must be preceded by a period. can be a constant expression Constant (0-31/63). Development Tools User Guide 4-9 . value range depending on instruction. can be a constant expression Constant (0-255). can be a constant expression Constant (0-7). define macros. Instead. initialize memory and so on.5 Assembler directives The Assembler supports a number of directives. can be a constant expression 4. An overview of the directives is given in the following table. The directives are not translated directly into opcodes. they are used to adjust the location of the program in memory.

CSEG Example: .Z .CSEG const: . the BYTE directive should be preceded by a label. Load Z register high r1.low(var1) . .CSEG ldi ldi ld 4.r0 .Code Segment r30. The directive takes one parameter. The default segment type is Code.Reserve bytes to a variable The BYTE directive reserves memory resources in the SRAM.mem. which are concatenated into one Code Segment when assembled. The directive does not take any parameters. Reserve 4 bytes in SRAM . reserve 1 byte to var1 table: .5. which is the number of bytes to reserve. reserve tab_size bytes . Syntax: . The Code Segments have their own location counter which is a word counter. DSEG and ESEG).5. An Assembler file can consist of several Code Segments.2 CSEG . Start data segment . Do something 4-10 Development Tools User Guide . Syntax: LABEL: .AVR Assembler User Guide 4. Load Z register low r31. The BYTE directive can not be used within a Code Segment.DW 2 mov r1. Write 0x0002 in prog. Start code segment .DSEG vartab: . Load VAR1 into register 1 The CSEG directive defines the start of a Code Segment.BYTE expression Example: . The directive can only be used within a Data Segment (see directives CSEG. The ORG directive (see description later in this document) can be used to place code and constants at specific locations in the Program memory.BYTE 4 .1 BYTE . In order to be able to refer to the reserved location.BYTE 1 . Note that a parameter must be given.BYTE tab_size . The allocated bytes are not initialized.high(var1) .DSEG var1: .

If the expression evaluates to a negative number. the 8 bits two's complement of the number will be placed in the program memory or EEPROM memory location. Read SREG into ior register .ESEG eeconst:. Syntax: LABEL: . delimited by commas. the last expression will be placed in a program memory word of its own.DB 0xff 4.AVR Assembler User Guide 4.DB expressionlist Example: . the DB directive should be preceded by a label. Exclusive or temp and ior Development Tools User Guide 4-11 . 255.DEF temp=R16 . and must contain at least one expression. -128. the expressions are packed so that two bytes are placed in each program memory word. A symbol can be redefined later in the program.5.CSEG consts: .CSEG ldi in eor temp. The DB directive must be placed in a Code Segment or an EEPROM Segment.5.DEF Symbol=Register Example: . 0b01010101. Syntax: . A defined symbol can be used in the rest of the program to refer to the register it is assigned to. Each expression must evaluate to a number between -128 and 255. If the DB directive is used in a Code Segment and the expressionlist contains more than one expression. A register can have several symbolic names attached to it.3 DB-Define constant byte(s) in program memory or E2PROM memory The DB directive reserves memory resources in the program memory or the EEPROM memory. If the expressionlist contains an odd number of expressions.ior .DEF ior=R0 .0xf0 ior.DB 0. Load 0xf0 into temp register . The expression list is a sequence of expressions. The DB directive takes a list of expressions. 0xaa .Set a symbolic name on a register The DEF directive allows the registers to be referred to through symbols. even if the next line in the assembly code contains a DB directive.4 DEF . In order to be able to refer to the reserved locations.0x3f temp.

If the size of the Code Segment or EEPROM Segment is larger than supported by the specified device.Data Segment The DSEG directive defines the start of a Data Segment.CSEG push r30 .6 DSEG . This statement will generate a warning since the specified device does not have this instruction . .AVR Assembler User Guide 4.BYTE tab_size . .5. it is assumed that all instructions are supported and that there are no restrictions on memory sizes.high(var1) r1.low(var1) r31. Use the AT90S1200 4. A Data Segment will normally only consist of BYTE directives (and labels). . Load Z register high . The ORG directive (see description later in this document) can be used to place the variables at specific locations in the SRAM. An Assembler file can consist of several Data Segments. which are concatenated into one Data Segment when assembled. reserve tab_size bytes. If the DEVICE directive is not used. 4-12 Development Tools User Guide .BYTE 1 table:. If this directive is used. Syntax: .CSEG ldi ldi ld r30.Define which device to assemble for The DEVICE directive allows the user to tell the Assembler which device the code is to be executed on.DEVICE AT90S1200 | AT90S2313 | AT90S4414 | AT90S8515 Example: . Syntax: . The directive does not take any parameters.5 DEVICE . a warning is issued.5.DSEG var1:. a warning is issued if an instruction not supported by the specified device occurs in the code.Z .DEVICE AT90S1200 . Load Z register low . Load var1 into register 1 . reserve 1 byte to var1 . The Data Segments have their own location counter which is a byte counter.DSEG Example: . Start data segment .

0b1001110001010101.5. The directive does not take any parameters.AVR Assembler User Guide 4.7 DW-Define constant word(s) in program memory or E2PROM memory The DW directive reserves memory resources in the program memory or EEPROM memory.ENDMACRO 4. Syntax: .65535 .r2 . Each expression must evaluate to a number between -32768 and 65535.ENDMACRO Example: . The expression list is a sequence of expressions. Write to Port A Development Tools User Guide 4-13 .5.9 EQU .MACRO SUBI16 subi sbci r16. Syntax: LABEL: . See the MACRO directive for more information on defining Macros. In order to be able to refer to the reserved locations.CSEG varlist:. Clear register 2 .EQU label = expression Example: . the DW directive should be preceded by a label. Start code segment . Syntax: .Set a symbol equal to an expression The EQU directive assigns a value to a label.DW 0xffff 4.-32768. Subtract low byte . End macro definition .CSEG clr out r2 porta.EQU porta = io_offset + 2 . and must contain at least one expression. This label can then be used in later expressions.ESEG eevar: .high(@0) .0xffff. Start macro definition .DW expressionlist Example: . The DW directive takes a list of expressions.End macro The ENDMACRO directive defines the end of a Macro definition.8 ENDMACRO . The DB directive must be placed in a Code Segment or an EEPROM Segment.EQU io_offset = 0x23 . If the expression evaluates to a negative number. delimited by commas.5.DW 0. A label assigned to a value by the EQU directive is a constant and can not be changed or redefined. Subtract high byte .low(@0) r17. the 16 bits two's complement of the number will be placed in the program memory location.

10 ESEG .asm” in r0. Include I/O definitions .Exit this file . The directive does not take any parameters.ESEG eevar: . The ORG directive (see description later in this document) can be used to place constants at specific locations in the EEPROM memory. Stack pointer low .sreg . iodefs.EXIT Example: . Start code segment .DW 2 . The Assembler then assembles the specified file until end of file (EOF) or an EXIT directive is encountered. Start data segment . Syntax: .INCLUDE “iodefs.mem. The BYTE directive can not be used within an EEPROM Segment.INCLUDE “filename” Example: . An included file may itself contain INCLUDE directives.5.5. .CSEG const: mov r1. Exit this file 4.DSEG vartab: . Initialize one word in . An Assembler file can consist of several EEPROM Segments. The EEPROM Segments have their own location counter which is a byte counter.EEPROM Segment The ESEG directive defines the start of an EEPROM Segment. the Assembler runs until end of file (EOF). Status register .DW 0xff0f .EQU . EEPROM .5. Syntax: . Syntax: .AVR Assembler User Guide 4. Reserve 4 bytes in SRAM The EXIT directive tells the Assembler to stop assembling the file.12 INCLUDE .asm .11 EXIT . If an EXIT directive appears in an included file.BYTE 4 .Include another file The INCLUDE directive tells the Assembler to start reading from a specified file.asm: . the Assembler continues from the line following the INCLUDE directive in the file containing the INCLUDE directive. Stack pointer high .EXIT .EQU sreg=0x3f sphigh=0x3e splow=0x3d . incdemo. Do something . Normally.EQU . Read status register 4-14 Development Tools User Guide .r0 4.ESEG Example: . which are concatenated into one EEPROM Segment when assembled. Write 0x0002 in prog.

Start macro definition .CSEG SUBI16 0x1234. Listfile generation is turned on by default.MACRO MACX add eor r0. Syntax: . Do something .low(@0) @2. End macro definition . the parameters are given as a comma separated list. Subtract high byte . Enable macro expansion .5.Turn macro expansion on . End macro definition .r1 .LISTMAC Example: . show expansion The MACRO directive tells the Assembler that this is the start of a Macro. a LISTMAC directive must be used.INCLUDE “macro.r16.5.AVR Assembler User Guide 4. When issuing a Macro call. The Assembler generates a listfile which is a combination of assembly source code.r17.inc” . Call macro. Sub. In order to include the macro expansion in the listfile.high(@0) .LIST 4. Subtract low byte . Do something . Syntax: .13 LIST .14 LISTMAC . Disable listfile generation . When the name of the Macro is written later in the program. Reenable listfile generation The LISTMAC directive tells the Assembler that when a macro is called.Turn the listfile generation on The LIST directive tells the Assembler to turn listfile generation on. be shown in the listfile .Begin macro r2. the Macro definition is expanded at the place it was used.0x1234 from r17:r16 Development Tools User Guide 4-15 . The default is that only the macro-call with parameters is shown in the listfile. A macro is marked with a + in the opcode field of the listfile. By default.15 MACRO . addresses and opcodes. The MACRO directive takes the Macro name as parameter.LIST Example: .INCLUDE “const.NOLIST .ENDMACRO .MACRO SUBI16 subi sbci @1. The Macro definition is terminated by an ENDMACRO directive. The included files will not .LISTMAC MACX 4. Define an example macro .@0 r1.ENDMACRO . only the call to the Macro is shown on the listfile generated by the Assembler. Syntax: . A Macro can take up to 10 parameters. Start code segment .5. These parameters are referred to as @0-@9 within the Macro definition. The directive can also be used together with the NOLIST directive in order to only generate listfile of selected parts of an assembly source file. the expansion of the macro is to be shown on the listfile generated by the Assembler.MACRO macroname Example: .@1 .def” .

Disable listfile generation . addresses and opcodes. then it is the SRAM location counter which is set. Start EEPROM Segment .5. counter .Turn listfile generation off The NOLIST directive tells the Assembler to turn listfile generation off.67H . Initialize one word 4-16 Development Tools User Guide . Start data segment .17 ORG .INCLUDE “macro. Set SRAM address to hex 67 .DW 0xfeff .AVR Assembler User Guide 4. The directive can also be used together with the LIST directive in order to only generate listfile of selected parts of an assembly source file.5. the label will be given the value of the parameter. but can be disabled by using this directive. then it is the EEPROM location counter which is set. then it is the Program memory counter which is set and if the directive is given within an EEPROM Segment. Set Program Counter to hex . Set EEPROM location .Set program origin . Do something . The default values of the Code and EEPROM location counters are zero.BYTE 1 . The value to set is given as a parameter. Listfile generation is turned on by default. adr.NOLIST . Enable listfile generation .ORG 0x10 mov r0.INCLUDE “const. Syntax: .LIST 4.CSEG . 10 .ORG 0x67 variable:.16 NOLIST .ESEG . if the directive is given within a Code Segment. The Assembler normally generates a listfile which is a combination of assembly source code. Note that the EEPROM and SRAM location counters count bytes whereas the Program memory location counter counts words. whereas the default value of the SRAM location counter is 32 (due to the registers occupying addresses 0-31) when the assembling is started. The included files will not . Reserve a byte at SRAM .ORG expression Example: . Reenable listfile generation The ORG directive sets the location counter to an absolute value.inc” .ORG 0x20 eevar: . Syntax: . be shown in the listfile .DSEG .r1 . If an ORG directive is given within a Data Segment. If the directive is preceded by a label (on the same source code line).NOLIST Example: .def” .

SET porta = io_offset + 2 . A label assigned to a value by the SET directive can be changed later in the program. the higher the priority.6. This label can then be used in later expressions.18 SET .6 4. operators and functions.6. s User defined variables defined by the SET directive s User defined constants defined by the EQU directive s Integer constants: constants can be given in several formats. $ff s c) Binary: 0b00001010.1 Expressions Operands The Assembler incorporates expressions. Syntax: .SET label = expression Example: .Set a symbol equal to an expression The SET directive assigns a value to a label.6. The higher the precedence.the current value of the Program memory location counter 4. and such expressions are always evaluated before combined with anything outside the parentheses.r2 . Write to Port A 4. 0b11111111 PC .SET io_offset = 0x23 . All expressions are internally 32 bits.5. 0xff.2 Functions The following functions are defined: s LOW(expression) returns the low byte of an expression s HIGH(expression) returns the second byte of an expression s BYTE2(expression) is the same function as HIGH s BYTE3(expression) returns the third byte of an expression s BYTE4(expression) returns the fourth byte of an expression s LWRD(expression) returns bits 0-15 of an expression s HWRD(expression) returns bits 16-31 of an expression s PAGE(expression) returns bits 16-21 of an expression s EXP2(expression) returns 2^expression s LOG2(expression) returns the integer part of log2(expression) The Assembler supports a number of operators which are described here. The following operands can be used: s User defined labels which are given the value of the location counter at the place they appear.CSEG clr out r2 porta. $0a. Expressions may be enclosed in parentheses. 4.3 Operators Development Tools User Guide 4-17 . 255 b) Hexadecimal (two notations): 0x0a. including a) Decimal (default): 10.AVR Assembler User Guide 4. Expressions can consist of operands. Start code segment . Clear register 2 .

Load r17 with 1 shifted . Load r16 with 0x0f Precedence: 14 Example: 4. Load r30 with label*2 Precedence: 13 Example: 4.-2 .7 Subtraction Symbol: Description: ldi Binary operator which returns the left expression minus the right expression r30.6 Addition Symbol: Description: ldi + Binary operator which returns the sum of two expressions r30.label/2 .6.6.c1-c2 .6.AVR Assembler User Guide 4.3.6.3.~0xf0 .3.Load r17 with c1-c2 Precedence: 11 Example: ldi r17.6. and returns 0 if the expression was nonzero Precedence: 14 Example: 4.3 Unary Minus Symbol: Description: ldi Unary operator which returns the arithmetic negation of an expression r16.3.6.8 Shift left Symbol: Description: ldi << Binary operator which returns the left expression shifted left a number of times given by the right expression r17.left bitmask times 4-18 Development Tools User Guide .1<<bitmask .3.6.3.!0xf0 .3.1 Logical Not Symbol: Description: ! Unary operator which returns 1 if the expression was zero.c1+c2 .6. Load r30 with c1+c2 Precedence: 12 Example: 4. Load r30 with label/2 Precedence: 12 Example: 4.3.2 Bitwise Not Symbol: Description: ldi ~ Unary operator which returns the input expression with all bits inverted r16.5 Division Symbol: Description: ldi / Binary operator which returns the integer quotient of the left expression divided by the right expression r30. Load -2(0xfe) in r16 Precedence: 13 Example: 4.4 Multiplication Symbol: Description: ldi * Binary operator which returns the product of two expressions r16.label*2 . Load r16 with 0x00 Precedence: 14 Example: 4.

9 Shift right Symbol: Description: >> Binary operator which returns the left expression shifted right a number of times given by the right expression. 0 otherwise Precedence: 10 Example: ori r18.6.bitmask*(c1>c2)+1 .bitmask*(c1==c2)+1 . 0 otherwise Precedence: 10 Example: ori r18.3.an expression 4.11 Less or Equal Symbol: Description: <= Binary operator which returns 1 if the signed expression to the left is Less than or Equal to the signed expression to the right.AVR Assembler User Guide 4.Or r18 with .Or r18 with .Load r17 with c1 shifted .bitmask*(c1<=c2)+1 .10 Less than Symbol: Description: < Binary operator which returns 1 if the signed expression to the left is Less than the signed expression to the right.3.3.an expression 4. 0 otherwise Precedence: 10 Example: ori r18.bitmask*(c1<c2)+1 .6.an expression 4. Precedence: 11 Example: ldi r17.bitmask*(c1>=c2)+1 . 0 otherwise Precedence: 9 Example: andi r19.3.6.Or r18 with . 0 otherwise Precedence: 10 Example: ori r18.And r19 with .c1>>c2 .14 Equal Symbol: Description: == Binary operator which returns 1 if the signed expression to the left is Equal to the signed expression to the right.Or r18 with .6.3.3.an expression 4.6.right c2 times 4.13 Greater or Equal Symbol: Description: >= Binary operator which returns 1 if the signed expression to the left is Greater than or Equal to the signed expression to the right.6.12 Greater than Symbol: Description: > Binary operator which returns 1 if the signed expression to the left is Greater than the signed expression to the right.an expression Development Tools User Guide 4-19 .

Load r18 with an expression 4-20 Development Tools User Guide .Load r18 with an expression Precedence: 6 Example: 4.3.Low(c1|c2) .Low(c1^c2) . 0 otherwise r18.Low(c1||c2) .6.6.6.6.Load r18 with an expression Precedence: 4 Example: ldi r18.19 Logical And Symbol: Description: ldi && Binary operator which returns 1 if the expressions are both nonzero.6.3.17 Bitwise Xor Symbol: Description: ldi ^ Binary operator which returns the bitwise Exclusive Or between two expressions r18.18 Bitwise Or Symbol: Description: ldi | Binary operator which returns the bitwise Or between two expressions r18.16 Bitwise And Symbol: Description: .6.3.AVR Assembler User Guide 4.20 Logical Or Symbol: Description: ldi || Binary operator which returns 1 if one or both of the expressions are nonzero.High(c1&c2) .15 Not Equal Symbol: Description: != Binary operator which returns 1 if the signed expression to the left is Not Equal to the signed expression to the right.Load r18 with an expression Precedence: 7 Example: 4. 0 otherwise r18.Load r18 with an expression Precedence: 5 Example: 4.Set flag to 1 or 0 Precedence: 8 Example: 4.3.Low(c1&&c2) .SET flag=(c1!=c2) & Binary operator which returns the bitwise And between two expressions .3. 0 otherwise Precedence: 9 Example: 4.3.

AVR Assembler User Guide 4.7.7. the insertion point appears in the upper left corner of the window. A typical editing session with the Assembler is shown in the following figure. The insertion point can be moved anywhere by moving the mouse cursor to the point where the insertion point is wanted and click the left button. the text automatically scrolls to the left so that the insertion point is always visible.3 4. Only the menu items specific to the Assembler are described. It is assumed that the user is familiar with the “Search” and “Window” menu items. the text editor will be inactive. It is still possible to assemble files larger than this.7.4 Typing and Formatting Text Moving the Insertion Point Development Tools User Guide 4-21 . A new editor window is created for every assembly file which is opened. 4.7 Microsoft Windows specifics This section describes the features specific to WAVRASM. but they can not be edited in the integrated editor. To create a new assembly file click the button on the toolbar or choose “File → New” (ALT-F N) from the menu. 4. The size of each file must be less than about 28K bytes due to a limitation in MS-Windows. Theoretically there is no limit on how many assembly files which can be open at one time.1 Opening Assembly Files A new or existing assembly files can be opened in WAVRASM. Right after a file is loaded into an editor window of the Assembler. 4. The insertion point moves to the right when typing. To open an existing file click the button on the toolbar or choose “File → Open” (ALT-F O) from the menu. Refer to the section on opening files on how to open a file. If text is written beyond the right margin.2 The Integrated Editor When WAVRASM is finished loading a file.7.

and press Backspace.7. the text to operate on must first be selected.7 Scrolling Editing Text If a line of text is longer or wider than can be shown at one time. To: insert a space delete a character to the left delete a character to the right end a line indent a line insert a tab stop Press: Spacebar Backspace Del Enter Tab Tab To split a line. use the following keys or key combinations: To move the insertion point: to the right in a line of text to the left in a line of text up in a body of text down in a body of text to the beginning of a line of text to the end of a line of text to the beginning of the file to the end of the file Press: Right arrow key Left arrow key Up arrow key Down arrow key Home End Ctrl+Home Ctrl+End 4. Before a command is selected from the Edit-menu to edit text.7. Text can be deleted. To cancel the selection. the file can be scrolled by using the scroll bars. 4. 2. move the insertion point to the beginning of the line to move. moved or copied to new locations. To join two lines. When text is deleted or copied with the commands Cut or Copy.8 Selecting Text 4-22 Development Tools User Guide . The Paste command copies text from the Clipboard to the editor. 4. Press and hold the Shift-key while moving the insertion point to the end of the text to select. the text is placed in the Clipboard.7. The Undo command can be used to revert the last edit.6 4. Release the Shift-key.AVR Assembler User Guide To move the insertion point with the keyboard. Transferring text to and from other windows or applications can be done via the clipboard. move the insertion point to the position where the break is wanted and press Enter.5 Formatting Text The keys in the table below describes the necessary operations to type in the text exactly as wanted. The editor joins the line with the line above. The Edit-menu contains some functions which can be of much help in editing.7. Use the arrow keys to move the insertion point to the beginning of the text to select. press one of the arrow keys. Selecting text with the keyboard: 1.

If one of the error lines in the message window is clicked. Click the button on the toolbar or choose “Edit → Paste” (Shift-Ins) from the menu.10 Moving Text Text can be moved from one location in the editor by first copy the text to the Clipboard with the Cut command. Move the mouse cursor to the beginning of the text to select. Select the text to delete. Move the insertion point to the new location. Click the button on the toolbar or choose “Edit → Copy” (Ctrl+Ins) from the menu. 3. Hold down the left mouse button while moving the cursor to the end of the text to select. To cancel the selection. When a program is assembled.7. 4.7. it need not be typed each time.9 Replacing Text When text is selected. 2. To copy text: 1. 4. the source line turns inverted red. the errors are listed in this message window. Press the button on the toolbar or choose “Edit → Cut” (Shift+Del) from the menu. The text can be copied to the Clipboard with Copy.7. Replacing text: 1. The selected text is deleted when the first new character is typed. 4. Development Tools User Guide 4-23 .12 Undoing an Edit The Undo command can be used to cancel the last edit. Type the new text. 2. 2. To restore the deleted text. text may accidentally have been deleted. Select the text to replace. 2. 4.7. To undo the last edit click the (Alt+Backspace) from the menu. For example. If errors are encountered. Press the button on the toolbar or choose “Edit → Paste” (Shift+Ins) from the menu. To move text: 1. The text is placed in the Clipboard. 4. press the key on the toolbar or choose “Edit → Undo” (Alt+Backspace) from the menu immediately after deleting the text. 2. Select the text to move. it can be immediately replaced it by typing new text. The text is placed in the Clipboard. Deleting Text: 1. 3.7.11 Copying Text If some text will be used more than once. 4.AVR Assembler User Guide Selecting text with the mouse: 1. 3. If the Undo command is chosen immediately after the mistake was done. and then pasting it to its new location using the Paste command. If the error is in a included file. Press the Del key. Select the text to copy. press the left mouse button or one of the arrow keys. Move the insertion point to the location to place the text. the text will be restored to what it was before the mistake. 4. and can then be pasted in many places by using the Paste command. nothing happens. a message window appears on the screen. or it has been copied to a wrong location. Release the mouse button.13 Click On Errors button on the toolbar or choose “Edit → Undo” The Assembler has a click on error function.

4-24 Development Tools User Guide . This means that if lines are added or removed in the source file.14 Setting Program Options Some of the default values of WAVRASM can be changed in the options menu. If the file containing the error is not opened (for instance an included file). the file must be reassembled in order to get the line numbers right. the following dialog box pops up.7.AVR Assembler User Guide This feature is demonstrated in the following figure: If the message window line is doubleclicked. the file containing the error becomes the active window. and the cursor is placed at the beginning of the line containing the error. If “Options” is selected on the menu bar. Note that this function points to lines in the assembled file. 4. then the file is automatically opened.

The user can select which output format to generate by using one of the options -m (Motorola S-record). If the OK button is clicked.8 Command line version For the MS-DOS command line version the Assembler is invoked by the command AVRASM [-m | -i | -g][-w] input.obj.lst. This feature is only useful when assembling for devices with 4K words of program memory. the values are remembered in subsequent runs of the Assembler. This feature is only used when assembling for devices with 4K words of program memory. produce the listfile output. output.rom and the object file input.asm. This EEPROM initialization file is in the same format as the Output file format selected. If an EEPROM Segment has been defined in the code. Development Tools User Guide 4-25 . and in the box labeled “Output-file extension” the default extension of the output file is written.obj' is used by the MS-Windows simulator. -i (Intel Hex) or -g (Generic).AVR Assembler User Guide In the box labeled “List-file extension” the default extension on the list file(s) is written. The Generic file format is used by default.rom AVRASM will now read source from input.lst output. Note that the object file (used by the simulator) is not affected by these options. Using this switch on these devices. the relative jump and call instructions will reach the entire program memory. The “Wrap relative jumps” option tells the Assembler to use wrapping of addresses. the relative jump and call instructions will reach the entire program memory. The -w option tells the Assembler to use wrapping of addresses. Using this option on such devices. The objectfile '*. the assembler will also generate a file with extension ‘EEP’ which is the initial values for the EEPROM memory. In the box labeled “Output file format” the type of format wanted on the output file can be selected. the extension of the object file is always ‘OBJ’ and the format is always the same. 4. The “Save before assemble” option makes the Assembler automatically save the contents of the editor before assembling is done.asm output.

AVR Assembler User Guide 4-26 Development Tools User Guide .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->