29.10.2014 Views

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

ARM Compiler toolchain v4.1 for µVision Using the Compiler

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Compiler</strong> Coding Practices<br />

5.33 Unaligned data access in C and C++ code<br />

It can be necessary to access unaligned data in memory, <strong>for</strong> example, when porting legacy code<br />

from a CISC architecture where instructions are available to directly access unaligned data in<br />

memory.<br />

On <strong>ARM</strong>v4 and <strong>ARM</strong>v5 architectures, and on <strong>the</strong> <strong>ARM</strong>v6 architecture depending on how it is<br />

configured, care is required when accessing unaligned data in memory, to avoid unexpected<br />

results. For example, when a conventional pointer is used to read a word in C or C++ source<br />

code, <strong>the</strong> <strong>ARM</strong> compiler generates assembly language code that reads <strong>the</strong> word using an LDR<br />

instruction. This works as expected when <strong>the</strong> address is a multiple of four, <strong>for</strong> example if it lies<br />

on a word boundary. However, if <strong>the</strong> address is not a multiple of four, <strong>the</strong> LDR instruction returns<br />

a rotated result ra<strong>the</strong>r than per<strong>for</strong>ming a true unaligned word load. Generally, this rotation is not<br />

what <strong>the</strong> programmer expects.<br />

On <strong>ARM</strong>v6 and later architectures, unaligned access is fully supported.<br />

5.33.1 See also<br />

Concepts<br />

• Types of data alignment on page 5-41<br />

• The __packed qualifier and unaligned data access in C and C++ code on page 5-46.<br />

<strong>ARM</strong> DUI 0375C Copyright © 2007-2008, 2011 <strong>ARM</strong>. All rights reserved. 5-45<br />

ID061811<br />

Non-Confidential

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!