ARM assembly and C mixed programming

Publisher:画意人生Latest update time:2020-08-04 Source: elecfansKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Inline assembly is to use assembly statements directly in C to program, so that the program can implement some tasks that cannot be completed by C language in C program. For example, in the following cases, inline assembly or embedded assembly program must be used to use saturated arithmetic operations (Saturating ArithmeTIc)


The program needs to operate the coprocessor

Complete the operation of the program status register in the C program

__asm__ __volatile__("asm code":output:input:changed registers);

Note:

Using __asm__ and __volume__ means that the compiler will not check the following content, but will hand it directly to the assembler.

If you want to optimize the transformer, __volatile__ can be omitted

There is no asm code and "" cannot be omitted

There is no preceding and middle part, and it cannot be omitted accordingly:

There is no changed part and it must be omitted accordingly:

The final ; cannot be omitted. For C language, this is a statement.

The assembly code must be placed in a string


asm code

"mov r0, r0nt" "mov r1,r1nt" "mov r2,r2"

output(asm->C)

:"constr ai nt" (variable)

"Constraint" is used to define the storage location of the variable:
r means use any available register
m means use the memory address of the variable
+ readable and writable
= write-only
& means that the output operand cannot use the register used by the input part and can only be used in the form of "+&" or "=&"

input(C->asm)

:"constraint" (variable/immediate)

"Constraint" is used to define the storage location of the variable:
r means use any available register (both immediate and variable)
m means use the memory address of the variable
i means use immediate

example

int a=100,b=200;int result;__asm__ __volatile__( "mov %0,%3nt" //%0 is a placeholder, indicating result, and the same applies to the following "ldr r0,%1nt" "ldr r1,%2nt" "str r0,%2nt" "str %1,%1nt" :"=r"(result),"+m"(a),"+m"(b) :"i"(123));

ATPCS

Parameters are passed between subroutines through registers R0~R3. If there are more than four parameters, the extra parameters are passed using the stack. The called subroutine does not need to restore the contents of registers R0~R3 before returning.


In a subroutine, registers R4 to R11 are used to save local variables. If some registers in R4 to R11 are used in a subroutine, the values ​​of these registers must be saved when the subroutine is entered, and the values ​​of these registers must be restored before returning. For registers not used in the subroutine, these operations do not need to be performed. In a Thumb program, usually only registers R4 to R7 can be used to save local variables.


R12 is used as the scrtach register between subroutines (used to save SP, and used to pop the stack when the function returns), denoted as ip

R13 is used as the data stack pointer, denoted as sp

R14 is used as a link register, denoted as lr

R15 is recorded as the program register, denoted as pc

Mutual calls

When calling between C and assembly, special attention should be paid to complying with the corresponding ATPCS rules

C calling assembly

//. c# include extern void strcopy(char* des, const char* src);int main(){ const char* srcstr = "src string"; char desstr[]="des string"; strcopy(desstr, srcstr); return 0;}

;.asm.global strcopystrcopy: ;R0 points to the destination string;R1 points to the source stringLDRB R2, [R1], #1; load bytes and update the source string pointer addressSTRB R2, [R0], #1; store the season and update the destination string pointer addressCMP R2, #0; determine whether it is the end of the stringBNE strcopy; if not, the program jumps to strcopy and continues the loopMOV pc, ir; the program returns


Assembly calls C

//.cint fcn(int a, int b, int c, int d, int e){ return a+b+c+d+e;}

;.asm;Assume that the value in R0 is i when the program enters f;int f(int i){return fcn(i, 2*i, 3*i, 4*i, 5*i);}.text.global _start_start: STR lr, [sp, #-4]! ;Save the return address lr ADD R1, R0, R0 ;Calculate 2*i (the second parameter) ADD R2, R1, R0 ;Calculate 3*i (the third parameter) ADD R3, R1, R2 ;Calculate 5*i STR R3, [SP, #-4]! ;The fifth parameter is passed through the stack ADD R3, R1, R1 ;Calculate 4*i (the fourth parameter) BL fcn ;Call C program ADD sp, sp, #4 ;Delete the fifth parameter from the stack.end

Keywords:ARM Reference address:ARM assembly and C mixed programming

Previous article:Implementation of FPGA Embedded System Based on ARM
Next article:Do you know the Arm Linux system call process?

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号