0

I am using inline assembly to do ARM operations in C code. In the C code I'm allocating memory with calloc. This memory block is divided into different buffers, such that:

int * SCRATCH = (int *)calloc(LEN, sizeof(int));
buffer1       = (int *)SCRATCH;
buffer2       = (short *)((int *) buffer1 + sizeof(* buffer1) * LEN_BUF_1);

where the lengths are all known. Now I'm accessing values from those buffers with

LDRD r4, r4, [r0], #8;

which gives me an error since the memory is not aligned properly. How would I manage to align all buffers that I'm using to be able to use double loads and stores?

Thank you!

  • Possible duplicate of [How to allocate aligned memory only using the standard library?](https://stackoverflow.com/questions/227897/how-to-allocate-aligned-memory-only-using-the-standard-library) – Colin Aug 10 '17 at 08:06
  • 1
    Allocate more memory than you need, then increment the start pointer to be on an 8 byte aligned address. – Colin Aug 10 '17 at 08:07
  • I tried the above link already and it didn't help, i.e. the ARM code is still stuck on the double loads. – dfasdsads sdasdasd Aug 10 '17 at 09:19
  • There's a lot (too much) of casting going on. What are types of buffer1/2? What's `r0` when the error happens? I suspect you have non-32-bit aligned buffer somewhere, since `ldrd` should work fine otherwise. – domen Aug 10 '17 at 13:44
  • It is giving an 'error' because you need `ldrd r4, r5, [r0], #8`. First register must be even second register must be next register (rN, rN+1 where 'N' is an even number). The assembler is not going to know about buffer alignment; only run time. If runtime is the issue, just use `TST r0, #1` and `ldrne [r4], [r0], #4` to prime the buffer before the `LDRD`. What is **an error**? – artless noise Aug 10 '17 at 15:56

0 Answers0