L10 assembly-language-programming-of-atmega328 p

29
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

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

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

Assembler Programming of Atmega328P(Lecture-10)

R S Ananda Murthy

Associate Professor and HeadDepartment of Electrical & Electronics Engineering,

Sri Jayachamarajendra College of Engineering,Mysore 570 006

R S Ananda Murthy Assembler Programming of Atmega328P

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

Language Options to Program Atmega328P

Higher Level Languages (HLP) – mostly machineindependent.

C/C++Wiring. This is nothing but higher level abstractionsimplemented using functions written in C/C++ to make themicrocontroller programming easier. Arduino IDE usesWiring.

Assembly Language (ALP) – machine dependent.

ALP is needs more effort as compared to HLP because itrequires the knowledge of the instruction set of the MCU inaddition to the knowledge of the internal architecture.

R S Ananda Murthy Assembler Programming of Atmega328P

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

Why Learn ALP?

ALP helps in better understanding of the internalarchitecture of MCU.ALP gives direct access to all the hardware blocks in sidethe 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 andspace-critical applications.Knowledge of ALP helps in detecting bugs in a machinelanguage program by translating the machine code backinto mnemonics using a disassembler which is a softwarethat performs the translation.Industries engaged in the design of embedded systemsexpect ALP and HLP skills in their prospective employees.

R S Ananda Murthy Assembler Programming of Atmega328P

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

What Facilities are Needed for ALP?

A text editor to create the assembly language source filewhich is typically stored as a .asm file.An assembler which is a software that parses the sourcefile and outputs the machine language instructions as a.hex file.A debugger or simulator to check the working of theprogram.A programmer – which is a hardware – along with itsrelated software to download .hex file to the MCU.

Note that these facilities are available on both GNU Linux andWindows platforms.

R S Ananda Murthy Assembler Programming of Atmega328P

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

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 Instructions3 Data Transfer Instructions4 Bit and Bit Test Instructions5 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

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

Atmega328P Instruction Types

Instruction Type No. of InstructionsArithmetic 17

Shift and Rotate 5Bit-wise Operations 12

Compare Operations 4Branching 27

Subroutine Calls 6I/O Instructions 6

Moving Data 29SREG Bit Operations 18

Program Memory Instructions 11MCU Control Instructions 6

Total 141

R S Ananda Murthy Assembler Programming of Atmega328P

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

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 patternrepresenting the operation code (opcode) in the instruction.Mnemonic field should contain either a mnemonic or apseudo 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 ofhaving 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 enhancereadability.

R S Ananda Murthy Assembler Programming of Atmega328P

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

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 usedonly along with DB assembler directive.

R S Ananda Murthy Assembler Programming of Atmega328P

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

Rules for Labels in ALP

Each label must be unique.Majority of assemblers permit not more than 6 charactersin a label.The first character must be an alphabet.Can contain alpha-numeric characters in both upper andlower case, question mark (?), period (.), at (@), underline(_), and dollar sign ($).Reserved words, assembler directives and mnemonicsgiven in the instruction set cannot be used as labels.Use of meaningful labels makes the ALP easier tounderstand and maintain.Avoid vague labels like X1, L1 etc.

R S Ananda Murthy Assembler Programming of Atmega328P

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

Some Valid and Invalid Labels

Valid Labels Invalid LabelsBEGIN 1BEGIN

LOOP1? 1LOOP?START@ @STARTQUIT_1 _QUIT1, 12345

FINISH.1 LDI, COM, EQU, ENDEND_@ 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

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

Assembler Directives (Pseudo Opcodes)

Assembler directives are commands given to theassembler 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 oneassembler to another assembler.For a detailed explanation of all assembler directives referto the assembler documentation.

R S Ananda Murthy Assembler Programming of Atmega328P

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

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

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

Addressing Modes in AVR Instruction Set

Method of determining the address of the data/operand/s isknown as addressing mode.

The following addressing modes are available in the instructionset of Atmega328P –

Register Direct Addressing

Single Register Rd, with Immediate DataSingle Register Rd.Two Registers Rd and Rr.

I/O Direct Addressing

R S Ananda Murthy Assembler Programming of Atmega328P

Page 14: L10 assembly-language-programming-of-atmega328 p

Addressing Modes in AVR Instruction Set

Data Memory

Direct AddressingIndirect AddressingIndirect 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 theaddressing modes.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 15: L10 assembly-language-programming-of-atmega328 p

Link to Instruction Set Manual

While programming Atmega328P, it is necessary to constantlyrefer to the Instruction Set Manual provided by Atmel tocarefully 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 documentmentioned above to understand the working of any instruction.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 16: L10 assembly-language-programming-of-atmega328 p

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 R31represented by 4-bit code given beside

1110 K7K6K5K4 dddd K3K2K1K0 2 bytes

Program Counter

dddd

LDIopcode

HigherNibble of K

Codefor Rd

LowerNibble of K

Rd

0000R16

0001R17

0010R18

::

1111R31

: PC PC+1Status Register : Not affectedInstruction Length : 2 bytesNumber of Cycles : 1Addressing Mode : Single Register Immediate

This instruction belongs to Data Transfer Group. Status flagsare not affected by any instruction in this group.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 17: L10 assembly-language-programming-of-atmega328 p

Example of Instruction – ADD Rd,Rr

ADD Rd,Rr Operation: Rd

Adds two registers Rd and Rr and places the sum inthe register Rd. Rd and Rr can be any GPRrepresented by 5-bit code given beside

2 bytes

Program Counter

Code

ADDopcode

Rd or Rr

: PC PC+1

