Advanced Assembly Chapter 6 Sepehr Naimi www Nicer
Advanced Assembly Chapter 6 Sepehr Naimi www. Nicer. Land. com www. Micro. Digital. Ed. com
Topics n n n Assembler directives Addressing modes Macro EEPROM memory Checksum 2
Some Assembler directives Example + LDI R 20, 5+3 ; LDI R 20, 8 - LDI R 30, 9 -3 ; LDI R 30, 6 * LDI R 25, 5*7 ; LDI R 25, 35 / LDI R 19, 8/2 ; LDI R 19, 4 Example & LDI R 20, 0 x 50&0 x 10 ; LDI R 20, 0 x 10 | LDI R 25, 0 x 50|0 x 1 ; LDI R 25, 0 x 51 ^ LDI R 23, 0 x 50^0 x 10 ; LDI R 23, 0 x 40 Example << LDI R 16, 0 x 10<<1 ; LDI R 16, 0 x 20 >> LDI R 16, 0 x 8 >>2 ; LDI R 16, 0 x 2 3
HIGH and LOW LDI R 20, LOW(0 x 1234) LDI R 20, $34 LDI R 21, HIGH(0 x 1234) LDI R 21, $1234 HIGH LDI R 20, LOW(-200) LDI R 20, $FF LDI R 21, HIGH(-200) LDI R 21, $38 LOW -200 = $FF 38 HIGH LOW 4
Single Register Addressing Mode n INC Rd n n INC R 19 DEC Rd n DEC R 23 ; R 23 = R 23 – 1 5
Immediate Addressing Mode (Single register with immediate) n LDI Rd, K n n SUBI Rd, K n n LDI R 19, 25 SUBI R 23, 5 ; R 23 = R 23 – 5 ANDI Rd, K n ANDI R 21, 0 x 15 6
Two-register addressing mode n ADD Rd, Rr n n ADD R 26, R 23 SUB Rd, Rr n LDI R 20, R 10 7
Direct addressing mode n LDS Rd, address n LDS R 19, 0 x 313 n STS address, Rs n STS 0 x 95, R 19 8
I/O direct addressing mode n OUT address, Rs n OUT 0 x 70, R 16 n IN Rs, address n IN R 19, 0 x 90 9
Register indirect addressing mode n LD Rd, X n n LD R 24, X LD R 19, Y LD R 20, Z ST X, Rd n n ST X, R 18 ST Y, R 20 10
Example n Write a program to copy the value $55 into memory locations $140 to $144 LDI R 19, 0 x 5 LDI R 16, 0 x 55 LDI YL, 0 x 40 LDI YH, 0 x 1 L 1: ST Y, R 16 INC YL DEC R 19 BRNE L 1 ; R 19 = 5 (R 19 for counter) ; load R 16 with value 0 x 55 (value to be copied) ; load the LDI low byte of Y with value 0 x 40 YL, LOW(0 x 140) ; load the LDI high. YH, HIGH(0 x 140) byte of Y with value 0 x 1 ; copy R 16 to memory location 0 x 140 ; increment the low byte of Y ; decrement the counter ; loop until counter = zero 11
Auto-increment and Auto decrement n Register indirect addressing with Post-increment n LD Rd, X+ n n ST X+, Rs n n LD R 20, X+ ST X+, R 8 Register indirect addressing with Pre-decrement n LD Rd, -X n n LD R 19, -X ST –X, R 31 12
Example n Write a program to copy the value $55 into memory locations $140 to $444 LDI R 19, 0 x 5 LDI R 16, 0 x 55 LDI YL, LOW($140) LDI YH, HIGH($140) L 1: ST Y+, R 16 DEC R 19 BRNE L 1 ; R 19 = 5 (R 19 for counter) ; load R 16 with value 0 x 55 (value to be copied) ; load the low byte of Y with value 0 x 40 ; load the high byte of Y with value 0 x 1 ; copy R 16 to memory location Y ; decrement the counter ; loop until counter = zero 13
Register indirect with displacement n STD n n STD LDD n LDD Z+q, Rr ; store Rr into location Z+q Z+5, R 20 ; store R 20 in location Z+5 Rd, Z+q ; load from Z+q into Rd R 20, Z+8 ; load from Z+8 into R 20 14
Storing fixed data in flash memory DATA 1: DATA 2: DATA 3: DATA 4: DATA 6: . DB. DB. DB 28 0 b 00110101 0 x 39 'Y' "Hello ALI" ; DECIMAL(1 C in hex) ; BINARY (35 in hex) ; HEX ; single ASCII char ; ASCII string 15
Storing fixed data in flash memory n LPM Rd, Z n n LPM R 15, Z Example: n n LDI R 30, 0 x 80 LDI R 31, 0 LPM R 18, Z ; read from the low byte of loc 0 x 40 LPM Rd, Z+ n LPM R 20, Z 16
Example n Analyze the following program; then rewrite it using LPM R 20, Z+ . ORG $0000 ; burn into ROM starting at 0 LDI R 20, 0 x. FF OUT DDRB, R 20 ; make PB an output LDI ZL, LOW(MYDATA<<1) ; ZL = 0 look-up table low-byte addr LDI ZH, HIGH(MYDATA<<1) ; ZH = 0 A look-up table high-byte addr LPM R 20, Z+ OUT PORTB, R 20 ; send it. OUT to Port B PORTB, R 20 INC ZL ; R 30 = 01 pointing to next byte (A 01) LPM R 20, Z ; load R 20 char pointed to by Z LPMwith 'S' R 20, Z+ OUT PORTB, R 20 ; send it. OUT to Port B PORTB, R 20 INC ZL ; R 30 = 02 pointing to next (A 02) LPM R 20, Z ; load R 20 with 'A' char pointed to by Z OUT PORTB, R 20 ; send it to Port B HERE: RJMP HERE ; stay here forever ; data is burned into code(program) space starting at $500. ORG $500 MYDATA: . DB "USA" 17
Example n Assume that ROM space starting at $500 contains the message “The Promise of World Peace”. Write a program to bring it into CPU one byte at a time and place the bytes in RAM locations starting at $140. . ORG 0 ; burn into ROM starting at 0 LDI ZL, LOW(MYDATA<<1) ; R 30 = 00 low-byte addr LDI ZH, HIGH(MYDATA<<1) ; R 31 = 0 A, high-byte addr LDI XL, LOW(0 x 140) ; R 26 = 40, low-byte RAM address LDI XH, HIGH(0 x 140) ; R 27 = 1, high-byte RAM address AGAIN: LPM R 16, Z+ ; read the table, then increment Z CPI R 16, 0 ; compare R 16 with 0 BREQ END ; exit if end of string ST X+, R 16 ; store R 16 in RAM and inc X RJMP AGAIN END: RJMP END. ORG 0 x 500 ; data burned starting at 0 x 500 MYDATA: . DB "The Promise of World Peace", 0 18
Macro. MACRO INITSTACK LDI R 16, HIGH(RAMEND) OUT SPH, R 16 LDI R 16, LOW(RAMEND) OUT SPL, R 16. ENDMACRO INITSTACK 19
Macro. MACRO LOADIO LDI R 20, @1 OUT @0, R 20. ENDMACRO LOADIO DDRB, 0 x. FF LOADIO PORTB, 0 x 55 20
EEPROM n n n EEPROM Address Register EEPROM Data Register EEPROM is a place to store data. It is not deleted when power is. EEPROM off Control Register ATmega 32 has 1024 bytes of EEPROM In AVR 3 registers are dedicated to EEPROM n n n EEARH: EEARL EEDR EECR 21
Reading from EEPROM 1. 2. 3. 4. Wait until EEWE becomes zero. Write new EEPROM address to EEAR Set EERE to one. Read EEPROM data from EEDR. The following program reads the content of location 0 x 005 F of EEPROM: WAIT: SBIC EECR, EEWE RJMP WAIT LDI OUT SBI IN R 18, 0 R 17, 0 x 5 F EEARH, R 18 EEARL, R 17 EECR, EERE R 16, EEDR ; check EEWE to see if last write is finished ; wait more ; load high byte of address to R 18 ; load low byte of address to R 17 ; load high byte of address to EEARH ; load low byte of address to EEARL ; set Read Enable to one ; load EEPROM Data Register to R 16 22
Writing into EEPROM 1. 2. 3. 4. 5. Wait until EEWE becomes zero. Write new EEPROM address to EEAR (optional). Write new EEPROM data to EEDR (optional). Set EEMWE bit to one. Within four clock cycles after setting EEMWE, set EEWE to one. The program writes ‘G’ into location 0 x 005 F of EEPROM: WAIT: SBIC RJMP LDI OUT SBI EECR, EEWE ; check EEWE to see if last write is finished WAIT ; wait more R 18, 0 ; load high byte of address to R 18 R 17, 0 x 5 F ; load low byte of address to R 17 EEARH, R 18 ; load high byte of address to EEARH EEARL, R 17 ; load low byte of address to EEARL R 16, 'G' ; load 'G' to R 16 EEDR, R 16 ; load R 16 to EEPROM Data Register EECR, EEMWE ; set Master Write Enable to one EECR, EEWE ; set Write Enable to one 23
Checksum n n To detect data corruption Calculating checksum byte: n n n Add the bytes together and drop the carries Take the 2’s complement of the total sum Testing checksum n n n Add the bytes together and drop the carries Add the checksum byte to the sum If the result is not zero, data is corrupted 24
Example Find the checksum byte for the followings: $25, $62, $3 F, $52 Solution: $25 + $62 + $3 F + $52 $1 18 Checksum byte = 2’s complement of $18 = $E 8 n 25
Example The checksum byte is $E 8. Test checksum for the following data: $25, $62, $3 F, $52 Solution: $25 + $62 + $3 F + $52 + $E 8 $00 not corrupted n 26
- Slides: 26