Embedded Linux arm compilation (vii)--C language and arm assembly hybrid programming

Source: Internet
Author: User

Embedded Linux arm Compilation (vii)--C language and arm Assembly mixed programming

in the development of embedded system, the main programming language currently used is C and assembly. In large-scale embedded software, such as the OS, most of the code is written in C, mainly because C language structure is better, easy to understand, and there are a large number of support libraries. However , many places still need to use assembly language, such as the initialization of hardware system when booting, including the CPU state setting, interrupt enable, the setting of the main frequency, as well as the control parameters and initialization of RAM, some interrupt processing may also involve the assembly. Another place to use the assembly is some very sensitive code blocks, which can not rely on the C compiler generated code, but to manually write the assembly, to achieve the purpose of optimization. Moreover, the Assembly language is closely related to the instruction set of the CPU, as the embedded system involved in the development of the underlying, proficiency in the use of assembly language is also necessary. This article will introduce the mixed programming of C language and arm assembly language.

call ARM assembler function inC language

in C, calling the function in the assembly file, the main work to do is two, one is to declare the function prototype in C, and add the extern keyword, and the second is to export the function name in the assembly, and use the function name as the assembly code snippet identification, and finally with mov pc,lr returns.

ARM Assembler Program:

Area Scopy,code32,readonly

EXPORT strcpy

strcpy

LDRB R2,[r1], #1

STRB R2,[r0], #1

CPM R2, #0

BNE strcpy

MOV PC,LR

END

C Language Program:

#include <stdio.h>

extern void mystrcpy (char *des, char *src);

int main (int argc, char *argv[])

{

Char strsrc[100] = "Hello";

Char strdes[100] = {0};

printf ("Copy string:%s\n", STRSRC);

mystrcpy (Strdes, STRSRC);

printf ("%s:%s\n", STRSRC, (const char *) strdes);

return 0;

}

Second, theARM assembler call C language function

The assembler is designed to comply with ATPCS and ensure that the parameters are passed correctly when the program is called. Use the import pseudo operation in the assembler to declare the C program function that will be called, when calling the C program, set the entry parameters correctly, and then use the BL call.

C Language Program:

int sum (int a, int b, int c)

{

Return a + b +c;

}

ARM Assembler Program:

Area Function,code,readonly

ENTRY

IMPORT sum

MOV R0, #1 // parameter a

MOV R1, #2 // parameter b

MOV R3, #3 // parameter c

BL sum

STOP

B STOP

END

Third, C Language program embedded ARM assembly Instructions

in ClanguageThe assembly directives embedded in the command contain most of the arm and thumb instructions, but their use differs from the instructions in the assembly file, there are some limitations, mainly the following
several aspects:
A,can not be directly assigned to the PC register, the program jumps to use B or BL instruction
B,do not use overly complex C expressions to avoid physical register collisions when using physical registers
C,R12 and R13 may be used by the compiler to hold intermediate compilation results, while evaluating expression values may be used to R0 to R3, R12, and R14 for subroutine calls, so avoid using these physical registers directly
D,generally do not specify physical registers directly, but let the compiler assign
tags used in inline assembly are __asm or ASM keywords

Syntax format for inline assembly:

__asm

{

instruction [; instruction]

...

[Instruction]

}

Program Examples:

#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

}

}

The value passing between C and the Assembly is implemented by a pointer to C.


This article from "Endless life, Struggle not only" blog, please be sure to keep this source http://9291927.blog.51cto.com/9281927/1786072

Embedded Linux arm compilation (vii)--C language and arm assembly hybrid programming

Related Article

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.