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>Using</strong> <strong>the</strong> Inline and Embedded Assemblers of <strong>the</strong> <strong>ARM</strong> <strong>Compiler</strong><br />

7.32 Embedded assembler syntax in C and C++<br />

An embedded assembly language function definition is marked by <strong>the</strong> __asm function qualifier<br />

in C and C++, or <strong>the</strong> asm function qualifier in C++, and can be used on:<br />

• member functions<br />

• non-member functions<br />

• template functions<br />

• template class member functions.<br />

Functions declared with __asm or asm can have arguments, and return a type. They are called<br />

from C and C++ in <strong>the</strong> same way as normal C and C++ functions. The syntax of an embedded<br />

assembly language function is:<br />

__asm return-type function-name(parameter-list)<br />

{<br />

// <strong>ARM</strong>/Thumb/Thumb-2 assembler code<br />

instruction{;comment is optional}<br />

...<br />

instruction<br />

}<br />

Note<br />

Argument names are permitted in <strong>the</strong> parameter list, but <strong>the</strong>y cannot be used in <strong>the</strong> body of <strong>the</strong><br />

embedded assembly function. For example, <strong>the</strong> following function uses integer i in <strong>the</strong> body of<br />

<strong>the</strong> function, but this is not valid in assembly:<br />

__asm int f(int i)<br />

{<br />

ADD i, i, #1 // error<br />

}<br />

You can use, <strong>for</strong> example, r0 instead of i.<br />

Example 7-1 shows a string copy routine as a not very optimal embedded assembler routine.<br />

Example 7-1 String copy with embedded assembler<br />

#include <br />

__asm void my_strcpy(const char *src, char *dst)<br />

{<br />

loop<br />

LDRB r2, [r0], #1<br />

STRB r2, [r1], #1<br />

CMP r2, #0<br />

BNE loop<br />

BX lr<br />

}<br />

int main(void)<br />

{<br />

const char *a = "Hello world!";<br />

char b[20];<br />

my_strcpy (a, b);<br />

printf("Original string: '%s'\n", a);<br />

printf("Copied string: '%s'\n", b);<br />

return 0;<br />

}<br />

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

ID061811<br />

Non-Confidential

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

Saved successfully!

Ooh no, something went wrong!