SlideShare una empresa de Scribd logo
1 de 29
Descargar para leer sin conexión
Assembler Programming of Atmega328P
(Lecture-10)
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
Language Options to Program Atmega328P
Higher Level Languages (HLP) – mostly machine
independent.
C/C++
Wiring. This is nothing but higher level abstractions
implemented using functions written in C/C++ to make the
microcontroller programming easier. Arduino IDE uses
Wiring.
Assembly Language (ALP) – machine dependent.
ALP is needs more effort as compared to HLP because it
requires the knowledge of the instruction set of the MCU in
addition to the knowledge of the internal architecture.
R S Ananda Murthy Assembler Programming of Atmega328P
Why Learn ALP?
ALP helps in better understanding of the internal
architecture of MCU.
ALP gives direct access to all the hardware blocks in side
the MCU which may not be possible by HLP.
ALP helps in writing highly optimized code (shortest,
fastest program). This is very essential in time-critical and
space-critical applications.
Knowledge of ALP helps in detecting bugs in a machine
language program by translating the machine code back
into mnemonics using a disassembler which is a software
that performs the translation.
Industries engaged in the design of embedded systems
expect ALP and HLP skills in their prospective employees.
R S Ananda Murthy Assembler Programming of Atmega328P
What Facilities are Needed for ALP?
A text editor to create the assembly language source file
which is typically stored as a .asm file.
An assembler which is a software that parses the source
file and outputs the machine language instructions as a
.hex file.
A debugger or simulator to check the working of the
program.
A programmer – which is a hardware – along with its
related software to download .hex file to the MCU.
Note that these facilities are available on both GNU Linux and
Windows platforms.
R S Ananda Murthy Assembler Programming of Atmega328P
Classification of Atmega328P Instructions
There are 141 instructions in the instruction set of Atmega328P.
They are classified as —
1 Arithmetic and Logic Instructions.
2 Branch Instructions
3 Data Transfer Instructions
4 Bit and Bit Test Instructions
5 MCU Control Instructions
Majority of the instructions are 2-bytes in length. Only LDS,
STS, JMP and CALL instructions are 4-bytes in length.
R S Ananda Murthy Assembler Programming of Atmega328P
Atmega328P Instruction Types
Instruction Type No. of Instructions
Arithmetic 17
Shift and Rotate 5
Bit-wise Operations 12
Compare Operations 4
Branching 27
Subroutine Calls 6
I/O Instructions 6
Moving Data 29
SREG Bit Operations 18
Program Memory Instructions 11
MCU Control Instructions 6
Total 141
R S Ananda Murthy Assembler Programming of Atmega328P
Assembly Language Statement Format
[label:] mnemonic [operands] [;comment]
Label and comments are optional.
Label field ends with colon (:)
Mnemonic is a short name given to the bit pattern
representing the operation code (opcode) in the instruction.
Mnemonic field should contain either a mnemonic or a
pseudo opcode (assembler directive).
Pseudo opcodes should be preceded by a period (.)
Comment begins with semicolon (;) or double-slash (//).
Like in C, source lines can be continued by means of
having a backslash () as the last character of a line.
Multi-line comment begins with /* and end with */ as in C.
Blank lines can be inserted in the source file to enhance
readability.
R S Ananda Murthy Assembler Programming of Atmega328P
Representation of Data in ALP
Hex numbers Eg. $AB or 0xABCD.
Decimal Eg. 10, 23, 639.
Binary Eg. 0b01101111.
ASCII Eg. ‘2’, ‘A’, ‘B’.
ASCII String Eg. “Hello World” – such strings can be used
only along with DB assembler directive.
R S Ananda Murthy Assembler Programming of Atmega328P
Rules for Labels in ALP
Each label must be unique.
Majority of assemblers permit not more than 6 characters
in a label.
The first character must be an alphabet.
Can contain alpha-numeric characters in both upper and
lower case, question mark (?), period (.), at (@), underline
(_), and dollar sign ($).
Reserved words, assembler directives and mnemonics
given in the instruction set cannot be used as labels.
Use of meaningful labels makes the ALP easier to
understand and maintain.
Avoid vague labels like X1, L1 etc.
R S Ananda Murthy Assembler Programming of Atmega328P
Some Valid and Invalid Labels
Valid Labels Invalid Labels
BEGIN 1BEGIN
LOOP1? 1LOOP?
START@ @START
QUIT_1 _QUIT1, 12345
FINISH.1 LDI, COM, EQU, END
END_@ INCLUDE, SET, ORG
begin LDS, STS, NEG
State reasons for validity or invalidity of labels given above.
R S Ananda Murthy Assembler Programming of Atmega328P
Assembler Directives (Pseudo Opcodes)
Assembler directives are commands given to the
assembler to do certain tasks.
They are not translated to machine code by the assembler.
All assembler directives should be preceded by a period.
Directives recognized by the assembler vary from one
assembler to another assembler.
For a detailed explanation of all assembler directives refer
to the assembler documentation.
R S Ananda Murthy Assembler Programming of Atmega328P
Some Frequently used Assembler Directives
Directive Description
EQU Assigns a label to an expression or a constant.
INCLUDE Read source from another file.
ORG Sets the starting address for assembly.
SET Assigns a value to a label which can be changed later.
DB Define constant byte(s) in program memory or in EEPROM.
DW Define constant word(s) in program memory or in EEPROM.
BYTE Reserve bytes for a variable in SRAM or in EEPROM.
CSEG Defines the start of a Code Segment.
DSEG Defines the start of a Data segment.
ESEG Defines the start of an EEPROM segment.
Refer to Atmel Assembler Manual here: http:
//www.atmel.com/webdoc/avrassembler/index.html
R S Ananda Murthy Assembler Programming of Atmega328P
Addressing Modes in AVR Instruction Set
Method of determining the address of the data/operand/s is
known as addressing mode.
The following addressing modes are available in the instruction
set of Atmega328P –
Register Direct Addressing
Single Register Rd, with Immediate Data
Single Register Rd.
Two Registers Rd and Rr.
I/O Direct Addressing
R S Ananda Murthy Assembler Programming of Atmega328P
Addressing Modes in AVR Instruction Set
Data Memory
Direct Addressing
Indirect Addressing
Indirect Addressing with Displacement.
Indirect Addressing with Pre-decrement.
Indirect Addressing with Post-increment.
Program Memory
Constant Addressing.
Addressing with Post-increment.
Direct Addressing.
Indirect Addressing.
Relative Addressing.
Refer to the Instruction Set Manual for explanation of all the
addressing modes.
R S Ananda Murthy Assembler Programming of Atmega328P
Link to Instruction Set Manual
While programming Atmega328P, it is necessary to constantly
refer to the Instruction Set Manual provided by Atmel to
carefully understand the working of each instruction.
Instruction Set Manual is available for download at:
http://www.atmel.com/Images/
Atmel-0856-AVR-Instruction-Set-Manual.pdf
In the following slides few instructions have been described.
The reader should develop the ability to refer to the document
mentioned above to understand the working of any instruction.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – LDI Rd,K
LDI Rd,K Operation: Rd K
Loads an 8-bit constant K directly into the register Rd.
Rd is any one of the registers from R16 to R31
represented by 4-bit code given beside
1110 K7K6K5K4 dddd K3K2K1K0 2 bytes
Program Counter
dddd
LDI
opcode
Higher
Nibble of K
Code
for Rd
Lower
Nibble of K
Rd
0000R16
0001R17
0010R18
::
1111R31
: PC PC+1
Status Register : Not affectedInstruction Length : 2 bytes
Number of Cycles : 1Addressing Mode : Single Register Immediate
This instruction belongs to Data Transfer Group. Status flags
are not affected by any instruction in this group.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – ADD Rd,Rr
ADD Rd,Rr Operation: Rd
Adds two registers Rd and Rr and places the sum in
the register Rd. Rd and Rr can be any GPR
represented by 5-bit code given beside
2 bytes
Program Counter
Code
ADD
opcode
Rd or Rr
: PC PC+1
Instruction Length : 2 bytes Number of Cycles : 1
Addressing Mode
Rd + Rr
000011 r3 r2 r1 r0d3 d2 d1d0r4 d4
Operands
00000R0
00001R1
00010R2
00011R3
::
11111R31
Flags Affected: Two Registers : All except I and T
This instruction belongs to Arithmetic Logical Group. To
understand how flags are affected refer to explanation given in
the instruction set.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – MOV Rd,Rr
MOV Rd,Rr Operation: Rd
Copies the contents of Rr to Rd. Content of Rr
remains unaltered. Rr and Rd can be any GPR
represented by 5-bit code given beside.
2 bytes
Program Counter
Code
MOV
opcode
Rd or Rr
: PC PC+1
Instruction Length : 2 bytes Number of Cycles : 1
Addressing Mode
Rr
001011 r3 r2 r1 r0d3 d2 d1d0r4 d4
Operands
00000R0
00001R1
00010R2
00011R3
::
11111R31
Flags Affected: Two Registers : None
This instruction belongs to Data Transfer Group. Status flags
are not affected by any instruction in this group.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – COM Rd
COM Rd Operation: Rd
Finds the 1's complement of Rd. Rd can be any
GPR represented by 5-bit code given beside.
2 bytes
Program Counter
CodeRd or Rr
: PC PC+1
Instruction Length : 2 bytes Number of Cycles : 1
Addressing Mode
$FF - Rd
1001010
Operand
00000R0
00001R1
00010R2
00011R3
::
11111R31
Flags Affected: One Register : All except I, T, H.
d3 d2 d1d0d4 0000
COM opcode
This instruction belongs to Arithmetic Logical Group. To
understand how flags are affected refer to explanation given in
the instruction set.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – JMP k
JMP k Operation: PC
Jump to an address within the entire 4M (words) program memory.
This is an unconditional jump instruction which is not available in all
devices. It is available in Atmega328P.
Program Counter : PC k
Instruction Length : 4 bytes (2 words) Number of Cycles : 3
Addressing Mode
k
Flags Affected: Program Memory Direct : None
k3 k2 k1k0k11k10k9k8k15 k14k13k12
110k161001 010k21 k20 k19k18k17
k7 k6 k5k4
This instruction belongs to Branch Group. Flags are not
affected by this instruction.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – RJMP k
RJMP k Operation: PC
Program Counter : PC PC+k+1
Instruction Length : 2 bytes (1 word) Number of Cycles : 2
Addressing Mode
PC + k +1
Flags Affected: Program Memory Relative : None
1100
Relative jump to an address within PC-2K+1 and PC+2K (words). In AVR
MCUs with program memory not exceeding 4K words, this instruction can
address the entire memory from every address location. Here
where . k is the offset. When k is negative, it indicates by how
many words to jump backward. When k is postive, it indicates by how
many words to jump forward.
k11k10 k9 k8 k3 k2 k1k0k7 k6 k5k4
This instruction belongs to Branch Group. Flags are not
affected by this instruction. For shorter jumps it is better to use
RJMP, which is a shorter instruction than JMP.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – LDS Rd,k
LDS Rd,k Operation: Rd
Loads one byte from the data memory to a register Rd. For MCU
with SRAM, the data space consists of the GPRs, I/O memory
and internal SRAM (and external SRAM if applicable). For MCU
without SRAM, the data space consists of the register file only.
The EEPROM has a separate address space. k is 16-bit address
of data memory location.
Program Counter : PC PC+2
Instruction Length : 4 bytes (2 words) Number of Cycles : 2
Addressing Mode
(k)
Flags Affected: Data Memory Direct : None
k3 k2 k1k0k11k10k9k8k15 k14k13k12
00001001 000d4
k7 k6 k5k4
d3 d2 d1d0
This instruction belongs to Data Transfer Group. Status flags
are not affected by any instruction in this group.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – STS k,Rd
STS k,Rd Operation: (k)
Copies a byte from a GPR to a data memory location. For MCU
with SRAM, the data space consists of the GPRs, I/O memory
and internal SRAM (and external SRAM if applicable). For MCU
without SRAM, the data space consists of the register file only.
The EEPROM has a separate address space. k is 16-bit address
of data memory location.
Program Counter : PC PC+2
Instruction Length : 4 bytes (2 words) Number of Cycles : 2
Addressing Mode
Rd
Flags Affected: Data Memory Direct : None
k3 k2 k1k0k11k10k9k8k15 k14k13k12
00001001 001d4
k7 k6 k5k4
d3 d2 d1d0
This instruction belongs to Data Transfer Group. Status flags
are not affected by any instruction in this group.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – IN Rd,A
IN Rd,A Operation: Rd
Loads 1-byte of data from the I/O Space (Ports, Timers, Configuration
Registers, etc.) into a GPR Rd represented by 5-bit code
and A is address of I/O Space specified by
Program Counter : PC PC+1
Instruction Length : 2 bytes (1 word) Number of Cycles : 1
Addressing Mode
I/O(A)
Flags Affected: I/O Direct : None
10110 d3 d2 d1d0d4A5A4
A3A2 A1A0
d3 d2 d1d0d4
IN
Opcode
Operands
A5A4
A3A2 A1A0
This instruction belongs to Data Transfer Group. Status flags
are not affected by any instruction in this group.
R S Ananda Murthy Assembler Programming of Atmega328P
Example of Instruction – OUT A,Rr
OUT A,Rr Operation: I/O(A)
Loads 1-byte of data from Rr to I/O Space (Ports, Timers, Configuration
Registers, etc.). Rr is represented by the 5-bit code
and A is address of I/O Space specified by
Program Counter : PC PC+1
Instruction Length : 2 bytes (1 word) Number of Cycles : 1
Addressing Mode
Rr
Flags Affected: I/O Direct : None
10111 d3 d2 d1d0d4A5A4
A3A2 A1A0
d3 d2 d1d0d4
OUT
Opcode
Operands
A5A4
A3A2 A1A0
This instruction belongs to Data Transfer Group. Status flags
are not affected by any instruction in this group.
R S Ananda Murthy Assembler Programming of Atmega328P
Steps in Assembly Language Programming
1 Use a text editor to create/edit the source file and save it as
.asm file. Statements can be typed in lower or upper case.
2 Feed the .asm file created above to the assembler.
3 If the assembler does not find any errors in the .asm file,
then, it produces an object file (.obj), a hex file (.hex), an
EEPROM file (.eep), a list file (.lst), and a map file (.map).
4 Test the program by running it on a simulator or by using a
debugger. Repeat Steps 1-4 until the program is found to
be working properly.
5 Download the working .hex file to the flash memory and
.eep file to the EEPROM of MCU using a programmer.
The list file is a text file, which contains the source statements
along with the machine code assembled by the program.
R S Ananda Murthy Assembler Programming of Atmega328P
Points to be Remembered in ALP of Atmega328P
Write a JMP to branch to the user program at the reset
addres $0000.
Do not write user program in the area $0000-$003F
reserved for interrupt vectors.
User program can be loaded immediately after the
interrupt vector locations, say from $0040.
If bootloader has been used, ensure that the user program
does not over write on the bootloader.
R S Ananda Murthy Assembler Programming of Atmega328P
Program to Add Two Numbers
Refer to Instruction Set Manual to understand the working
of LPM instruction.
Trace through this program and find the contents of
registers and memory at each step.
R S Ananda Murthy Assembler Programming of Atmega328P
License
This work is licensed under a
Creative Commons Attribution 4.0 International License.
R S Ananda Murthy Assembler Programming of Atmega328P

Más contenido relacionado

La actualidad más candente

Chapter 1 microprocessor introduction
Chapter 1 microprocessor introductionChapter 1 microprocessor introduction
Chapter 1 microprocessor introductionShubham Singh
 
Introduction to arm processor
Introduction to arm processorIntroduction to arm processor
Introduction to arm processorRAMPRAKASHT1
 
L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1rsamurti
 
Advance Microcontroller AVR
Advance Microcontroller AVRAdvance Microcontroller AVR
Advance Microcontroller AVRDaksh Raj Chopra
 
Microprocessor Microcontroller Interview & Viva Question.pdf
Microprocessor Microcontroller Interview & Viva Question.pdfMicroprocessor Microcontroller Interview & Viva Question.pdf
Microprocessor Microcontroller Interview & Viva Question.pdfEngineering Funda
 
Interrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptxInterrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptxSujalKumar73
 
ATmega32-AVR microcontrollers-Part I
ATmega32-AVR microcontrollers-Part IATmega32-AVR microcontrollers-Part I
ATmega32-AVR microcontrollers-Part IVineethMP2
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set ArchitectureDilum Bandara
 
Lect 3 ARM PROCESSOR ARCHITECTURE
Lect 3  ARM PROCESSOR ARCHITECTURE Lect 3  ARM PROCESSOR ARCHITECTURE
Lect 3 ARM PROCESSOR ARCHITECTURE Dr.YNM
 
Interfacing of io device to 8085
Interfacing of io device to 8085Interfacing of io device to 8085
Interfacing of io device to 8085Nitin Ahire
 
VHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDLVHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDLRevathi Subramaniam
 
Unit II arm 7 Instruction Set
Unit II arm 7 Instruction SetUnit II arm 7 Instruction Set
Unit II arm 7 Instruction SetDr. Pankaj Zope
 
建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card艾鍗科技
 

La actualidad más candente (20)

Chapter 1 microprocessor introduction
Chapter 1 microprocessor introductionChapter 1 microprocessor introduction
Chapter 1 microprocessor introduction
 
Introduction to arm processor
Introduction to arm processorIntroduction to arm processor
Introduction to arm processor
 
L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1L8 understanding-atmega328 p-1
L8 understanding-atmega328 p-1
 
Arm architecture
Arm architectureArm architecture
Arm architecture
 
Subtractor (1)
Subtractor (1)Subtractor (1)
Subtractor (1)
 
Advance Microcontroller AVR
Advance Microcontroller AVRAdvance Microcontroller AVR
Advance Microcontroller AVR
 
ARM Architecture
ARM ArchitectureARM Architecture
ARM Architecture
 
Microprocessor Microcontroller Interview & Viva Question.pdf
Microprocessor Microcontroller Interview & Viva Question.pdfMicroprocessor Microcontroller Interview & Viva Question.pdf
Microprocessor Microcontroller Interview & Viva Question.pdf
 
Interrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptxInterrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptx
 
ATmega32-AVR microcontrollers-Part I
ATmega32-AVR microcontrollers-Part IATmega32-AVR microcontrollers-Part I
ATmega32-AVR microcontrollers-Part I
 
Unit 4-booth algorithm
Unit 4-booth algorithmUnit 4-booth algorithm
Unit 4-booth algorithm
 
Interrupts and types of interrupts
Interrupts and types of interruptsInterrupts and types of interrupts
Interrupts and types of interrupts
 
Interrupts in pic
Interrupts in picInterrupts in pic
Interrupts in pic
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
Lect 3 ARM PROCESSOR ARCHITECTURE
Lect 3  ARM PROCESSOR ARCHITECTURE Lect 3  ARM PROCESSOR ARCHITECTURE
Lect 3 ARM PROCESSOR ARCHITECTURE
 
Interfacing of io device to 8085
Interfacing of io device to 8085Interfacing of io device to 8085
Interfacing of io device to 8085
 
VHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDLVHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDL
 
Unit II arm 7 Instruction Set
Unit II arm 7 Instruction SetUnit II arm 7 Instruction Set
Unit II arm 7 Instruction Set
 
ARM Introduction
ARM IntroductionARM Introduction
ARM Introduction
 
建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card
 

Destacado

L4 speeding-up-execution
L4 speeding-up-executionL4 speeding-up-execution
L4 speeding-up-executionrsamurti
 
L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 prsamurti
 
L16 usart-atmega328 p
L16 usart-atmega328 pL16 usart-atmega328 p
L16 usart-atmega328 prsamurti
 
Introduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXIntroduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXrsamurti
 
L1 intro-to-mpu-mcu
L1 intro-to-mpu-mcuL1 intro-to-mpu-mcu
L1 intro-to-mpu-mcursamurti
 
L5 data-parallel-computers
L5 data-parallel-computersL5 data-parallel-computers
L5 data-parallel-computersrsamurti
 
L6 primary-memory
L6 primary-memoryL6 primary-memory
L6 primary-memoryrsamurti
 
Transformers
TransformersTransformers
Transformersrsamurti
 
L3 instruction-execution-steps
L3 instruction-execution-stepsL3 instruction-execution-steps
L3 instruction-execution-stepsrsamurti
 
Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2rsamurti
 
L13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 pL13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 prsamurti
 
L7 starting-to-use-mcu
L7 starting-to-use-mcuL7 starting-to-use-mcu
L7 starting-to-use-mcursamurti
 
Lecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power ElectronicsLecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power Electronicsrsamurti
 
Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4rsamurti
 
Lecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power ElectronicsLecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power Electronicsrsamurti
 
L14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 pL14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 prsamurti
 
Three phase-circuits
Three phase-circuitsThree phase-circuits
Three phase-circuitsrsamurti
 
Synchronous generators
Synchronous generatorsSynchronous generators
Synchronous generatorsrsamurti
 
Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1rsamurti
 
Sowing and planting machines
Sowing and planting machinesSowing and planting machines
Sowing and planting machinesagriyouthnepal
 

Destacado (20)

L4 speeding-up-execution
L4 speeding-up-executionL4 speeding-up-execution
L4 speeding-up-execution
 
L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 p
 
L16 usart-atmega328 p
L16 usart-atmega328 pL16 usart-atmega328 p
L16 usart-atmega328 p
 
Introduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeXIntroduction to-Tex-and-LaTeX
Introduction to-Tex-and-LaTeX
 
L1 intro-to-mpu-mcu
L1 intro-to-mpu-mcuL1 intro-to-mpu-mcu
L1 intro-to-mpu-mcu
 
L5 data-parallel-computers
L5 data-parallel-computersL5 data-parallel-computers
L5 data-parallel-computers
 
L6 primary-memory
L6 primary-memoryL6 primary-memory
L6 primary-memory
 
Transformers
TransformersTransformers
Transformers
 
L3 instruction-execution-steps
L3 instruction-execution-stepsL3 instruction-execution-steps
L3 instruction-execution-steps
 
Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2Lecture-5 : Semiconductor Power Switching Devices-2
Lecture-5 : Semiconductor Power Switching Devices-2
 
L13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 pL13 interrupts-in-atmega328 p
L13 interrupts-in-atmega328 p
 
L7 starting-to-use-mcu
L7 starting-to-use-mcuL7 starting-to-use-mcu
L7 starting-to-use-mcu
 
Lecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power ElectronicsLecture-3 : More Applications of Power Electronics
Lecture-3 : More Applications of Power Electronics
 
Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4Lecture-7 : Semiconductor Power Switching Devices-4
Lecture-7 : Semiconductor Power Switching Devices-4
 
Lecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power ElectronicsLecture-2 : Applications of Power Electronics
Lecture-2 : Applications of Power Electronics
 
L14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 pL14 kb-lcd-interfacing-with-atmega328 p
L14 kb-lcd-interfacing-with-atmega328 p
 
Three phase-circuits
Three phase-circuitsThree phase-circuits
Three phase-circuits
 
Synchronous generators
Synchronous generatorsSynchronous generators
Synchronous generators
 
Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1Lecture-4 : Semiconductor Power Switching Devices-1
Lecture-4 : Semiconductor Power Switching Devices-1
 
Sowing and planting machines
Sowing and planting machinesSowing and planting machines
Sowing and planting machines
 

Similar a L10 assembly-language-programming-of-atmega328 p

Similar a L10 assembly-language-programming-of-atmega328 p (20)

Assembler Programming
Assembler ProgrammingAssembler Programming
Assembler Programming
 
Assignment
AssignmentAssignment
Assignment
 
Bascom avr-course
Bascom avr-courseBascom avr-course
Bascom avr-course
 
ARM.ppt
ARM.pptARM.ppt
ARM.ppt
 
Arm
ArmArm
Arm
 
The ARM Architecture: ARM : ARM Architecture
The ARM Architecture: ARM : ARM ArchitectureThe ARM Architecture: ARM : ARM Architecture
The ARM Architecture: ARM : ARM Architecture
 
Arm
ArmArm
Arm
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
Unit 4 _ ARM Processors .pptx
Unit 4 _ ARM Processors .pptxUnit 4 _ ARM Processors .pptx
Unit 4 _ ARM Processors .pptx
 
Doc8453
Doc8453Doc8453
Doc8453
 
Effisiensi prog atmel
Effisiensi prog atmelEffisiensi prog atmel
Effisiensi prog atmel
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
Module 2 ARM CORTEX M3 Instruction Set and Programming
Module 2 ARM CORTEX M3 Instruction Set and ProgrammingModule 2 ARM CORTEX M3 Instruction Set and Programming
Module 2 ARM CORTEX M3 Instruction Set and Programming
 
12 mt06ped008
12 mt06ped008 12 mt06ped008
12 mt06ped008
 
Emb day2 8051
Emb day2 8051Emb day2 8051
Emb day2 8051
 
MICROPROCESSOR_Notes.pptx
MICROPROCESSOR_Notes.pptxMICROPROCESSOR_Notes.pptx
MICROPROCESSOR_Notes.pptx
 
gayathri.p.pptx
gayathri.p.pptxgayathri.p.pptx
gayathri.p.pptx
 
Microcontroller directives
Microcontroller directivesMicrocontroller directives
Microcontroller directives
 
microprocessor
 microprocessor microprocessor
microprocessor
 

Último

DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectssuserb6619e
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxachiever3003
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxNiranjanYadav41
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectErbil Polytechnic University
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate productionChinnuNinan
 

Último (20)

DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptx
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate production
 

L10 assembly-language-programming-of-atmega328 p

  • 1. Assembler Programming of Atmega328P (Lecture-10) 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
  • 2. Language Options to Program Atmega328P Higher Level Languages (HLP) – mostly machine independent. C/C++ Wiring. This is nothing but higher level abstractions implemented using functions written in C/C++ to make the microcontroller programming easier. Arduino IDE uses Wiring. Assembly Language (ALP) – machine dependent. ALP is needs more effort as compared to HLP because it requires the knowledge of the instruction set of the MCU in addition to the knowledge of the internal architecture. R S Ananda Murthy Assembler Programming of Atmega328P
  • 3. Why Learn ALP? ALP helps in better understanding of the internal architecture of MCU. ALP gives direct access to all the hardware blocks in side the MCU which may not be possible by HLP. ALP helps in writing highly optimized code (shortest, fastest program). This is very essential in time-critical and space-critical applications. Knowledge of ALP helps in detecting bugs in a machine language program by translating the machine code back into mnemonics using a disassembler which is a software that performs the translation. Industries engaged in the design of embedded systems expect ALP and HLP skills in their prospective employees. R S Ananda Murthy Assembler Programming of Atmega328P
  • 4. What Facilities are Needed for ALP? A text editor to create the assembly language source file which is typically stored as a .asm file. An assembler which is a software that parses the source file and outputs the machine language instructions as a .hex file. A debugger or simulator to check the working of the program. A programmer – which is a hardware – along with its related software to download .hex file to the MCU. Note that these facilities are available on both GNU Linux and Windows platforms. R S Ananda Murthy Assembler Programming of Atmega328P
  • 5. Classification of Atmega328P Instructions There are 141 instructions in the instruction set of Atmega328P. They are classified as — 1 Arithmetic and Logic Instructions. 2 Branch Instructions 3 Data Transfer Instructions 4 Bit and Bit Test Instructions 5 MCU Control Instructions Majority of the instructions are 2-bytes in length. Only LDS, STS, JMP and CALL instructions are 4-bytes in length. R S Ananda Murthy Assembler Programming of Atmega328P
  • 6. Atmega328P Instruction Types Instruction Type No. of Instructions Arithmetic 17 Shift and Rotate 5 Bit-wise Operations 12 Compare Operations 4 Branching 27 Subroutine Calls 6 I/O Instructions 6 Moving Data 29 SREG Bit Operations 18 Program Memory Instructions 11 MCU Control Instructions 6 Total 141 R S Ananda Murthy Assembler Programming of Atmega328P
  • 7. Assembly Language Statement Format [label:] mnemonic [operands] [;comment] Label and comments are optional. Label field ends with colon (:) Mnemonic is a short name given to the bit pattern representing the operation code (opcode) in the instruction. Mnemonic field should contain either a mnemonic or a pseudo opcode (assembler directive). Pseudo opcodes should be preceded by a period (.) Comment begins with semicolon (;) or double-slash (//). Like in C, source lines can be continued by means of having a backslash () as the last character of a line. Multi-line comment begins with /* and end with */ as in C. Blank lines can be inserted in the source file to enhance readability. R S Ananda Murthy Assembler Programming of Atmega328P
  • 8. Representation of Data in ALP Hex numbers Eg. $AB or 0xABCD. Decimal Eg. 10, 23, 639. Binary Eg. 0b01101111. ASCII Eg. ‘2’, ‘A’, ‘B’. ASCII String Eg. “Hello World” – such strings can be used only along with DB assembler directive. R S Ananda Murthy Assembler Programming of Atmega328P
  • 9. Rules for Labels in ALP Each label must be unique. Majority of assemblers permit not more than 6 characters in a label. The first character must be an alphabet. Can contain alpha-numeric characters in both upper and lower case, question mark (?), period (.), at (@), underline (_), and dollar sign ($). Reserved words, assembler directives and mnemonics given in the instruction set cannot be used as labels. Use of meaningful labels makes the ALP easier to understand and maintain. Avoid vague labels like X1, L1 etc. R S Ananda Murthy Assembler Programming of Atmega328P
  • 10. Some Valid and Invalid Labels Valid Labels Invalid Labels BEGIN 1BEGIN LOOP1? 1LOOP? START@ @START QUIT_1 _QUIT1, 12345 FINISH.1 LDI, COM, EQU, END END_@ INCLUDE, SET, ORG begin LDS, STS, NEG State reasons for validity or invalidity of labels given above. R S Ananda Murthy Assembler Programming of Atmega328P
  • 11. Assembler Directives (Pseudo Opcodes) Assembler directives are commands given to the assembler to do certain tasks. They are not translated to machine code by the assembler. All assembler directives should be preceded by a period. Directives recognized by the assembler vary from one assembler to another assembler. For a detailed explanation of all assembler directives refer to the assembler documentation. R S Ananda Murthy Assembler Programming of Atmega328P
  • 12. Some Frequently used Assembler Directives Directive Description EQU Assigns a label to an expression or a constant. INCLUDE Read source from another file. ORG Sets the starting address for assembly. SET Assigns a value to a label which can be changed later. DB Define constant byte(s) in program memory or in EEPROM. DW Define constant word(s) in program memory or in EEPROM. BYTE Reserve bytes for a variable in SRAM or in EEPROM. CSEG Defines the start of a Code Segment. DSEG Defines the start of a Data segment. ESEG Defines the start of an EEPROM segment. Refer to Atmel Assembler Manual here: http: //www.atmel.com/webdoc/avrassembler/index.html R S Ananda Murthy Assembler Programming of Atmega328P
  • 13. Addressing Modes in AVR Instruction Set Method of determining the address of the data/operand/s is known as addressing mode. The following addressing modes are available in the instruction set of Atmega328P – Register Direct Addressing Single Register Rd, with Immediate Data Single Register Rd. Two Registers Rd and Rr. I/O Direct Addressing R S Ananda Murthy Assembler Programming of Atmega328P
  • 14. Addressing Modes in AVR Instruction Set Data Memory Direct Addressing Indirect Addressing Indirect Addressing with Displacement. Indirect Addressing with Pre-decrement. Indirect Addressing with Post-increment. Program Memory Constant Addressing. Addressing with Post-increment. Direct Addressing. Indirect Addressing. Relative Addressing. Refer to the Instruction Set Manual for explanation of all the addressing modes. R S Ananda Murthy Assembler Programming of Atmega328P
  • 15. Link to Instruction Set Manual While programming Atmega328P, it is necessary to constantly refer to the Instruction Set Manual provided by Atmel to carefully understand the working of each instruction. Instruction Set Manual is available for download at: http://www.atmel.com/Images/ Atmel-0856-AVR-Instruction-Set-Manual.pdf In the following slides few instructions have been described. The reader should develop the ability to refer to the document mentioned above to understand the working of any instruction. R S Ananda Murthy Assembler Programming of Atmega328P
  • 16. Example of Instruction – LDI Rd,K LDI Rd,K Operation: Rd K Loads an 8-bit constant K directly into the register Rd. Rd is any one of the registers from R16 to R31 represented by 4-bit code given beside 1110 K7K6K5K4 dddd K3K2K1K0 2 bytes Program Counter dddd LDI opcode Higher Nibble of K Code for Rd Lower Nibble of K Rd 0000R16 0001R17 0010R18 :: 1111R31 : PC PC+1 Status Register : Not affectedInstruction Length : 2 bytes Number of Cycles : 1Addressing Mode : Single Register Immediate This instruction belongs to Data Transfer Group. Status flags are not affected by any instruction in this group. R S Ananda Murthy Assembler Programming of Atmega328P
  • 17. Example of Instruction – ADD Rd,Rr ADD Rd,Rr Operation: Rd Adds two registers Rd and Rr and places the sum in the register Rd. Rd and Rr can be any GPR represented by 5-bit code given beside 2 bytes Program Counter Code ADD opcode Rd or Rr : PC PC+1 Instruction Length : 2 bytes Number of Cycles : 1 Addressing Mode Rd + Rr 000011 r3 r2 r1 r0d3 d2 d1d0r4 d4 Operands 00000R0 00001R1 00010R2 00011R3 :: 11111R31 Flags Affected: Two Registers : All except I and T This instruction belongs to Arithmetic Logical Group. To understand how flags are affected refer to explanation given in the instruction set. R S Ananda Murthy Assembler Programming of Atmega328P
  • 18. Example of Instruction – MOV Rd,Rr MOV Rd,Rr Operation: Rd Copies the contents of Rr to Rd. Content of Rr remains unaltered. Rr and Rd can be any GPR represented by 5-bit code given beside. 2 bytes Program Counter Code MOV opcode Rd or Rr : PC PC+1 Instruction Length : 2 bytes Number of Cycles : 1 Addressing Mode Rr 001011 r3 r2 r1 r0d3 d2 d1d0r4 d4 Operands 00000R0 00001R1 00010R2 00011R3 :: 11111R31 Flags Affected: Two Registers : None This instruction belongs to Data Transfer Group. Status flags are not affected by any instruction in this group. R S Ananda Murthy Assembler Programming of Atmega328P
  • 19. Example of Instruction – COM Rd COM Rd Operation: Rd Finds the 1's complement of Rd. Rd can be any GPR represented by 5-bit code given beside. 2 bytes Program Counter CodeRd or Rr : PC PC+1 Instruction Length : 2 bytes Number of Cycles : 1 Addressing Mode $FF - Rd 1001010 Operand 00000R0 00001R1 00010R2 00011R3 :: 11111R31 Flags Affected: One Register : All except I, T, H. d3 d2 d1d0d4 0000 COM opcode This instruction belongs to Arithmetic Logical Group. To understand how flags are affected refer to explanation given in the instruction set. R S Ananda Murthy Assembler Programming of Atmega328P
  • 20. Example of Instruction – JMP k JMP k Operation: PC Jump to an address within the entire 4M (words) program memory. This is an unconditional jump instruction which is not available in all devices. It is available in Atmega328P. Program Counter : PC k Instruction Length : 4 bytes (2 words) Number of Cycles : 3 Addressing Mode k Flags Affected: Program Memory Direct : None k3 k2 k1k0k11k10k9k8k15 k14k13k12 110k161001 010k21 k20 k19k18k17 k7 k6 k5k4 This instruction belongs to Branch Group. Flags are not affected by this instruction. R S Ananda Murthy Assembler Programming of Atmega328P
  • 21. Example of Instruction – RJMP k RJMP k Operation: PC Program Counter : PC PC+k+1 Instruction Length : 2 bytes (1 word) Number of Cycles : 2 Addressing Mode PC + k +1 Flags Affected: Program Memory Relative : None 1100 Relative jump to an address within PC-2K+1 and PC+2K (words). In AVR MCUs with program memory not exceeding 4K words, this instruction can address the entire memory from every address location. Here where . k is the offset. When k is negative, it indicates by how many words to jump backward. When k is postive, it indicates by how many words to jump forward. k11k10 k9 k8 k3 k2 k1k0k7 k6 k5k4 This instruction belongs to Branch Group. Flags are not affected by this instruction. For shorter jumps it is better to use RJMP, which is a shorter instruction than JMP. R S Ananda Murthy Assembler Programming of Atmega328P
  • 22. Example of Instruction – LDS Rd,k LDS Rd,k Operation: Rd Loads one byte from the data memory to a register Rd. For MCU with SRAM, the data space consists of the GPRs, I/O memory and internal SRAM (and external SRAM if applicable). For MCU without SRAM, the data space consists of the register file only. The EEPROM has a separate address space. k is 16-bit address of data memory location. Program Counter : PC PC+2 Instruction Length : 4 bytes (2 words) Number of Cycles : 2 Addressing Mode (k) Flags Affected: Data Memory Direct : None k3 k2 k1k0k11k10k9k8k15 k14k13k12 00001001 000d4 k7 k6 k5k4 d3 d2 d1d0 This instruction belongs to Data Transfer Group. Status flags are not affected by any instruction in this group. R S Ananda Murthy Assembler Programming of Atmega328P
  • 23. Example of Instruction – STS k,Rd STS k,Rd Operation: (k) Copies a byte from a GPR to a data memory location. For MCU with SRAM, the data space consists of the GPRs, I/O memory and internal SRAM (and external SRAM if applicable). For MCU without SRAM, the data space consists of the register file only. The EEPROM has a separate address space. k is 16-bit address of data memory location. Program Counter : PC PC+2 Instruction Length : 4 bytes (2 words) Number of Cycles : 2 Addressing Mode Rd Flags Affected: Data Memory Direct : None k3 k2 k1k0k11k10k9k8k15 k14k13k12 00001001 001d4 k7 k6 k5k4 d3 d2 d1d0 This instruction belongs to Data Transfer Group. Status flags are not affected by any instruction in this group. R S Ananda Murthy Assembler Programming of Atmega328P
  • 24. Example of Instruction – IN Rd,A IN Rd,A Operation: Rd Loads 1-byte of data from the I/O Space (Ports, Timers, Configuration Registers, etc.) into a GPR Rd represented by 5-bit code and A is address of I/O Space specified by Program Counter : PC PC+1 Instruction Length : 2 bytes (1 word) Number of Cycles : 1 Addressing Mode I/O(A) Flags Affected: I/O Direct : None 10110 d3 d2 d1d0d4A5A4 A3A2 A1A0 d3 d2 d1d0d4 IN Opcode Operands A5A4 A3A2 A1A0 This instruction belongs to Data Transfer Group. Status flags are not affected by any instruction in this group. R S Ananda Murthy Assembler Programming of Atmega328P
  • 25. Example of Instruction – OUT A,Rr OUT A,Rr Operation: I/O(A) Loads 1-byte of data from Rr to I/O Space (Ports, Timers, Configuration Registers, etc.). Rr is represented by the 5-bit code and A is address of I/O Space specified by Program Counter : PC PC+1 Instruction Length : 2 bytes (1 word) Number of Cycles : 1 Addressing Mode Rr Flags Affected: I/O Direct : None 10111 d3 d2 d1d0d4A5A4 A3A2 A1A0 d3 d2 d1d0d4 OUT Opcode Operands A5A4 A3A2 A1A0 This instruction belongs to Data Transfer Group. Status flags are not affected by any instruction in this group. R S Ananda Murthy Assembler Programming of Atmega328P
  • 26. Steps in Assembly Language Programming 1 Use a text editor to create/edit the source file and save it as .asm file. Statements can be typed in lower or upper case. 2 Feed the .asm file created above to the assembler. 3 If the assembler does not find any errors in the .asm file, then, it produces an object file (.obj), a hex file (.hex), an EEPROM file (.eep), a list file (.lst), and a map file (.map). 4 Test the program by running it on a simulator or by using a debugger. Repeat Steps 1-4 until the program is found to be working properly. 5 Download the working .hex file to the flash memory and .eep file to the EEPROM of MCU using a programmer. The list file is a text file, which contains the source statements along with the machine code assembled by the program. R S Ananda Murthy Assembler Programming of Atmega328P
  • 27. Points to be Remembered in ALP of Atmega328P Write a JMP to branch to the user program at the reset addres $0000. Do not write user program in the area $0000-$003F reserved for interrupt vectors. User program can be loaded immediately after the interrupt vector locations, say from $0040. If bootloader has been used, ensure that the user program does not over write on the bootloader. R S Ananda Murthy Assembler Programming of Atmega328P
  • 28. Program to Add Two Numbers Refer to Instruction Set Manual to understand the working of LPM instruction. Trace through this program and find the contents of registers and memory at each step. R S Ananda Murthy Assembler Programming of Atmega328P
  • 29. License This work is licensed under a Creative Commons Attribution 4.0 International License. R S Ananda Murthy Assembler Programming of Atmega328P