5

Is it possible to create an array that doesn't cross 256 byte boundary? That is addresses of the individual array items only differ in the lower byte. This is weaker requirement than keeping the array aligned to 256 bytes. The only solution I could think of was aligning to next_power_of_two(sizeof(array)), but I'm not sure about the gaps that would appear this way.

It is for a library for AVR microcontrollers, and this would save me a few precious instructions in an interrupt handler.The array that should have this property is 54 byte long out of about 80 bytes of total static memory used by the library. I'm looking for a way that doesn't increase the memory requirements.

I'm using avr-as gnu assembler and avr-ld linker.

Example:If the array starts at address 0x00f0, then the higher word will change from 0x00 to 0x01 while traversing the array.

I don't really care whether it starts at address 0x0100 or 0x0101 as long as it doesn't cross the boundary.

cube
  • 3,867
  • 7
  • 32
  • 52
  • You need to keep the address of the array aligned? How does that reduce your instruction count since you already know the array is <= 256 elements? – MSN Oct 29 '10 at 17:02
  • I don't need to keep it aligned. See the example i added. – cube Oct 29 '10 at 19:35
  • 1
    note however that keeping the array aligned to a **64** byte boundary will satisfy your requirements - your linker *should* be able to sort these allocations so that you don't get wasted memory. – Paul R Nov 01 '10 at 08:32

2 Answers2

1

You only need 64 byte alignment to meet this requirement, so e.g. this should work:

uint8_t a[54] __attribute__ ((aligned(64)));
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    This might increase memory footprint because I don't think the linker is smart enough to reorder .bss entries to conserver memory – Peer Stritzinger Nov 04 '10 at 14:41
0

I don't know anything about AVR microcontrollers, but, generally speaking, static variables are usually placed in the data section of the executable, and, since your static memory requirements are low, all you need to ensure is that the data section is 256 byte aligned. (Which it may be by default. On x86, it usually is.) Check the linker options...

Eugene Smith
  • 9,126
  • 6
  • 36
  • 40
  • 1
    And what happens when some other code that is linked against my library allocates more static memory? It all goes to .bss and my little array gets moved anywhere in the whole section, doesn't it? – cube Oct 29 '10 at 20:06
  • What you could do is put it in its own section and ensure the alignment in the special section in the linker command file. But probably only worth it if you need to tweak it for saving memory. – Peer Stritzinger Nov 04 '10 at 14:40