![](https://csdnimg.cn/release/download_crawler_static/86655151/bg1.jpg)
Beginners Introduction to the
Assembly Language of
ATMEL-AVR-Microprocessors
by
Gerhard Schmidt
http://www.avr-asm-tutorial.net
October 2004
![](https://csdnimg.cn/release/download_crawler_static/86655151/bg2.jpg)
![](https://csdnimg.cn/release/download_crawler_static/86655151/bg3.jpg)
Avr-Asm-Tutorial 1 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
AT90Sxxxx are ideal for learning assembler........................................................................................1
Test it!....................................................................................................................................................1
Hardware for AVR-Assembler-Programming...........................................................................................2
The ISP-Interface of the AVR-processor family...................................................................................2
Programmer for the PC-Parallel-Port....................................................................................................2
Experimental board with a AT90S2313................................................................................................3
Ready-to-use commercial programming boards for the AVR-family...................................................4
Tools for AVR assembly programing........................................................................................................5
The editor..............................................................................................................................................5
The assembler........................................................................................................................................6
Programming the chips..........................................................................................................................7
Simulation in the studio.........................................................................................................................7
Register......................................................................................................................................................9
What is a register?.................................................................................................................................9
Different registers................................................................................................................................10
Pointer-register....................................................................................................................................10
Recommendation for the use of registers............................................................................................11
Ports.........................................................................................................................................................12
What is a Port?....................................................................................................................................12
Details of relevant ports in the AVR...................................................................................................13
The status register as the most used port.............................................................................................13
Port details...........................................................................................................................................14
SRAM..................................................................................................................................................15
Using SRAM in AVR assembler language.........................................................................................15
What is SRAM?...................................................................................................................................15
For what purposes can I use SRAM?..................................................................................................15
How to use SRAM?.............................................................................................................................15
Use of SRAM as stack.........................................................................................................................16
Defining SRAM as stack................................................................................................................16
Use of the stack...............................................................................................................................17
Bugs with the stack operation.........................................................................................................17
Jumping and Branching............................................................................................................................19
Controlling sequential execution of the program................................................................................19
What happens during a reset?.........................................................................................................19
Linear program execution and branches..............................................................................................20
Timing during program execution.......................................................................................................20
Macros and program execution...........................................................................................................21
Subroutines..........................................................................................................................................21
Interrupts and program execution........................................................................................................23
Calculations..............................................................................................................................................25
Number systems in assembler.............................................................................................................25
Positive whole numbers (bytes, words, etc.)..................................................................................25
Signed numbers (integers)..............................................................................................................25
Binary Coded Digits, BCD.............................................................................................................25
Packed BCDs..................................................................................................................................26
Numbers in ASCII-format..............................................................................................................26
Bit manipulations................................................................................................................................26
Shift and rotate....................................................................................................................................27
Adding, subtracting and comparing....................................................................................................28
Format conversion for numbers...........................................................................................................29
Multiplication......................................................................................................................................30
Decimal multiplication...................................................................................................................30
Binary multiplication......................................................................................................................30
![](https://csdnimg.cn/release/download_crawler_static/86655151/bg4.jpg)
Avr-Asm-Tutorial 2 http://www.avr-asm-tutorial.net
AVR-Assembler program...............................................................................................................31
Binary rotation................................................................................................................................32
Multiplication in the studio.............................................................................................................32
Division...............................................................................................................................................34
Decimal division.............................................................................................................................34
Binary division...............................................................................................................................34
Program steps during division........................................................................................................35
Division in the simulator................................................................................................................35
Number conversion.............................................................................................................................37
Decimal Fractions................................................................................................................................37
Linear conversions..........................................................................................................................37
Example 1: 8-bit-AD-converter with fixed decimal output............................................................38
Example 2: 10-bit-AD-converter with fixed decimal output..........................................................40
Annex.......................................................................................................................................................41
Commands sorted by function.............................................................................................................41
Command list in alphabetic order.......................................................................................................43
Assembler directives.......................................................................................................................43
Commands......................................................................................................................................43
Port details...........................................................................................................................................45
Status-Register, Accumulator flags................................................................................................45
Stackpointer....................................................................................................................................45
SRAM and External Interrupt control............................................................................................45
External Interrupt Control...............................................................................................................46
Timer Interrupt Control..................................................................................................................46
Timer/Counter 0..............................................................................................................................47
Timer/Counter 1..............................................................................................................................48
Watchdog-Timer.............................................................................................................................49
EEPROM........................................................................................................................................49
Serial Peripheral Interface SPI........................................................................................................50
UART.............................................................................................................................................51
Analog Comparator........................................................................................................................51
I/O Ports..........................................................................................................................................52
Ports, alphabetic order.........................................................................................................................52
List of abbreviations............................................................................................................................53
Errors in previous versions..................................................................................................................54
![](https://csdnimg.cn/release/download_crawler_static/86655151/bg5.jpg)
Avr-Asm-Tutorial 1 http://www.avr-asm-tutorial.net
Why learning Assembler?
Assembler or other languages, that is the question. Why should I learn another language, if I already
learned other programming languages? The best argument: while you live in France you are able to get
through by speaking english, but you will never feel at home then, and life remains complicated. You can
get through with this, but it is rather inappropriate. If things need a hurry, you should use the country's
language.
Short and easy
Assembler commands translate one by one to executed machine commands. The processor needs only to
execute what you want it to do and what is necessary to perform the task. No extra loops and unnecessary
features blow up the generated code. If your program storage is short and limited and you have to optimize
your program to fit into memory, assembler is choice 1. Shorter programs are easier to debug, every step
makes sense.
Fast and quick
Because only necessary code steps are executed, assembly programs are as fast as possible. The
duration of every step is known. Time critical applications, like time measurements without a hardware
timer, that should perform excellent, must be written in assembler. If you have more time and don't mind if
your chip remains 99% in a wait state type of operation, you can choose any language you want.
Assembler is easy to learn
It is not true that assmbly language is more complicated or not as easy to understand than other
languages. Learning assembly language for whatever hardware type brings you to understand the basic
concepts of any other assembly language dialect. Adding other dialects later is easy. The first assembly
code does not look very attractive, with every 100 additional lines programmed it looks better. Perfect
programs require some thousand lines of code of exercise, and optimization requires lots of work. As
some features are hardware-dependant optimal code requires some familiarity with the hardware concept
and the dialect. The first steps are hard in any language. After some weeks of programming you will laugh
if you go through your first code. Some assembler commands need some monthes of experience.
AT90Sxxxx are ideal for learning assembler
Assembler programs are a little bit silly: the chip executes anything you tell it to do, and does not ask you if
you are sure overwriting this and that. All protections must be programmed by you, the chip does anything
like it is told. No window warns you, unless you programmed it before.
Basic design errors are as complicated to debug like in any other computer language. But: testing
programs on ATMEL chips is very easy. If it does not do what you expect it to do, you can easily add some
diagnostic lines to the code, reprogram the chip and test it. Bye, bye to you EPROM programmers, to the
UV lamps used to erase your test program, to you pins that don't fit into the socket after having them
removed some douzend times.
Changes are now programmed fast, compiled in no time, and either simulated in the studio or checked in-
circuit. No pin is removed, and no UV lamp gives up just in the moment when you had your excellent idea
about that bug.
Test it!
Be patient doing your first steps! If you are familiar with another (high-level) language: forget it for the first
time. Behind every assembler language there is a certain hardware concept. Most of the special features
of other computer languages don't make any sense in assembler.
The first five commands are not easy to learn, then your learning speed rises fast. After you had your first
lines: grab the instruction set list and lay back in the bathtub, wondering what all the other commands are
like.
Don't try to program a mega-machine to start with. This does not make sense in any computer language,
and just produces frustration.
Comment your subroutines and store them in a special directory, if debugged: you will need them again in
a short time.
Have success!