Assembly Programming in AVR

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

‫‪Islamic Azad University‬‬

‫‪Najafabad Branch‬‬
‫‪Computer Department‬‬
‫‪Ahmad M. Shafiee‬‬
‫ضماره صفحات ذکر ضده در اسالیدىا مربوط بو ک تاب «برناموریزی و استفاده از میکروکنترلر ‪ »AVR‬از انتصارات دانشپژوىان میباضد‪.‬‬
 The Assembler is not case sensitive.
 The operands have the following forms:
◦ Rd: R0-R31 or R16-R31 (depending on instruction)
◦ Rr: R0-R31
◦ b: Constant (0-7), can be a constant expression
◦ s: Constant (0-7), can be a constant expression
◦ P: Constant (0-31/63), can be a constant
expression
◦ K: Constant (0-255), can be a constant expression
◦ k: Constant, value range depending on instruction.
◦ Can be a constant expression.
◦ q: Constant (0-63), can be a constant expression
assembler directives
Directive Description
.DEF Defines a synonym for a register (e.g. .DEF MyReg = R16)
.EQU Defines a symbol and sets its value (later changes of this value remain possible, syntax:
.EQU test = 1234567, internal storage of the value is 4-byte- Integer)
.SET Fixes the value of a symbol (later redefinition is not possible)
.INCLUDE Includes a file and assembles its content (like part of the calling file)
(.INCLUDE "C:\avrtools\appnotes\8515def.inc")
.ESEG The code produced will go to the EEPROM memory (the code produces an .EEP-file)
.CSEG All that follows is assembled to the code segment and will go to the program space (flash)
.DB Inserts one or more constant bytes in the code segment. (could be numbers from 0..255, an
ASCII-character like 'c', a string like 'abcde' or a combination like 1,2,3,'abc'. In code
segment, the number of inserted bytes must be even, otherwise an additional zero byte
will be inserted by the assembler.)
.DW Insert a binary word in defined segment
.ORG Defines the address within the respective segment, where the assembler assembles to.
(e.g. .ORG 0x0000)
.EXIT End of the assembler-source code (stops the assembling process)
Number Symbol
format
Decimal default
Hexadecimal $ or 0x
Binary 0b
function description
LOW(expression) low byte of the expression
HIGH(expression) second byte of the expression
BYTE2(expression) second byte of the expression
BYTE3(expression) third byte of the expression
BYTE4(expression) fourth byte of the expression
LWRD(expression) bits 0-15 of the expression
HWRD(expression) bits 16-31 of the expression
EXP2(expression) 2 ^ expression
LOG2(expression) integer part of log2(expression)
operator description
! logical not
~ bitwise not
- unary minus
* multiplication
/ division
+ addition
- subtraction
<< shift left
>> shift right
< less than (result = 0 or 1)
operator description
<= less or equal (result = 0 or 1)
> greater than (result = 0 or 1)
>= greater or equal (result = 0 or 1)
== equal (result = 0 or 1)
!= not equal (result = 0 or 1)
& bitwise and
^ bitwise xor
| bitwise or
&& logical and
|| logical or
Label

.ESEG

LookUpTable:
.db 0, 1, 4, 9, 16, …

.CSEG

ldi r30,low(LookUpTable+4); Load Z register low
ldi r31,high(LookUpTable+4); Load Z register high
out EEARH,ZH
out EEARL,ZL; Loads address of look up table to EEAR for future use

.CSEG

ldi r30,low(LookUpTable*2+4); Load Z register low
ldi r31,high(LookUpTable*2+4); Load Z register high
LPM; Loads 16 from look up table to r0

LookUpTable:
.db 0, 1, 4, 9, 16, …

Use the add and shift-left operators ("<<") in the assembler.
Observe that the "+" operator has precedence over "<<".

An example to bring the MCU to idle mode:

in r16, MCUCR
sbr r16, (1<<SE)+(1<<SM); set SE and SM
out MCUCR, r16
sleep

You might also like