L11 assembly-language-programming-of-atmega328 p

13
Assembler Programming of Atmega328P (Lecture-11) R S Ananda Murthy Associate Professor and Head Department of Electrical & Electronics Engineering, Sri Jayachamarajendra College of Engineering, Mysore 570 006 R S Ananda Murthy Assembler Programming of Atmega328P

Transcript of L11 assembly-language-programming-of-atmega328 p

Page 1: L11 assembly-language-programming-of-atmega328 p

Assembler Programming of Atmega328P(Lecture-11)

R S Ananda Murthy

Associate Professor and HeadDepartment of Electrical & Electronics Engineering,

Sri Jayachamarajendra College of Engineering,Mysore 570 006

R S Ananda Murthy Assembler Programming of Atmega328P

Page 2: L11 assembly-language-programming-of-atmega328 p

Storing Data in Flash Memory

Though flash memory is referred to as program memory,we can also store fixed data in it using DB and DWdirectives as shown above.Since each word address in flash stores two bytes inlittle-endian order, at any word address, the low-byteaddress is twice the word address and the high-byteaddress is low-byte address+1.

Determine the word and byte addresses in the example givenabove.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 3: L11 assembly-language-programming-of-atmega328 p

HIGH() and LOW() Functions

AVR assembler provides these two functions.HIGH() returns the higher byte of a 16-bit value.LOW() returns the lower byte of a 16-bit value.These functions are typically used to initialize Z register topoint at data bytes stored in the flash memory before usingthe LPM instruction.

For example:

LDI ZH,HIGH($100<<1);Initialize ZH.LDI ZL,LOW($100<<1) ;Initialize ZL.LPM

R S Ananda Murthy Assembler Programming of Atmega328P

Page 4: L11 assembly-language-programming-of-atmega328 p

Arithmetic and Logical Expressions in ALP

Arithmetic Operators Logical OperatorsSymbol Operation Symbol Operations

+ Addition & Bitwise AND− Subtraction | Bitwise OR* Multiplication ^ Bitwise XOR/ Division ~ Bitwise NOT

% Modulo << k Shifts left by k bits>> k Shifts right by k bits

We can write arithmetic and logical operations in ALP using thesymbols given above.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 5: L11 assembly-language-programming-of-atmega328 p

Examples of Arithmetic and Logical Expressions

R S Ananda Murthy Assembler Programming of Atmega328P

Page 6: L11 assembly-language-programming-of-atmega328 p

Device Definitions File

This file contains .EQU directives which define labelsassigned to various ports, port bits, registers and registerbits.For Atmega328P device, this file is named asm328pdef.inc

When we select a device for an assembly language projectin Atmel Studio, these definitions are available to the userprogram through the device definitions file.If we are not using Atmel Studio, then, we have to use.INCLUDE directive to include the Device Definitions File inthe .asm file to use the standard labels.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 7: L11 assembly-language-programming-of-atmega328 p

A Glimpse of Device Definitions File for Atmega328P

R S Ananda Murthy Assembler Programming of Atmega328P

Page 8: L11 assembly-language-programming-of-atmega328 p

Labels for Status Flags in m328Pdef.inc File

In m328Pdef.inc file, labels for status flags are defined asshown above.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 9: L11 assembly-language-programming-of-atmega328 p

Setting Specific Flags in SREG using Flag Labels

Labels for status flags can be used to set specific flags asshown above.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 10: L11 assembly-language-programming-of-atmega328 p

Labels Related to Memories in m328Pdef.inc File

We can use RAMEND to initialize Stack Pointer as shown later.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 11: L11 assembly-language-programming-of-atmega328 p

Stack in Atmega328P

Stack is a portion of R/W memory which is used for storingtemporary data, for storing local variables and for storingreturn addresses before executing subroutines or InterruptService Routines.In AVR MCUs, stack is, implemented as Last-In-First-Out(LIFO), growing from higher memory to lower memorylocations.PUSH Rr command will store the content of Rr in the stackand then decrements the SP by 1.POP Rd command will first increment the SP by 1 andthen, byte from the stack is copied to Rd.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 12: L11 assembly-language-programming-of-atmega328 p

Stack Pointer in Atmega328P

In Atmega328P SP is a 16-bit register having two 8-bitregisters SPH and SPL in the I/O space of the R/Wmemory in MCU as shown above.In AVR MCUs having very small memory only SPL will bepresent.The Stack Pointer (SP) register always points to the top ofthe stack.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 13: L11 assembly-language-programming-of-atmega328 p

License

This work is licensed under aCreative Commons Attribution 4.0 International License.

R S Ananda Murthy Assembler Programming of Atmega328P