[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] array of pointers to PROGMEM strings
From: |
Thedore A. Roth |
Subject: |
Re: [avr-gcc-list] array of pointers to PROGMEM strings |
Date: |
Sat, 16 Nov 2002 11:02:27 -0800 (PST) |
On Sat, 16 Nov 2002, raphael deimel wrote:
:)Hi, I stumbled over a problem. I want to define an array of pointers to
:)ascii strings, everything in progmem to save RAM.
:)
:)I thought about defining it like this:
:)
:)PGM_P PROGMEM array[2] = {
:) "ascii string",
:) "ascii string"
:)}
:)
:)But this would put the ascii strings into ram, right?
:)Does someone know how to define it?
This should be added to the FAQ. ;-) (I'll do it today.)
You need to do something like this:
cat <<EOF > foo.c
#include <avr/io.h>
#include <avr/pgmspace.h>
const char foo[] PROGMEM = "Foo";
const char bar[] PROGMEM = "Bar";
PGM_P array[2] PROGMEM = {
foo,
bar
};
int main (void)
{
char buf[32];
strcpy_P (buf, array[1]); /* copy bar into buf */
return 0;
}
EOF
Looking at the disassembly of the resulting object file we see that array in
in flash as such:
0000008c <__ctors_end>:
8c: 46 6f ori r20, 0xF6 ; 246
8e: 6f 00 .word 0x006f ; ????
00000090 <bar>:
90: 42 61 ori r20, 0x12 ; 18
92: 72 00 .word 0x0072 ; ????
00000094 <array>:
94: 8c 00 .word 0x008c ; ????
96: 90 00 .word 0x0090 ; ????
foo is at addr 0x008c
bar is at addr 0x0090
array is at addr 0x0094
Then in main() we see this:
strcpy_P (buf, array[1]); /* copy bar into buf */
de: 60 e9 ldi r22, 0x90 ; 144
e0: 70 e0 ldi r23, 0x00 ; 0
e2: ce 01 movw r24, r28
e4: 01 96 adiw r24, 0x01 ; 1
e6: 0e 94 79 00 call 0xf2
The addr of bar (0x0090) is loaded into the r23:r22 pair which is the second
parameter passed to strcpy_P. The r25:r24 pair is the addr of buf.
Is that what you intended to do?
Ted Roth
PS: If anyone sees a flaw in this discussion, please point it out so the FAQ
entry will be correct.
PSS: If someone has a better way to do this, I'm all ears.
avr-gcc-list at http://avr1.org