dsidlauskas1 wrote:
> Consider the following code:
>
> ============================
> char buf[]={1,2,3,4,5,6,7,8};
> int *ip, x[4];
>
> for (i=0; i<4; i++)
> {
> ip = (int *)&buf[i];
> x[4] = *ip;
> }
> =============================
> The Keil compiler compiles this without warning, but does not produce
> the expected (for me) result in x. The problem is that Keil uses the
> LDR instruction to effect the transfer and this is valid only on 4
> byte boundaries.
>
> GCC compiles but does give a non-aligned access warning.
>
> I believe that the compiler has enough information to use byte aligned
> transfers, and should, or at least give a warning.
>
> Anybody want to weigh in on this one.
>
> Thanks in advance for your comments.
>
> Dave Sidlauskas
>
Hello Dave,
I'm not really sure what you expect from this piece of code, but I think your try to copy these 8
bytes in 4 half-word (16bit) operations in a loop. If yes you should increment your counter variable
i by 2 instead of 1.
I would suggest:
==================================================
char buf[]={1,2,3,4,5,6,7,8};
int *ip, x[4]; /* assuming that int is 16bit-wide on your compiler */
ip = (int*) buff;
for (i=0; i<4; i++) {
x[i] = *ip++; /* copy content with halfword-post-increment */
}
===================================================
--
/************************************************
Do you need a tiny and efficient real time
operating system (RTOS) with a preemtive
multitasking for LPC2000 or AT91SAM7?
http://nanortos.net-attack.de/
Or some open-source tools and code for LPC2000?
http://www.net-attack.de/
************************************************/Message
Re: [lpc2000] For C Experts
2006-03-30 by Sten
Attachments
- No local attachments were found for this message.