267x Filetype PDF File size 2.70 MB Source: www.avr-asm-download.de
Beginners Introduction to the
Assembly Language of
ATMEL-AVR-Microprocessors
by
Gerhard Schmidt
http://www.avr-asm-tutorial.net
November 2022
History:
Added additional examples on fixed dot numbers in November 2022
Added chapters on binary floating points and on memory access in September 2021
Added chapter on code structures in April 2009
Additional corrections and updates as of January 2008
Corrected version as of July 2006
Original version of December 2003
Avr-Asm-Tutorial 2 http://www.avr-asm-tutorial.net
Content
Why learning Assembler?..........................................................................................................................1
Short and easy.......................................................................................................................................1
Fast and quick........................................................................................................................................1
Assembler is easy to learn.....................................................................................................................1
AVRs are ideal for learning assembler..................................................................................................1
Test it!....................................................................................................................................................2
Hardware for AVR-Assembler-Programming...........................................................................................3
The ISP-Interface of the AVR-processor family...................................................................................3
Programmer for the PC-Parallel-Port....................................................................................................3
Experimental boards..............................................................................................................................4
Experimental board with an ATtiny13.............................................................................................4
Experimental board with an AT90S2313/ATmega2313..................................................................5
Ready-to-use commercial programming boards for the AVR-family...................................................6
STK200.............................................................................................................................................6
STK500.............................................................................................................................................6
AVR Dragon.....................................................................................................................................7
Tools for AVR assembly programming.....................................................................................................8
From a text file to instruction words in the flash memory....................................................................8
The editor..........................................................................................................................................8
Structuring assembler code...............................................................................................................9
Comments.........................................................................................................................................9
Things to be written on top...............................................................................................................9
Things that should be done at program start...................................................................................10
Structuring of program code...........................................................................................................10
The assembler.................................................................................................................................13
Programming the chips........................................................................................................................14
Simulation in the studio.......................................................................................................................14
What is a register?...............................................................................................................................18
Different registers................................................................................................................................20
Pointer-registers...................................................................................................................................20
Accessing memory locations with pointers....................................................................................20
Reading program flash memory with the Z pointer........................................................................21
Tables in the program flash memory..............................................................................................21
Accessing registers with pointers...................................................................................................21
Recommendation for the use of registers............................................................................................22
Ports.........................................................................................................................................................23
What is a Port?....................................................................................................................................23
Write access to ports.......................................................................................................................23
Read access to ports........................................................................................................................24
Read-Modify-Write access to ports................................................................................................24
Memory mapped port access..........................................................................................................24
Details of relevant ports in the AVR..............................................................................................25
The status register as the most used port.............................................................................................25
Port details...........................................................................................................................................26
SRAM......................................................................................................................................................27
Using SRAM in AVR assembler language.........................................................................................27
What is SRAM?...................................................................................................................................27
For what purposes can I use SRAM?..................................................................................................27
How to use SRAM?.............................................................................................................................27
Direct addressing............................................................................................................................27
Pointer addressing...........................................................................................................................28
Pointer with offset...........................................................................................................................28
Use of SRAM as stack.........................................................................................................................28
Defining SRAM as stack................................................................................................................29
Use of the stack...............................................................................................................................29
Bugs with the stack operation.........................................................................................................30
Jumping and Branching............................................................................................................................31
Controlling sequential execution of the program................................................................................31
What happens during a reset?.........................................................................................................31
Linear program execution and branches..............................................................................................32
Branching........................................................................................................................................32
Timing during program execution.......................................................................................................33
Macros and program execution...........................................................................................................33
Avr-Asm-Tutorial 3 http://www.avr-asm-tutorial.net
Subroutines..........................................................................................................................................34
Interrupts and program execution........................................................................................................35
Calculations..............................................................................................................................................38
Number systems in assembler.............................................................................................................38
Positive whole numbers (bytes, words, etc.)..................................................................................38
Signed numbers (integers)..............................................................................................................38
Binary Coded Digits, BCD.............................................................................................................38
Packed BCDs..................................................................................................................................39
Numbers in ASCII-format..............................................................................................................39
Bit manipulations................................................................................................................................39
Shift and rotate....................................................................................................................................40
Adding, subtracting and comparing....................................................................................................41
Adding and subtracting 16-bit numbers.........................................................................................41
Comparing 16-bit numbers.............................................................................................................41
Comparing with constants..............................................................................................................41
Packed BCD math...........................................................................................................................42
Format conversion for numbers...........................................................................................................43
Conversion of packed BCDs to BCDs, ASCII or Binaries............................................................43
Conversion of Binaries to BCD......................................................................................................43
Multiplication......................................................................................................................................43
Decimal multiplication...................................................................................................................43
Binary multiplication......................................................................................................................44
AVR-Assembler program...............................................................................................................44
Binary rotation................................................................................................................................45
Multiplication in the studio.............................................................................................................45
Hardware multiplication......................................................................................................................47
Hardware multiplication of 8-by-8-bit binaries..............................................................................47
Hardware multiplication of a 16- by an 8-bit-binary......................................................................48
Hardware multiplication of a 16- by a 16-bit-binary......................................................................50
Hardware multiplication of a 16- by a 24-bit-binary......................................................................52
Division...............................................................................................................................................53
Decimal division.............................................................................................................................53
Binary division...............................................................................................................................53
Program steps during division........................................................................................................54
Division in the simulator................................................................................................................54
Number conversion.............................................................................................................................56
Decimal Fractions, pseudo-floats, fixed-dot numbers.........................................................................56
Linear conversions..........................................................................................................................56
Example 1: 8-bit-AD-converter with fixed decimal output............................................................57
Example 2: 10-bit-AD-converter with fixed decimal output..........................................................58
Example 3: 10-bit-AD converter with internal reference voltage for voltage measurements........58
Voltage prescaler........................................................................................................................59
Conversion of the AD result to a voltage...................................................................................59
Multiplication with the hardware multiplier..............................................................................59
Multiplying without hardware multiplicator..............................................................................60
Conversion to the decimal ASCII display string.......................................................................61
Example 4: A measuring device for +/-15V...................................................................................62
The prescaler for positive and negative voltages.......................................................................62
Conversion of the ADC results to voltage strings.....................................................................62
Conclusions....................................................................................................................................63
Floating point numbers in assembler language...................................................................................64
Floating points, if necessary...........................................................................................................64
The format of floating point numbers.............................................................................................64
Conversion of binary to decimal number format............................................................................65
Conclusion:.....................................................................................................................................65
Converting floating point numbers to decimal in assembler language...............................................65
Allocation of numbers....................................................................................................................65
Converting the mantissa to decimal................................................................................................66
Converting the exponent bits..........................................................................................................69
Rounding the decimal mantissa......................................................................................................71
Conversion from BCD to ASCII....................................................................................................71
Execution times..............................................................................................................................72
Faster than above: converting a 40-bit-binary to decimal...................................................................72
Conclusion......................................................................................................................................73
Floating point arithmetic in assembly language..................................................................................73
Avr-Asm-Tutorial 4 http://www.avr-asm-tutorial.net
Converting decimals to binary floating point numbers in assembler language..............................73
Decimal number formats............................................................................................................73
The assembler software for the conversion................................................................................73
Detecting the negative sign...................................................................................................73
Read the decimal mantissa and convert it to a binary integer...............................................73
Calculate the binary mantissa................................................................................................74
Determine the decimal exponent and convert it....................................................................74
Normalization and sign processing.......................................................................................74
Results...................................................................................................................................74
Conclusion.................................................................................................................................75
Address modes in AVRs..........................................................................................................................76
Accessing SRAM, registers and port registers....................................................................................76
Accessing SRAM locations with fixed addresses...............................................................................76
Accessing SRAM location with pointers............................................................................................77
Accessing SRAM location with increasing pointers...........................................................................78
Accessing SRAM location with decreasing pointers..........................................................................79
Accessing SRAM locations with displacement addressing.................................................................81
Accessing port registers.......................................................................................................................83
Accessing classical port registers........................................................................................................83
Access to extended port registers........................................................................................................84
Access with pointers, example: the circular LED light.......................................................................84
Accessing EEPROM...........................................................................................................................87
EEPROM initiation with the .ESEG directive....................................................................................87
EEPROM port registers.......................................................................................................................87
Writing the EEPROM address............................................................................................................88
Reading from the EEPROM................................................................................................................89
Write access to the EEPROM..............................................................................................................89
Flash memory accesses........................................................................................................................91
The .CSEG directive............................................................................................................................91
The LPM instruction...........................................................................................................................92
Advanced LPM instructions................................................................................................................93
Use examples for LPM........................................................................................................................93
Annex.......................................................................................................................................................97
Instructions sorted by function............................................................................................................97
Directives and Instruction lists in alphabetic order.............................................................................99
Assembler directives in alphabetic order........................................................................................99
Instructions in alphabetic order....................................................................................................100
Port details.........................................................................................................................................102
Status-Register, Accumulator flags..............................................................................................102
Stackpointer..................................................................................................................................102
SRAM and External Interrupt control..........................................................................................102
External Interrupt Control.............................................................................................................103
Timer Interrupt Control................................................................................................................103
Timer/Counter 0............................................................................................................................105
Timer/Counter 1............................................................................................................................106
Watchdog-Timer...........................................................................................................................107
EEPROM......................................................................................................................................107
Serial Peripheral Interface SPI......................................................................................................108
UART...........................................................................................................................................109
Analog Comparator......................................................................................................................109
I/O Ports........................................................................................................................................110
Ports, alphabetic order.......................................................................................................................110
List of abbreviations..........................................................................................................................111
no reviews yet
Please Login to review.