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

*((int *)&Buf[2]) = 0x12345678;

RVMDK v3.00a, Hello World sample for RealView compiler

#pragma pack(1)

unsigned char Buf[40];

int main (void)
{
*((short int *)&Buf[0]) = 0x77ff;
*((int *)&Buf[2]) = 0x12345678;
/* initialize the serial interface */
PINSEL0 = 0x00050000;
U1LCR = 0x83;
U1DLL = 97;
U1LCR = 0x03;

printf ("Hello World\n");
printf("%x %x %x %x \n", Buf[0],Buf[1],Buf[2],Buf[3]);
}

Hello World

78 56 34 12
------------
The same thing is for CARM compiler 2.4, 2.5

What I am wrong ?

Parents Reply Children
  • Well, there is one.

    _packed extention


    Kind regards to those goblins who write doc's and readme!

  • Well, I am trying to write a word to an address location which is not aligned to a 4-byte boundary. But Is not it compiler realisation's problem/task ?

    It appears you try to force the CPU to do what is not designed for, but it speaks gently back to you, for the unaligned word access will perform a deterministic byte rotation of word reads (ldr); and it will mask the unaligned address with 0xFFFFFFFE for unaligned writes (str).
    On ARM cores with a system control processor (such as CP15 on ARM720T), one can select whether to raise a data abort exception on unaligned access to memory, or not.
    On ARM7TDMI-S, this is not an option, and therefore the unaligned accesses will *not* raise data abort exception, even though ARM ARM states so. Be carefull going from general to specific claims, it all depends on the context and implementation.
    It is not a compiler's problem.

    Be well.

  • It's the compiler's job (actually the linker's) to make sure that multi-byte objects are properly aligned. But once you start taking addresses of individual bytes and manipulating those addresses, you take the responsibility of maintaining proper alignment on yourself.

    Few RISC processors operate correctly with misaligned data. Most of them take an exception. The ARM core does not, but external memory hardware might.

  • Kind regards to those goblins who write doc's and readme!

    'Goblins'. very kind, indeed. I can think of different words.

    Erik