AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

The Basics of AVR Assembly Language

Tutorial Objectives


After completing this AVR microcontroller tutorial readers should be able to:
  • Give a simple definition for assembly language.
  • Write simple assembly programs.
  • Demonstrate good programming practice in writing assembly programs.


Introduction to AVR Assembly Language


What is assembly language?

Assembly language is an alphanumeric representation of machine code. Below is an example of a AVR assembly code written in assembly language. Each line of the code is an instruction telling the microcontroller to carry out a task.

            ADD     R16, R17       ; Add value in R16 to value in R17
            DEC     R17            ; Minus 1 from the value contained in R17
            MOV     R18, R16       ; Copy the value in R16 to R18
END:        JMP     END            ; Jump to the label END



The instructions used in writing programs in assembly language are not general but specific to the microcontroller. Each company provides a set of instructions for there microcontrollers. AVR 8-bits microcontrollers have a common instruction set .

Please note that not all instructions are available to all microcontrollers. The set available to each AVR microcontroller is given in its datasheet.


Good Assembly Programming Practice


When writing your AVR assembly programs it is a good practice to organize your code in four columns as shown in the code below. This has the benefit of your program being easier to read/debug by you and others.

;Col_1      Col_2   Col_3           Col_4
 
            ADD     R16, R17       ; Add value in R16 to value in R17
            DEC     R17            ; Minus 1 from the value contained in R17
            MOV     R18, R16       ; Copy the value in R16 to R18
END:        JMP     END            ; Jump to the label END

Column 1 (Col_1) is used for labels. Labels are basically markers use by the programmer when indicating to the microcontroller to jump to a specific location in the code.

Column 2 (Col_2) is used for the microcontroller instructions.

Column 3 (Col_3) is used for arguments operated on by the instruction in column 2.

Column 4 (Col_4) is used for comments. Note here that a semi-colon ";" is put in front of each comments. That is the semi-colon indicate that the statement that follows it in the same line is a comment.

AVR Tutorials hopes this AVR assembly tutorial was benificial to you and looks forward to your continued visits for all your microcontroller tutorial needs.