01.12.2014 Views

RealView Compilation Tools Developer Guide - ARM Information ...

RealView Compilation Tools Developer Guide - ARM Information ...

RealView Compilation Tools Developer Guide - ARM Information ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

混 合 使 用 C、C++ 和 汇 编 语 言<br />

从 汇 编 语 言 调 用 C<br />

示 例 5-10 和 示 例 5-11 显 示 如 何 从 汇 编 语 言 调 用 C。<br />

示 例 5-10 定 义 C 语 言 函 数<br />

int g(int a, int b, int c, int d, int e)<br />

{<br />

return a + b + c + d + e;<br />

}<br />

示 例 5-11 汇 编 语 言 调 用<br />

; int f(int i) { return g(i, 2*i, 3*i, 4*i, 5*i); }<br />

EXPORT f<br />

AREA f, CODE, READONLY<br />

IMPORT g<br />

; i is in r0<br />

STR lr, [sp, #-4]! ; preserve lr<br />

ADD r1, r0, r0 ; compute 2*i (2nd param)<br />

ADD r2, r1, r0 ; compute 3*i (3rd param)<br />

ADD r3, r1, r2 ; compute 5*i<br />

STR r3, [sp, #-4]! ; 5th param on stack<br />

ADD r3, r1, r1 ; compute 4*i (4th param)<br />

BL g<br />

; branch to C function<br />

ADD sp, sp, #4 ; remove 5th param<br />

LDR pc, [sp], #4 ; return<br />

END<br />

从 C++ 调 用 C<br />

示 例 5-12 和 第 5-24 页 的 示 例 5-13 显 示 如 何 从 C++ 调 用 C。<br />

示 例 5-12 从 C++ 调 用 C 函 数<br />

struct S {<br />

// has no base classes<br />

// or virtual functions<br />

S(int s) : i(s) { }<br />

int i;<br />

};<br />

extern "C" void cfunc(S *);<br />

// declare the C function to be called from C++<br />

<strong>ARM</strong> DUI 0203BSC © 2002、 2003 <strong>ARM</strong> Limited 版 权 所 有 。 保 留 所 有 权 利 。 5-23

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

Saved successfully!

Ooh no, something went wrong!