Instruction Length : 2 bytes Number of Cycles : 1Addressing 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. Tounderstand how flags are affected refer to explanation given inthe instruction set.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 18: L10 assembly-language-programming-of-atmega328 p

Example of Instruction – MOV Rd,Rr

MOV Rd,Rr Operation: Rd

Copies the contents of Rr to Rd. Content of Rrremains unaltered. Rr and Rd can be any GPRrepresented by 5-bit code given beside.

2 bytes

Program Counter

Code

MOVopcode

Rd or Rr

: PC PC+1

Instruction Length : 2 bytes Number of Cycles : 1Addressing 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 flagsare not affected by any instruction in this group.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 19: L10 assembly-language-programming-of-atmega328 p

Example of Instruction – COM Rd

COM Rd Operation: Rd

Finds the 1's complement of Rd. Rd can be anyGPR represented by 5-bit code given beside.

2 bytes

Program Counter

CodeRd or Rr

: PC PC+1

Instruction Length : 2 bytes Number of Cycles : 1Addressing 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. Tounderstand how flags are affected refer to explanation given inthe instruction set.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 20: L10 assembly-language-programming-of-atmega328 p

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 alldevices. It is available in Atmega328P.

Program Counter : PC k

Instruction Length : 4 bytes (2 words) Number of Cycles : 3Addressing 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 notaffected by this instruction.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 21: L10 assembly-language-programming-of-atmega328 p

Example of Instruction – RJMP k

RJMP k Operation: PC

Program Counter : PC PC+k+1

Instruction Length : 2 bytes (1 word) Number of Cycles : 2Addressing 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 AVRMCUs with program memory not exceeding 4K words, this instruction canaddress the entire memory from every address location. Herewhere . k is the offset. When k is negative, it indicates by how many words to jump backward. When k is postive, it indicates by howmany words to jump forward.

k11k10 k9 k8 k3 k2 k1k0k7 k6 k5k4

This instruction belongs to Branch Group. Flags are notaffected by this instruction. For shorter jumps it is better to useRJMP, which is a shorter instruction than JMP.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 22: L10 assembly-language-programming-of-atmega328 p

Example of Instruction – LDS Rd,k

LDS Rd,k Operation: Rd

Loads one byte from the data memory to a register Rd. For MCUwith SRAM, the data space consists of the GPRs, I/O memoryand internal SRAM (and external SRAM if applicable). For MCUwithout SRAM, the data space consists of the register file only.The EEPROM has a separate address space. k is 16-bit addressof data memory location.

Program Counter : PC PC+2

Instruction Length : 4 bytes (2 words) Number of Cycles : 2Addressing 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 flagsare not affected by any instruction in this group.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 23: L10 assembly-language-programming-of-atmega328 p

Example of Instruction – STS k,Rd

STS k,Rd Operation: (k)

Copies a byte from a GPR to a data memory location. For MCUwith SRAM, the data space consists of the GPRs, I/O memoryand internal SRAM (and external SRAM if applicable). For MCUwithout SRAM, the data space consists of the register file only.The EEPROM has a separate address space. k is 16-bit addressof data memory location.

Program Counter : PC PC+2

Instruction Length : 4 bytes (2 words) Number of Cycles : 2Addressing 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 flagsare not affected by any instruction in this group.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 24: L10 assembly-language-programming-of-atmega328 p

Example of Instruction – IN Rd,A

IN Rd,A Operation: Rd

Loads 1-byte of data from the I/O Space (Ports, Timers, ConfigurationRegisters, etc.) into a GPR Rd represented by 5-bit codeand A is address of I/O Space specified by

Program Counter : PC PC+1

Instruction Length : 2 bytes (1 word) Number of Cycles : 1Addressing Mode

I/O(A)

Flags Affected: I/O Direct : None

10110 d3 d2 d1d0d4A5A4

A3A2 A1A0

d3 d2 d1d0d4

INOpcode

Operands

A5A4

A3A2 A1A0

This instruction belongs to Data Transfer Group. Status flagsare not affected by any instruction in this group.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 25: L10 assembly-language-programming-of-atmega328 p

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, ConfigurationRegisters, etc.). Rr is represented by the 5-bit codeand A is address of I/O Space specified by

Program Counter : PC PC+1

Instruction Length : 2 bytes (1 word) Number of Cycles : 1Addressing Mode

Rr

Flags Affected: I/O Direct : None

10111 d3 d2 d1d0d4A5A4

A3A2 A1A0

d3 d2 d1d0d4

OUTOpcode

Operands

A5A4

A3A2 A1A0

This instruction belongs to Data Transfer Group. Status flagsare not affected by any instruction in this group.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 26: L10 assembly-language-programming-of-atmega328 p

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), anEEPROM file (.eep), a list file (.lst), and a map file (.map).

4 Test the program by running it on a simulator or by using adebugger. Repeat Steps 1-4 until the program is found tobe 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 statementsalong with the machine code assembled by the program.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 27: L10 assembly-language-programming-of-atmega328 p

Points to be Remembered in ALP of Atmega328P

Write a JMP to branch to the user program at the resetaddres $0000.Do not write user program in the area $0000-$003Freserved for interrupt vectors.User program can be loaded immediately after theinterrupt vector locations, say from $0040.If bootloader has been used, ensure that the user programdoes not over write on the bootloader.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 28: L10 assembly-language-programming-of-atmega328 p

Program to Add Two Numbers

Refer to Instruction Set Manual to understand the workingof LPM instruction.Trace through this program and find the contents ofregisters and memory at each step.

R S Ananda Murthy Assembler Programming of Atmega328P

Page 29: L10 assembly-language-programming-of-atmega328 p

License

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

R S Ananda Murthy Assembler Programming of Atmega328P