First, a little background. In Avr, tiny data can be stored in registers, in sram, in eeprom, or in program space. Register and sram are volatile storage, but eeprom and program space are not. (i.e. data remains in the absence of power.)
When programming in c (using the avr-gcc libraries) a typical code might look like this:
#define F_CPU 8000000UL
#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
const uint8_t dat_eeprom EEMEM= 0xab;
const uint8_t dat_pgm_space PROGMEM= 0xcd;
uint8_t dat_sram = 0xef;
int main(){
while(1){
;;
}
}
Compiling with:
avr-gcc -g -mmcu=attiny44 -o test.elf test.c
And extract intel.hex from .elf:
avr-objcopy -j .text -j .data -O ihex test.elf test.hex
The following test.hex is obtained:
:1000000011C023C022C021C020C01FC01EC01DC0FF
:100010001CC01BC01AC019C018C017C016C015C01C
:1000200014C0CD0011241FBECFE5D1E0DEBFCDBF8F
:1000300010E0A0E6B0E0EAE5F0E002C005900D9225
:10004000A236B107D9F702D006C0DACFCF93DF933B
:0A005000CDB7DEB7FFCFF894FFCF65
:02005A00EF00B5
:00000001FF
And the following disassembly:
00000000 <.sec1>:
0: 11 c0 rjmp .+34 ; 0x24
<...skipped interrupt vector table...>
20: 14 c0 rjmp .+40 ; 0x4a
22: cd 00 .word 0x00cd ; this is our data stored in the program mem.
24: 11 24 eor r1, r1
26: 1f be out 0x3f, r1 ; 63
28: cf e5 ldi r28, 0x5F ; 95
2a: d1 e0 ldi r29, 0x01 ; 1
2c: de bf out 0x3e, r29 ; 62
2e: cd bf out 0x3d, r28 ; 61
30: 10 e0 ldi r17, 0x00 ; 0
32: a0 e6 ldi r26, 0x60 ; X register low byte ; address of dest. in
34: b0 e0 ldi r27, 0x00 ; X register high byte; sram
36: ea e5 ldi r30, 0x5A ; z register low byte ; address of data in
38: f0 e0 ldi r31, 0x00 ; z register high byte; program memory
3a: 02 c0 rjmp .+4 ; 0x40
3c: 05 90 lpm r0, Z+
3e: 0d 92 st X+, r0
40: a2 36 cpi r26, 0x62 ; 98
42: b1 07 cpc r27, r17
44: d9 f7 brne .-10 ; 0x3c
<...skipped rcall to main...>
5a: ef 00 .word 0x00ef ; this is our data that
should be stored in the sram.
So, how is the data (0xef) that we wanted to include in the sram initialization?
The answer is carried out through a routine before the main one.
, sram, 0x5a . sram :
- x , sram. (0x60) , , .
- z, , (0x5a)
- , z, r0 lpm. , z ( , ) sram.
- r0 sram , x.
- , , sram.
rcall.
/ ?