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 25, 2018 Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 1 / 14
Introduction to Assembly This lecture will introduce you to the assembly language, the compilation and downloading process. Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 2 / 14
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 3 / 14
LDI instruction LDI Rd, K ;load Rd with immediate value k 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,0 x3f ;load R20 with immediate hex value 0x3F E 3 4 F Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 4 / 14
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 5 / 14
LDS instruction LDS Rd, K ;load Rd with the content of memory location K Loads the register directly from data space (one byte value) Examples: LDS R5,0 x400 ;load R5 with content of memory location 0x400 LDS R6,0 x1 ;load R6 with content of memory location 0x1 What is in memory locate 0x1? Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 6 / 14
STS instruction STS K, Rr ;store register into location K Store direct to data space Location could be any value in the data space Examples: STS 0 x400, R25 ;store R25 to data space location 0x230 STS 0 x27, R16 ;store R16 to memory locate 0x27 What is in memory locate 0x27? Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 7 / 14
IN instruction IN Rd, A ;load an I/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,0 x10 ;load R19 with data from location 0x10 What is in memory locate 0x10? Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 8 / 14
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 9 / 14
OUT instruction OUT A, Rr ;store register Rr to I/O location A Cannot copy and immediate value to an I/O register nor to an SRAM location Examples: OUT PORTA, R0 ;load R19 with data from location 0x10 What is PORTA? Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 10 / 14
Assembly language Assembly language consists of, instructions referred to as mnemonics, directives and labels.. INCLUDE " M1284DEF. INC " LDI R16, hi8 ( RAMEND ) OUT SPH, R16 LDI R16, lo8 ( RAMEND ) 5 OUT SPL, R16 ;initialize stack pointer SBI DDRC, 0 ;set bit 0 of DDRC HERE : SBI PORTC, 0 10 CALL DELAY ;call DELAY subroutine CBI PORTC, 0 CALL DELAY RJMP HERE 15 DELAY : LDI R20, 255 DL1 : DEC R20 BRNE DL1 RET Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 11 / 14
Assembly language Assembly language consists of, instructions referred to as mnemonics, directives and labels.. INCLUDE " M1284DEF. INC " LDI R16, hi8 ( RAMEND ) OUT SPH, R16 LDI R16, lo8 ( RAMEND ) 5 OUT SPL, R16 ;initialize stack pointer SBI DDRC, 0 ;set bit 0 of DDRC HERE : SBI PORTC, 0 10 CALL DELAY ;call DELAY subroutine CBI PORTC, 0 CALL DELAY RJMP HERE 15 DELAY : LDI R20, 255 DL1 : DEC R20 BRNE DL1 RET Directive Label Comment Instruction Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 11 / 14
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) Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 12 / 14
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) What is this code represent? Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 12 / 14
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 Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 13 / 14
Downloading the hex file avrdude -v -c dragon_jtag -p m1284 -Pusb -U flash:w:ex.hex Aly El-Osery (NMT) EE 308: Microcontrollers January 25, 2018 14 / 14