Arm Instruction Set and Mixed Programming

Source: Internet
Author: User

I. Arm Instruction Set

1. Redirect command: B, BL (with return), blx, and Bx (with status)

2. Data Transmission: mov (Universal storage zone), MVN (bitwise inversion), CMP (CPSR exists in comparison results), and TST (bitwise), add, sub, And, Orr, Bic, Mul,

3. Access status registers: MSR and Mrs

Ii. assembly instruction sets

1. LDR, STR (B)

2. STR,

3. LDM and STM (batch processing)

4. SWP

5. LSl and ror

6. SWI and bkpt

Iii. pseudoinstructions

1. gbla, gbll, g133, llinoleic, lcll, lcls, Seta, SETl, sets,

2. rlist

3. DCB (Distributed Storage Unit)

4. Space

5. Map

6. If, else, And endif

7. While, Wend,

8. Area, code, rdonly, export, and import

IV. C and Assembly hybrid programming

In the development of embedded systems, the main programming languages currently used are C and assembly. c ++ has a corresponding compiler, but it is still rarely used. In a relatively large-scale embedded software, such as operating systems, most of the Code is written in C, mainly because the C language structure is better and easier for people to understand, there are also a large number of supported libraries. However, assembly languages are used in many places, such as hardware system initialization during startup, including CPU status setting, interrupt enabling, and clock speed setting, as well as Ram control parameters and initialization, some interrupt processing may also involve compilation. Another place to use assembly is some code blocks that are very performance-sensitive. This is not to rely on the C compiler to generate code, but to manually compile the Assembly for optimization purposes. In addition, the Assembly language is closely linked to the CPU instruction set. As an embedded system involving the underlying development, it is also necessary to skillfully use the corresponding assembly language.

For simple C or assembly programming, please refer to relevant books or manuals. Here we mainly discuss mixed programming of C and assembly, including mutual function calls. The following is a discussion in four cases. c ++ is not involved currently.

1. Embedded Assembly in C Language

The Assembly commands embedded in C contain most of the arm and thumb commands. However, their usage is somewhat different from the commands in the Assembly file, and there are some restrictions, mainly including the following aspects:

A. You cannot assign values directly to the PC register. B or bl commands are required for program redirection.

B. Do not use overly complex C expressions when using physical registers to avoid physical register conflicts.

C. R12 and R13 may be used by the compiler to store intermediate compilation results. When calculating expression values, R0 to R3, R12, and R14 may be used for subroutine calls. Therefore, avoid directly using these physical registers.

D. Generally, do not directly specify physical registers for the compiler to allocate.

The marker used for Embedded Assembly is the _ ASM or ASM keyword. The usage is as follows:

_ ASM

{

Instruction [; instruction]

...

[Instruction]

}

 

ASM ("Instruction [; instruction]");

The following example shows how to embed the Assembly Language in C,

# Include <stdio. h>

 

Void my_strcpy (const char * SRC, char * DEST)

{

Char ch;

_ ASM

{

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", );

Printf ("copyed: % s", B );

Return 0;

}

Here, the value transfer between C and assembly is implemented using the C pointer, because the pointer corresponds to the address, so it can also be accessed in assembly.

 

2. Use global variables defined by C in assembly

Embedded Assembly does not need to edit assembly language files separately. It is concise but has many restrictions. When there are a large number of assembly code, it is generally placed in a separate Assembly file. In this case, we need to transfer some data between Assembly and C. The simplest way is to use global variables.

/* Cfile. c

* Defines global variables and serves as the main program

*/

 

# Include <stdio. h>

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;

}

 

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. Call the compiled function in C.

There are two main tasks to call functions in the Assembly file in C. One is to declare the function prototype in C and add the extern keyword. The other is to use export to export the function name in the Assembly, use the function name as the identifier of the assembly code segment, and return the result using mov PC and LR. Then, you can use this function in C. From the perspective of C, I don't know whether the function is implemented in C or assembly. The deeper reason is that the function name of C indicates the start address of the function code, which is consistent with the label of the Assembly.

/* Cfile. c

* In C, call an ASM function, asm_strcpy

* Sep 9, 2004

*/

 

# Include <stdio. h>

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;

}

 

; 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, the parameter transfer between C and assembly is implemented according to the atpcs (arm thumb procedure call standard. Simply put, if the function has no more than four parameters, corresponding to the use of R0-R3 to pass, more than four with stack, the return value of the function is returned through R0.

 

4. Call the C function in the Assembly.

To call a C function in an assembly, you need to import the corresponding C function name in the Assembly, and then put the C code in an independent C file for compilation, the rest of the work is 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

Area asmfile, code, readonly

Import cfun

Entry

MoV r0, #11

MoV R1, #22

MoV R2, #33

BL cfun

End

 

/* C file, called by asmfile */

 

Int cfun (int A, int B, int C)

{

Return A + B + C;

}

Call the C function in the Assembly, and the parameter transmission is also implemented through atpcs. It should be noted that when the number of function parameters is greater than 4, stack should be used. For details, see the atpcs specification.

5. Arm coprocessor

1. coprocessor chip, CP15,

2. CDP, LDC, STC, MCR, and MRC

3. MRC P15, 0, R1, C0, C0, 2 put the status C0 to the Register r1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.