On Mar 19, 8:25=A0am, Mike Silva <snarflem...@yahoo.com> wrote:
> On Mar 19, 7:25=A0am, "Mike Warren" <miwa-not-this-...@or-this-
>
>
>
>
>
> csas.net.au> wrote:
> > I have a table in program memory and want to store a byte at a given
> > location into register R16.
>
> > (This is just example data for testing)
>
> > MyTable: ; 256 bytes
> > .db $00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F
> > .db $10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$1A,$1B,$1C,$1D,$1E,$1F
> > =A0 =A0 <snip>
> > .db $F0,$F1,$F2,$F3,$F4,$F5,$F6,$F7,$F8,$F9,$FA,$FB,$FC,$FD,$FE,$FF
>
> > R17 contains a number between $00 and $FF which represents the offset
> > from MyTable.
>
> > Can someone please show me a nice way of doing this?
>
> You need to use the LPM instruction, which uses the Z register pair
> (R31:R30). =A0Something like (no guarantees on the syntax)
>
> ldi =A0r31, (high) MyTable
> ldi =A0r30, (low) MyTable =A0;load base of table into Z pair
> add =A0r30,r17
> adc =A0r31,#0 =A0;address of desired value now in Z pair
> lpm =A0r16,Z =A0 ;fetch desired value- Hide quoted text -
>
> - Show quoted text -
OK, looks like "adc r31,#0" isn't allowed. I suppose the alternative
is
ldi r16,0
adc r31,r16
AVR assembly programmers, is there a better way here?
BTW, I know avr-gcc sets up a zero register (r1 IIRC). If you set one
up in your program it would be useful to use here for the adc
instruction (and many other places, presumably).