EE 308: 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 , 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× 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

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 - 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: elf32-avr 0000000a : Sections : HERE: IdxName Size VMA LMA Fileoff Algn SBIPORTC, 0 5 0 .text 00000020 00000000 00000000 00000074 2**1 a: 40 9a sbi 0x08, 0 ;8 CONTENTS,ALLOC,LOAD,READONLY,CODE 35 CALLDELAY ;callDELAY subroutine 1 .data 00000000 00800100 00000020 00000094 2**0 c: 0e 94 0c 00 call 0x18 ;0x18 CONTENTS,ALLOC,LOAD,DATA CBIPORTC, 0 2 .stab 000000c0 00000000 00000000 00000094 2**2 10: 40 98 cbi 0x08, 0 ;8 10 CONTENTS,READONLY,DEBUGGING CALLDELAY 3 .stabstr 00000006 00000000 00000000 00000154 2**0 40 12: 0e940c00 call 0x18 ;0x18 CONTENTS,READONLY,DEBUGGING RJMPHERE 16: f9 cf rjmp .-14 ;0xa Disassembly of section .text: 15 00000018 : 00000000 <__ctors_end>: 45 .INCLUDE "m1284def.inc" DELAY: LDI R16, hi8(RAMEND) LDI R20, 255 0: 00 e4 ldi r16, 0x40 ; 64 18: 4f ef ldi r20, 0xFF ; 255 20 OUTSPH, R16 2: 0e bf out 0x3e, r16 ; 62 50 0000001a : LDI R16, lo8(RAMEND) DL1 :DEC R20 4: 0f ef ldi r16, 0xFF ; 255 1a: 4a 95 dec r20 OUTSPL, R16 ;initialize stack pointer BRNE DL1 25 6: 0d bf out 0x3d, r16 ; 61 1c: f1 f7 brne .-4 ;0x1a 55 RET SBIDDRC, 0 ;set bit0 ofDDRC 1e: 08 95 ret 8: 38 9a sbi 0x07, 0 ;7

Introduction Assembly Assembly Program Aly El-Osery (NMT) EE 308: Microcontrollers January 29, 2019 15 / 15