Beginners Introduction to the Assembly Language of ATMEL-AVR-Microprocessors
Total Page:16
File Type:pdf, Size:1020Kb
Beginners Introduction to the Assembly Language of ATMEL-AVR-Microprocessors by Gerhard Schmidt http://www.avr-asm-tutorial.net September 2021 History: 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........................................................................20 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