Arm and C Mixed Programming

Source: Internet
Author: User

 

First: Basic knowledge

(1) Register, use rules for parameter passing

A. In the subroutine, use the register R4 ~ R11 to save local variables.

B. Register R12 is used for Scratch registers between subprograms (used to save the SP and use this register to exit the register when the function returns) and is recorded as IP addresses.

C. Register R13 is used as the data stack pointer and recorded as sp. The SP value in the register must be the same as that in the exit subroutine.

D. Register R14 is called a link register and recorded as LR. It is used to save the return address of the subroutine.

E. Register R15 is a program counter, which is recorded as a PC

F. When there are no more than four parameters, you can use the register R0 ~ R3 to pass parameters. When there are more than four parameters, you can also use the data stack to pass parameters.

G. When the result is a 32-bit integer, it can be returned through the R0 register.

H. When the result is a 64-bit integer, it can be returned through the registers R0 and R1, and so on.

(2) stack usage rules the stack uses the full decline type (FD, full descending), that is, the stack grows down by reducing the memory address, and the stack pointer points to the lowest address containing valid data items. Second: Instances(1) Calling the Assembly function (Mark) Init. s in C Language
   IMPORT funEXPORT fun_asmAREA |C$$code|, CODE, READONLYPRESERVE8ENTRYstartBL funfun_asmLDMFD R13!,{R4-R8}  MOV r0,r4END

Test. c

extern  fun_asm();int fun(){fun_asm(1,2,3,4,5,6,7,8);return 0;}

(2) Embedded Assembly statements in C program

_ ASM {Command [; command]…… [Command]}

Note: "_ ASM" is the keyword for Embedded Assembly statements. Note that there are two underscores in front. Commands are separated by semicolons. If one command occupies multiple lines, use the hyphen "/" except the last line.
(3) Use the C-Defined Function init. s in the Assembly.

Import funexport test_asm_argsarea | C $ code |, code, readonlypreserve8entrystartstr LR, [Sp, #-4]! ; Save the current lr ldr r0, = 0x1; parameter 1 LDR R1, = 0x2; parameter 2 LDR R2, = 0x3; parameter 3 LDR R3, = 0x4; parameter 4 LDR R4, = 0x8 running fd sp !, {R4}; STR R4, [Sp, #-4]! ; Method 2 parameter 5; LDR R4, = 0x7; STR R4, [Sp, #-4]! ; Method 2 parameter 6; LDR R4, = 0x6; STR R4, [Sp, #-4]! ; Method 2 parameter 7; LDR R4, = 0x5; STR R4, [Sp, #-4]! ; Method 2 parameter 8 BL fun end

Test.c
extern  test_asm_args();    int fun(int i,int j,int x,int y,int z,int a)    {test_asm_args(1,2,3,4,5,6,7,8);return 0;    }

(4) use the global variable assembly language defined by C in Assembly to introduce

Import var VAR (global variable name in C)


  • Previous Article: Power Designer vbs uses EXCEL to generate entities without one input.
  • Next article: three points of function parameters in C Language
  • 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.