EE 308: Microcontrollers Introduction to the Assmbly Language
Aly El-Osery
Electrical Engineering Department New Mexico Institute of Mining and Technology Socorro, New Mexico, USA
January 29, 2019
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 1 / 15 Introduction to Assembly
This lecture will introduce you to the assembly language, the compilation and downloading process.
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 2 / 15 Fields of an assembly language instruction
[label:] mnemonic [operands] [;comment]
label . . . marks lines that can be referred to by the program mnemonic . . . instruction and results in an opcode operands . . . could be registers, address to a desired memory, or constant depending on the instruction comments . . . comments start with a semicolon
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 3 / 15 LDI instruction
LDI Rd,K ;load Rd with immediate valuek
K is an 8-bit value Rd can only be R16 to R31 Hex values for K can be represented using 0× Machine code for LDI 1110 kkkk dddd kkkk Examples: LDI R20,0x3F ;load R20 with immediate hex value0x3F
E 3 4 F
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 4 / 15 ADD instruction
ADD Rd,Rr ;Add Rr to Rd and store result to Rd
Uses the ALU Result affects the status register Examples: ADD R16,R17 ;Add R17 to R16 and store in R16
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 5 / 15 LDS instruction
LDS Rd,K ;load Rd with the content of memory locationK
Loads the register directly from data space (one byte value) Examples: LDS R5,0x400 ;load R5 with content of memory location0x400 LDS R6,0x1 ;load R6 with content of memory location0x1 What is in memory location 0x1?
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 6 / 15 STS instruction
STS K,Rr ;store register into locationK
Store direct to data space Location could be any value in the data space Examples: STS 0x400,R25 ;store R25 to data space location0x230 STS 0x27,R16 ;store R16 to memory location0x27 What is in memory location 0x27?
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 7 / 15 IN instruction
IN Rd,A ;load anI/O location Rd
A is an I/O location 0 to 0x3F I/O locations are offset from memory addresses by 0x20 therefore they start at 0x00 Examples: IN R19,0x10 ;load R19 with data from location0x10 What is in memory locate 0x10?
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 8 / 15 Difference between IN and LDS
IN is faster IN is a 2-byte instruction vs the 4-byte instruction LDS Can use I/O register names with IN IN as opposed to LDS it is available in all AVRs
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 9 / 15 OUT instruction
OUT A,Rr ;store register Rr toI/O locationA
Cannot copy and immediate value to an I/O register nor to an SRAM location Examples: OUTPORTA,R0 ;Save the value of R0 in location0x10 What is PORTA?
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 10 / 15 Assembly language
Assembly language consists of, instructions referred to as mnemonics, directives and labels. .INCLUDE "m1284def.inc" LDI R16, hi8(RAMEND) OUTSPH, R16 LDI R16, lo8(RAMEND) 5 OUTSPL, R16 ;initialize stack pointer
SBIDDRC, 0 ;set bit0 ofDDRC HERE: SBIPORTC, 0 10 CALLDELAY ;callDELAY subroutine CBIPORTC, 0 CALLDELAY RJMPHERE
15 DELAY: LDI R20, 255 DL1 :DEC R20 BRNE DL1 RET
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 11 / 15 Assembly language
Assembly language consists of, instructions referred to as mnemonics, directives and labels. .INCLUDE "m1284def.inc" Directive LDI R16, hi8(RAMEND) OUTSPH, R16 LDI R16, lo8(RAMEND) 5 OUTSPL, R16 ;initialize stack pointer
SBIDDRC, 0 ;set bit0 ofDDRC HERE: Label SBIPORTC, 0 10 CALLDELAY ;callDELAY subroutine CBIPORTC, 0 CALLDELAY RJMPHERE Comment
15 DELAY: LDI R20, 255 DL1 :DEC R20 Instruction BRNE DL1 RET
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 11 / 15 Values follows little endian (low byte are written first followed by high byte)
Intel Hex
Intel Hex
:1000000000E40EBF0FEF0DBF389A409A0E940C001B :1000100040980E940C00F9CF4FEF4A95F1F70895F0 :00000001FF
Start code (:) Byte count data only (2 characters) Address (4 characters) Record type (2 characters) 00 data record 01 end of file Data (2k characters) Checksum (2 characters)
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 12 / 15 Values follows little endian (low byte are written first followed by high byte)
Intel Hex
Intel Hex
:1000000000E40EBF0FEF0DBF389A409A0E940C001B :1000100040980E940C00F9CF4FEF4A95F1F70895F0 :00000001FF What is this code represent?
Start code (:) Byte count data only (2 characters) Address (4 characters) Record type (2 characters) 00 data record 01 end of file Data (2k characters) Checksum (2 characters)
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 12 / 15 Intel Hex
Intel Hex
:1000000000E40EBF0FEF0DBF389A409A0E940C001B :1000100040980E940C00F9CF4FEF4A95F1F70895F0 :00000001FF What is this code represent?
Start code (:) Byte count data only (2 characters) Address (4 characters) Record type (2 characters) 00 data record 01 end of file Data (2k characters) Checksum (2 characters) Values follows little endian (low byte are written first followed by high byte)
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 12 / 15 What to download on the microcontroller
Assembler — replaces mnemonics by their opcodes, resolves include directives, translates register names to addresses, removes comments, etc. avr-as -mmcu=atmega1284 -o ex.o ex.s
Linker — links multiple files avr-ld -mavr51 -o ex.elf ex.o
Translate object files avr-objcopy -j .text -j .data -O ihex -o ex.hex ex.elf
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 13 / 15 Downloading the hex file
avrdude -v -c arduino -p m1284 -Pusb -b 19200 -U flash:w:ex.hex
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 14 / 15 Disassembly
avr-objdump -h -S ex.elf ex.lst
ex.elf: file format elf32-avr 0000000a
Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 15 / 15