Category:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
0
1
2
3
4
5
6
7
8
9
Position:IcFull.com » IC Electronic information » Category: A

IC Electronic information

ARM in C and assembly programming and sample

In Electronic Infomation Category: A | on November 30,2010

In embedded systems development, currently used mainly and AD768AR datasheet and assembler programming language is C, C + + compiler has been a corresponding, but now use is still relatively small. In a little large-scale embedded software, such as containing the OS, most of the code is written using C, mainly because the structure of C language better, to facilitate peoples understanding, and AD768AR price and there are a lot of support libraries.

However, many places still have to use the assembly language, for example, the initialization of the hardware system boot time, including the CPU state setting, the interrupt is enabled, the main frequency settings, and AD768AR suppliers and RAM of the control parameters and initialization, a number of interrupt processing may also be involved in assembly. Another is to use the assembly where the performance is very sensitive to some block of code, which can not rely on C compiler generated code, and to hand-write assembly, to optimize the objective. Moreover, assembly language and the CPUs instruction set is closely linked, as related to the underlying embedded system development, skilled use of the corresponding assembly language is necessary.

pure C or assembly programming, please refer to the relevant books or manuals, this is mainly to discuss the mixed C and assembly programming, including function calls between . The following discussion points to four cases, temporarily involved in C + +.

1. Embedded in the C language compilation

embedded in C, assembly instructions and includes most of the ARM Thumb instructions, but its use with the assembly file instruction is somewhat different, there are some limitations, mainly the following aspects:

a. can not be assigned directly to the PC register, the program jumps to use B or BL instruction

b. in the use of physical registers, do not use too complex C expressions, the physical registers to avoid conflicts

c. R12 and R13 may be used to store intermediate compiler compile the results, evaluate the expression values may be R0 to R3, R12 and R14 for subroutine calls, so to avoid direct use of these physical registers

d. generally do not directly specify the physical register, and let the compiler assign

tag is used inline assembly or asm __asm keyword, use the following:

__asm

{

instruction [ ; instruction]

...

[instruction]

}

asm ("instruction [; instruction]");

following an example to illustrate how to embed assembly language in C,

# include

void my_strcpy (const char * src, char * dest)

{

char ch;

__asm

{< / P>

loop:

ldrb ch, [src], # 1

strb ch, [dest], # 1

cmp ch, # 0

bne loop

}

}

int main ()

{

char * a = "forget it and move on!";

char b [64];

my_strcpy (a, b);

printf ("original:% s", a);

printf ("copyed:% s", b);

return 0;

} < / P>

where the value of C and assembly to pass between the C pointer is implemented as a pointer corresponding to the address, so the compilation can also visit.

2. Used in the compilation of global variables defined in C

inline assembly language assembler files do not have a separate editorial, relatively simple, but there are many limitations, when the assembly code more when the general assembly in a separate file. This required the compilation and C in some of the data transfer between the easiest way is to use global variables.

/ * cfile.c

* define global variables, and as a caller

* /

# include

int gVar_1 = 12;

extern asmDouble (void);

int main ()

{

printf ("original value of gVar_1 is:% d", gVar_1);

asmDouble ();

printf ("modified value of gVar_1 is:% d" , gVar_1);

return 0;

}

the corresponding assembly language file

; called by main (in C), to double an integer, a global var defined in C is used.

AREA asmfile, CODE, READONLY

EXPORT asmDouble

IMPORT gVar_1

asmDouble

ldr r0, = gVar_1

ldr r1, [r0]

mov r2, # 2

mul r3, r1, r2

str r3, [r0]

mov pc, lr

END

3. Calling assembly in the C function

call in the C function in the assembly file, do the main work has two, one in C, function prototype declarations, and add extern keyword; the second is used in the compilation EXPORT export function name, and use the function name as the identity of assembly code, and finally mov pc, lr return. You can then use this function in C a. From Cs point of view, does not know the realization of this function is C or assembler. Deeper reason is that C function name to play shows around the starting address of the function code, and compilation of the label that is consistent.

/ * cfile.c

* in C, call an asm function, asm_strcpy

* Sep 9, 2004

* /

# include

extern void asm_strcpy (const char * src, char * dest);

int main ()

{

const char * s = "seasons in the sun";

char d [32];

asm_strcpy (s, d) ;

printf ("source:% s", s);

printf ("destination:% s", d);

return 0; < / P>

}

; asm function implementation

AREA asmfile, CODE, READONLY

EXPORT asm_strcpy

asm_strcpy

loop

ldrb r4, [r0], # 1; address increment after read

cmp r4, # 0

beq over

strb r4, [r1], # 1

b loop

over

mov pc, lr

END

Here, C and assembly to pass through between the parameters ATPCS (ARM Thumb Procedure Call Standard) to carry out the provisions of the. Simply means that if the function has more than four parameters, corresponding with the R0-R3 to be passed, more than 4 with the help of the stack, the return value to return by R0.

4. In the compilation of a function call C

in the assembly to call C functions, you need to IMPORT in the compilation of the corresponding C function name, and then the code C in a separate C file to compile, and the rest handled by the connector.

; the details of parameters transfer comes from ATPCS

; if there are more than 4 args, stack will be used

EXPORT asmfile < P> AREA asmfile, CODE, READONLY

IMPORT cFun

ENTRY

mov r0, # 11

mov r1, # 22 < / P>

mov r2, # 33

BL cFun

END

/ * C file, called by asmfile * / < P> int cFun (int a, int b, int c)

{

return a + b + c;

}

in the compilation of C function call, parameter passing is achieved through ATPCS. Should be noted that when the function is greater than the number of parameters 4, to help stack, see specific ATPCS specification.

Summary

more than a few simple examples demonstrate the development of commonly used embedded C and assembly programming for some of the ways and the basic idea, in fact, the core question is how to pass values between C and assembly, the remaining problem is their own way to deal with. These are just initiate a more detailed and complex to use and practical application to refer to the relevant information.

AD768AR datasheetAD768AR suppliersAD768AR Price

Related technical information

All right reserved:icfull.com © 2010-2016 Certificate