SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Introduction to AVR Microcontroller
Introduction to AVR Microcontroller Introduction to AVR Microcontroller
Introduction to AVR Microcontroller Mahmoud Sadat
 
L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2rsamurti
 
Microcontoller and Embedded System
Microcontoller and Embedded SystemMicrocontoller and Embedded System
Microcontoller and Embedded SystemKaran Thakkar
 
Introduction to arm architecture
Introduction to arm architectureIntroduction to arm architecture
Introduction to arm architectureZakaria Gomaa
 
Pic 18 microcontroller
Pic 18 microcontrollerPic 18 microcontroller
Pic 18 microcontrollerAshish Ranjan
 
ARM architcture
ARM architcture ARM architcture
ARM architcture Hossam Adel
 
C Programming For Embedded Systems
C Programming For Embedded SystemsC Programming For Embedded Systems
C Programming For Embedded SystemsGanesh Samarthyam
 
Msp 430 module 3
Msp 430 module 3Msp 430 module 3
Msp 430 module 3SARALA T
 
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...Shrishail Bhat
 
Module 1 - ARM 32 Bit Microcontroller
Module 1 - ARM 32 Bit Microcontroller Module 1 - ARM 32 Bit Microcontroller
Module 1 - ARM 32 Bit Microcontroller Amogha Bandrikalli
 
PIC 16F877 micro controller by Gaurav raikar
PIC 16F877 micro controller by Gaurav raikarPIC 16F877 micro controller by Gaurav raikar
PIC 16F877 micro controller by Gaurav raikarGauravRaikar3
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersMohamed Tarek
 
8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdf8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdfSrikrishna Thota
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Aarav Soni
 

Tendances (20)

Introduction to AVR Microcontroller
Introduction to AVR Microcontroller Introduction to AVR Microcontroller
Introduction to AVR Microcontroller
 
L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2L9 understanding-atmega328 p-2
L9 understanding-atmega328 p-2
 
Microcontoller and Embedded System
Microcontoller and Embedded SystemMicrocontoller and Embedded System
Microcontoller and Embedded System
 
ARM Architecture
ARM ArchitectureARM Architecture
ARM Architecture
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Introduction to arm architecture
Introduction to arm architectureIntroduction to arm architecture
Introduction to arm architecture
 
Pic 18 microcontroller
Pic 18 microcontrollerPic 18 microcontroller
Pic 18 microcontroller
 
ARM architcture
ARM architcture ARM architcture
ARM architcture
 
C Programming For Embedded Systems
C Programming For Embedded SystemsC Programming For Embedded Systems
C Programming For Embedded Systems
 
ARM Architecture
ARM ArchitectureARM Architecture
ARM Architecture
 
Msp 430 module 3
Msp 430 module 3Msp 430 module 3
Msp 430 module 3
 
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...
Embedded Systems (18EC62) - ARM Cortex-M3 Instruction Set and Programming (Mo...
 
Module 1 - ARM 32 Bit Microcontroller
Module 1 - ARM 32 Bit Microcontroller Module 1 - ARM 32 Bit Microcontroller
Module 1 - ARM 32 Bit Microcontroller
 
PIC 16F877 micro controller by Gaurav raikar
PIC 16F877 micro controller by Gaurav raikarPIC 16F877 micro controller by Gaurav raikar
PIC 16F877 micro controller by Gaurav raikar
 
Introduction to Avr Microcontrollers
Introduction to Avr MicrocontrollersIntroduction to Avr Microcontrollers
Introduction to Avr Microcontrollers
 
ARM- Programmer's Model
ARM- Programmer's ModelARM- Programmer's Model
ARM- Programmer's Model
 
8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdf8051 Architecture and PIN Configuration.pdf
8051 Architecture and PIN Configuration.pdf
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
Pentium processor
Pentium processorPentium processor
Pentium processor
 
8259 a
8259 a8259 a
8259 a
 

En vedette

L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 prsamurti
 
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
 

En vedette (20)

L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
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
 

Similaire à L10 assembly-language-programming-of-atmega328 p

Similaire à 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
 
Microprocessor Microcontroller Interview & Viva Question.pdf
Microprocessor Microcontroller Interview & Viva Question.pdfMicroprocessor Microcontroller Interview & Viva Question.pdf
Microprocessor Microcontroller Interview & Viva Question.pdf
 
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
 
Arm architecture
Arm architectureArm architecture
Arm architecture
 
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
 

Dernier

Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate productionChinnuNinan
 
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
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
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
 
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
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptbibisarnayak0
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
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
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
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
 

Dernier (20)

Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate production
 
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...
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
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
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.ppt
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
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
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
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
 

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