Skip to main content

AVR Assembly language Programming

Introduction to AVR assembly language programming

In this section we discuss Assembly language format and define some widely used terminology associated with Assembly language programming. While the CPU can work only in binary, it can do so at a very high speed. It is quite tedious and slow for humans, however, to deal with Os and Is in order to program the computer. A program that consists of Os and Is is called machine language. In the early days of the computer, programmers coded programs in machine language. Although the hexadecimal system was used as a more efficient way to represent binary numbers, the process of working in machine code was still cumbersome for humans. Eventually, Assembly languages were developed, which provided mnemonics for the machine code instructions, plus other features that made programming faster and less prone to error. The term mnemonic is frequently used in computer science and engineering literature to refer to codes and abbreviations that are relatively easy to remember. Assembly language programs must be translated into machine code by a program called an assembler. Assembly language is referred to as a low-level language because it deals directly with the internal structure of the CPU. To program in Assembly language, the programmer must know all the registers of the CPU and the size of each, as well as other details. Today, one can use many different programming languages, such as BASIC, Pascal, C, C++, Java, and numerous others. These languages are called high-level languages because the programmer does not have to be concerned with the internal details of the CPU. Whereas an assembler is used to translate an Assembly language program into machine code (sometimes also called object code or opcode for operation code), high-level languages are translated into machine code by a program called a compiler. For instance, to write a program in C, one must use a C compiler to translate the program into machine language. Next we look at AVR Assembly language format.

Structure of assembly languge

An Assembly language program consists of, among other things, a series of lines of Assembly language instructions. An Assembly language instruction consists of a mnemonic, optionally followed by one or two operands. The operands are the data items being manipulated, and the mnemonics are the commands to the CPU, telling it what to do with those items. An Assembly language program is a series of state-
ments, or lines, which are either Assembly language instructions such as ADD and LDI, or statements called directives. While instructions tell the CPU what to do, directives (also called pseudo-instructions) give directions to the assembler. For example, while the LDI and ADD instructions are commands to the CPU, .0RG and ,EQU are directives to the assembler. The directive ORG tells the assembler to place the opcode at memory location 0, while .EQU introduces a new expression equal to a known expression. An Assembly language instruction consists of four fields:
[ label:] mnemonic operands] [ ; comment]
Brackets indicate that a field is optional and not all lines have them.
Brackets should not be typed in. Regarding the above format, the following points should be noted:
1. The label field allows the program to refer to a line of code by name. The label field cannot exceed a certain number of characters. Check your assembler forthe rule.
2. The Assembly language mnemonic (instruction) and operand(s) fields together perform the real work of the program and accomplish the tasks for which the program was written. In Assembly language statements such as
LDI 223, $55
ADD R23, R19
SUBI R23, $67
ADD and LDI are the mnemonics that produce opcodes; the "$55" and "S67"
are the operands. Instead of a mnemonic and an operand, these two fields could contain assembler pseudo-instructions, or directives. Remember that directives do not generate any machine code (opcode) and are used only by the assembler, as opposed to instructions that are translated into machine code (opcode) for the CPU to execute.  The commands .0RG (origin) and
.EQU are examples of directives. More of these pseudo-instructions are discussed in future.
3. The comment field begins with a semicolon comment indicator Comments may be at the end of a line or on a line by themselves. The assembler ignores comments, but they are indispensable to programmers. Although comments
are optional, it is recommended that they be used to describe the program in a way that makes it easier for someone else to read and understand.
4. We are using JMP and other branching instructions in the program. We are using labels to branch skip some part of our programs. If we have do not know about exact memory location to be branched, we should use label.

Rules for labels in assembly language program

By choosing label names that are meaningful, a programmer can make a program much easier to read and maintain. There are several rules that names must follow. First, each label name must be unique. The names used for labels in Assembly language programmmg consist of alphabetic letters in both uppercase and lowercase, the digits 0 through 9, and the special characters question mark (?), period (.), at (@), underline (_), and dollar sign ($). The first character of the label must be an alphabetic character. In other words, it cannot be a number. Every assembler has some reserved words that must not be used as labels in the program.
Foremost among the reserved words are the mnemonics for the instructions. For example, ''LDI" and "ADD" are reserved because they are instruction mnemonics. In addition to the mnemonics there are some other reserved words. Check your assembler for the list of reserved words.

Assembling an AVR program

Now that the basic form of an Assembly language program has been given, the next question is: How it is created, assembled, and made ready to run? Thesteps to create an executable Assembly language program are out-lined as follows:
1. First we use a text editor to type in a program similar to Program 2-1. In the case of the AVR microcontrollers, we use the AVRStudio IDE, which has a text editor, assembler, simulator, and much more all in one software package. It is an excellent development software that supports all the AVR chips and is free.
Many editors or word processors are also available that can be used to create or edit the program. A widely used editor is the Notepad in Windows, which comes with all Microsoft operating systems, Notice that the editor must be ableto produce an ASCII file. For assemblers, the file names follow the usual DOS
conventions, but the source file has the extension "asm". The "asm" extension
for the source file is used by an assembler in the next step.

2. The "asm" source file containing the program code created in step I is fed to the AVR assembler. The assembler produces an object file, a hex file, an eep-rom file, a list file, and a map file. The object file has the extension "obj", the hex file extension is "hex", the list file extension is "1st", the map file extension is "map", and the eeprom file has the extension "eep". After a successful link, the hex file is ready to be burned into the AVR's program ROM and is
downloaded into the AVR Trainer. We can write the eeprom file into the AVR 's EEPROM to initialize the EEPROM.

lst and map files


The lst (list) file, which is optional, is very useful to the programmer. The list shows the binary and source code; it also shows which instructions are used in the source code, and the amount of memory the program uses. Many assemblers assume that the list file is not wanted unless you indicate
that you want to produce it. These files can be accessed by a text editor such as Notepad and displayed on the monitor, or sent to the printer to get a hard copy. The map file is shown below;

The programmer uses the list and map files to ensure correct system design. There are many different AVR assemblers available for free nowadays. If you use the Windows Operating system, AVR Studio can be a good choice. It has
a nice environment and provides great help. The lst file generated is shown below;

Comments

Popular posts from this blog

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click  here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.  Program ORG 00H MOV R0,#50H   ; get memory location in memory pointer R0 MOV R1,#51H   ; get memory location on memory pointer register R1 MOV A,@R0       ; get content of memory location 50H to accumulator ADD A,@R1        ; add content of A with content of memory location 51H and store result in A MOV R0,#52H    ; get 52H to memory pointer R0 MOV@R0,A         ; copy content of A to memory location 52H END Add 16-bit Numbers Program ORG 00H MOV DPTR,#2040H   ; get 2040H into DPTR MOV A,#2BH              ; get lower byte of second 16-bit number on accumulator MO

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly: ●It is much easier and less time consuming to write programs in C assembly. ●C is more flexible; it is easier to modify and update. ●Programming in C allows to use code available in function libraries. ●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications. Data Types in 8051 Embedded C The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range. Data type

8051 Keyboard Interfacing

Keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports. Therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and a column make a contact. Otherwise, there is no connection between rows and columns. In IBM PC keyboards, a single microcontroller takes care of hardware and software interfacing. A 4x4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input port. Grounding Rows and reading Columns:- 8051 keyboard interfacing It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed. To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, then it reads the columns. If the data read from columns is D3 – D0 =1111, no key has been pressed and the process continues till key press is detected. If one of t