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.

3
  • 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? Commented Oct 29, 2010 at 17:02
  • I don't need to keep it aligned. See the example i added. Commented Oct 29, 2010 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. Commented Nov 1, 2010 at 8:32

2 Answers 2

1

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

uint8_t a[54] __attribute__ ((aligned(64)));
Sign up to request clarification or add additional context in comments.

1 Comment

This might increase memory footprint because I don't think the linker is smart enough to reorder .bss entries to conserver memory
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...

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.