37.C and assembly mixed programming

Source: Internet
Author: User

37.C and assembly mixed programming

New light.c:

Modify start. S

Modify Makefile: Add LIGHT.O

Finally make successful, burn write success.

Again, we can call the functions in our assembly in the C function:

Modify start. S: Declare light_led as global:

is called in light.c:

Make compile-and-write to the Development board: The lights can be lit. The call assembler function succeeds in C.

The next step is to embed the assembly code in C instead of calling:

Embed assembler code in C:

Format:

C Inline assembly begins with the keyword "__asm__" or "ASM", containing four parts of the content, for example, separate parts with ":", the first part must be written, the following three parts can be ignored, but the semicolon: cannot be omitted.

    1. Assembly statements
    2. Output section: C variables modified in the language
    3. input section: from C arguments coming in
    4. Destruction Description: The value of the register is modified and the register is listed here.

Simple example:

read out the value of the C1 register in the CPSR in the C language:

In the assembly statement above, notice that the position of the Universal register is a parameter%0, and that all the parts to be read are placed in the input section, and all the parts to be written are placed in the output section.

The "R" in the input section above is a general-purpose register, assigned by the system, followed by parentheses value, which is present in the register.

The value of the C1 register written in the CPSR is implemented in the C language:

The parameter register is the value of the register in the output CPSR.

The equals sign is write-only, which is the write-only operation of register r, and the value in the register is written in value.

The destruction section has memory, which tells the system to modify the value of the variable in memory in the Assembly statement.

Volatile is telling the CPU not to optimize the code:

To achieve illumination:

The above with%0 and% means that this is a two parameter, the value of the parameter is two general register value. "R" in "R" (Gpkcon) represents a universal register with a value of Gpkcon. "R0" is the destruction department because the entire operation affects the value of R0.

Make Burn and the light is on.

37.C and assembly mixed programming

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.