Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Re: Help needed:- what's the quickest way to store 256 bytes of data?

2006-02-14 by Peter Harrison

And there is Duff's device (Google is your friend here) for the more 
general case where the count is neither 8 bits nor a multiple of 8:

    count = 256;
    uint8_t *to = mydata;
    register int n = (count + 7) / 8;      /* count > 0 assumed */

    switch (count % 8)
    {
    case 0:        do {  *to++ = PIND;
    case 7:              *to++ = PIND;
    case 6:              *to++ = PIND;
    case 5:              *to++ = PIND;
    case 4:              *to++ = PIND;
    case 3:              *to++ = PIND;
    case 2:              *to++ = PIND;
    case 1:              *to++ = PIND;
                       } while (--n > 0);
    }

Which compiles, with GCC3.4.5 and -Os, to give:

    count = 256;
    uint8_t *to = mydata;
  136:	e2 e6       	ldi	r30, 0x62	; 98
  138:	f0 e0       	ldi	r31, 0x00	; 0
    register int n = (count + 7) / 8;      /* count > 0 assumed */
  13a:	20 e2       	ldi	r18, 0x20	; 32
  13c:	30 e0       	ldi	r19, 0x00	; 0

    switch (count % 8)
    {
    case 0:        do {  *to++ = PIND;
  13e:	80 b3       	in	r24, 0x10	; 16
  140:	81 93       	st	Z+, r24
    case 7:              *to++ = PIND;
  142:	80 b3       	in	r24, 0x10	; 16
  144:	81 93       	st	Z+, r24
    case 6:              *to++ = PIND;
  146:	80 b3       	in	r24, 0x10	; 16
  148:	81 93       	st	Z+, r24
    case 5:              *to++ = PIND;
  14a:	80 b3       	in	r24, 0x10	; 16
  14c:	81 93       	st	Z+, r24
    case 4:              *to++ = PIND;
  14e:	80 b3       	in	r24, 0x10	; 16
  150:	81 93       	st	Z+, r24
    case 3:              *to++ = PIND;
  152:	80 b3       	in	r24, 0x10	; 16
  154:	81 93       	st	Z+, r24
    case 2:              *to++ = PIND;
  156:	80 b3       	in	r24, 0x10	; 16
  158:	81 93       	st	Z+, r24
    case 1:              *to++ = PIND;
  15a:	80 b3       	in	r24, 0x10	; 16
  15c:	81 93       	st	Z+, r24
                       } while (--n > 0);
  15e:	21 50       	subi	r18, 0x01	; 1
  160:	30 40       	sbci	r19, 0x00	; 0
  162:	12 16       	cp	r1, r18
  164:	13 06       	cpc	r1, r19
  166:	5c f3       	brlt	.-42     	; 0x13e <myloop+0x6c>
  168:	08 95       	ret

I have to dash to work so I have not checked the cycle count and such 
but it looks pretty good to me.

Pete Harrison

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.