The interaction of C with the assembly in CORTEX-M3

Source: Internet
Author: User

The following is excerpted from the ARM cortex-m3 authoritative guide

Overview

programming on CM3, you can use both C and assembly. There may be other languages in the compiler, but most people will still be in C and the Assembly of the world cruising. C and the assembly are Chang, cannot replace each other. Use C to develop large programs, while compilations are used to perform special tasks.

When do I use the assembly?

If the project is relatively small, it is often possible to use a pure assembly, and you can optimize and control the program. However, your development cycle will grow longer. Especially when engineering becomes larger, you need to deal with more complex data structures, and when you want to manage libraries, you will find that the compilation will increase the workload. All kinds of addresses and indirect references are complicated; bug slashing; even a few days can not finish, it is self-abuse. Of course, if you want to become a system development of prawns, you have to "I do not hell who go to hell," the determination to bravely face, the day after tomorrow, happy and happy.

No matter what, time is precious. We should use C to achieve the large framework of the program, and in line with the principle of good steel used in the cutting edge of the assembly, because only in a few special occasions are not used in assembly language, they are:

Functions that cannot be written in C, such as manipulating special function registers, and implementing mutually exclusive access.
Cheng (for example, NMI service routines) that perform processing at critical juncture.
Memory is extremely limited, and it is possible to squeeze the program or data into it only by using the assembly.
A very high performing subroutine, such as the operating system scheduler.
A subroutine associated with a processor architecture, such as a context switch.
Applications with high performance requirements, such as fire control systems for anti-aircraft guns

How do I embed a compilation in C?

Although the development was greatly accelerated after the use of C, the underlying system control often required assembly code. Many compilers allow you to plug and assemble directly into C code, called Inline assembly, and also allow you to write separate assembly modules that are connected to the compiled C module. In the past, the use of inline compilations was much more, but inline compilations of thumb‐2 directives were not supported in the arm compiler. Instead, starting with version 3.0 of the RealView C compiler, there is a new feature called "Embedded Assembler" that supports thumb‐2 directives. It allows you to insert functions written in assembly language in a C program, for example:

__asm void setfaultmask (unsigned int new_value)
{
Use assembly code here to implement this function
MSR Faultmask, New_value//new_value written to Faultmask
BX LR//Return to main program (not omitted)
}

The RealView C compiler's detailed discussion of the embedded assembly is given in the RVCT 3.0 Compiler and Library Guide (REF6).

assembly interface with C

In many cases, it is necessary to have C program modules interact with assembler modules, which include:
Embedded assembly used in C code (or in the GNU tool, inline assembly is used)
The C program calls the assembler, which is implemented in a separate assembly source file.
The assembler called the C program.

In these cases, you must know how the arguments are passed and how the values are returned in order to work together between the keynote function and the subroutine. These interaction mechanisms are clearly defined in arm and are given by the document ARM Architecture Procedure call Standard (aAPCs, REF5).

In most cases, however, the situation is simple: when the key function needs to pass parameters (arguments), they use R0‐R3. Where R0 passed the first one, R1 passed the 2nd one ... On return, write the return value to R0. In subroutines, you can use R0‐R3, as well as R12 (review the 9th chapter, and think about why you push them). However, if you use R4‐R11, you must push them before use and return with pop.

As can be seen, assembler uses R0‐R3, R12 will be very comfortable. But if the assembly is to call C function, the way to think about the problem is different: you must be aware that subroutines can rewrite R0‐r3, R12, but never change r4‐r11. Therefore, if you need to use r0‐r3,r12 after the call, you must push before the call, and then pop them from the C function, and not worry about R4‐r11. In the example program in this chapter, the vast majority simply call Assembly subroutines, which affect only a small number of registers, or restore the contents of the register before returning, so there is often no strict adherence to aapcs. This is mainly to highlight other priorities, simplify the process, readers should not be in the blind.

The interaction of C with the assembly in CORTEX-M3

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.