This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Unaligned access problems

Note: This was originally posted on 1st June 2009 at http://forums.arm.com

We had written some code for an ARM Cortex M3 platform. But now, we have to port it to an ARM7TDMI based controller in very little time. The problem is the former supports unaligned access, but the latter doesnt. Now, we have lots of code of the form:

unsigned int a;
char b[100];
a =  *( ( unsigned int *) &b[5] );


As you can see, we were using typecasts to copy 4-byte entities and also 2 byte entities in many places. We are using IAR compiler. The code works fine when run on Cortex M3 but when ARM7TDMI encounters this kind of instructions, it goes into abort. Is there any way we can cause the compiler to generate appropriate instructions so that aborts do not occur, other than replacing all of this kind of instructions with byte-wise memcopys ?
Parents
  • Note: This was originally posted on 25th June 2009 at http://forums.arm.com

    we were not using any pointers.. we were using '&'s and typecasts.


    Well, &myVar generates a pointer - your code just immediately consumes it. So make it explicit via a temporary variable and you have pointers you can do fun things with. It is worth noting that the hardware needs this temporary register, so number of instructions won't typically go up.

    Using your first example,

    __packed unsigned int* pA;
    unsigned int a
    char b[100];

    /* generate explicit unaligned pointer */
    pA = (__packed unsigned int *) &b[5];

    /* load from it. */
    a = *pA

    /* This may also work - but binanry will be same size anyway =) */
    a = *( ( __packed unsigned int *) &b[5] );
Reply
  • Note: This was originally posted on 25th June 2009 at http://forums.arm.com

    we were not using any pointers.. we were using '&'s and typecasts.


    Well, &myVar generates a pointer - your code just immediately consumes it. So make it explicit via a temporary variable and you have pointers you can do fun things with. It is worth noting that the hardware needs this temporary register, so number of instructions won't typically go up.

    Using your first example,

    __packed unsigned int* pA;
    unsigned int a
    char b[100];

    /* generate explicit unaligned pointer */
    pA = (__packed unsigned int *) &b[5];

    /* load from it. */
    a = *pA

    /* This may also work - but binanry will be same size anyway =) */
    a = *( ( __packed unsigned int *) &b[5] );
Children
No data