ARM assembly and C mixed programming

Publisher:WhisperingWishLatest update time:2020-01-12 Source: eefocusKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

GNU Inline Assembler

Inline assembly is to use assembly statements directly in C for programming, so that the program can implement some tasks that cannot be completed by C language in C program. For example, inline assembly or embedded assembly must be used in the following situations


Using Saturating Arithmetic in Programs

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 __volatile__ 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 or intermediate part, and the corresponding parts cannot be omitted:

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, and you cannot press Enter to wrap the line in the middle of the string. You can write multiple strings. Note that there cannot be any symbols in the middle, which will merge the two strings into one.

There must be a line break between instructions, and you can also use t to keep the instructions neat in the assembly.

asm code

"mov r0, r0nt"

"mov r1,r1nt"

"mov r2,r2"

output(asm->C)

:"constraint" (variable)

"Constraint" is used to define the storage location of the variable:

r means use any available register

m indicates the memory address of the variable to be used

+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 (immediate values ​​and variables are both OK)

m indicates the memory address of the variable to be used

i means using an immediate value


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 strcopy

strcopy: ;R0 points to the destination string

                        ;R1 points to the source string

    LDRB R2, [R1], #1 ; Load bytes and update source string pointer address

    STRB R2, [R0], #1 ;Store the season and update the destination string pointer address

    CMP R2, #0 ; Check if it is the end of the string

    BNE strcopy ;If not, the program jumps to strcopy to continue the loop

    MOV pc, ir ; program returns

Assembly calls C

//.c

int fcn(int a, int b, int c, int d, int e){

    return a+b+c+d+e;

}

;.asm

; Assume that when the program enters f, the value in R0 is i

;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 4th parameter)

    BL fcn ;Call C program

    ADD sp, sp, #4 ; remove the fifth parameter from the stack

    .end


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

Previous article:ARM assembler structure
Next article:ARM register analysis

Recommended ReadingLatest update time:2024-11-16 11:45

【C51 Self-study Notes】Independent keyboard + matrix keyboard
Independent keyboard: keyboard: vKeyboards are divided into coded keyboards (such as PS2 keyboards) and non-coded keyboards. vThe recognition of keys on the keyboard is realized by a dedicated hardware encoder, and the key code number or key value is generated, which is called an encoded keyboard, such as a BCD co
[Microcontroller]
【C51 Self-study Notes】Independent keyboard + matrix keyboard
An article about ARM startup
Well written, should be helpful to everyone: Most ARM-based chips are complex on-chip systems. Most hardware modules in such complex systems are configurable and need to be set to their required working states by software. Therefore, before the user's application program, a special piece of code is required to complet
[Microcontroller]
Research on infrared light vehicle speed management system based on ARM
1 Introduction   Vehicles should travel at a speed that matches the road conditions on the highway. If they travel too fast, accidents are likely to occur. If they travel too slowly, they will become a stumbling block for the following vehicles. However, some drivers often violate traffic rules by not following the
[Microcontroller]
Research on infrared light vehicle speed management system based on ARM
Detailed explanation of absolute address positioning of KEIL C51
       For MCU space allocation, see *.M51 file; for ARM and DSP space allocation, see *.map file        1. Function positioning:        If you want to C source file tools.c function        int BIN2HEX(int xx)        {        ...        }        Place it at 0x1000 of CODE MEMORY, compile the project firs
[Microcontroller]
Interface design of touch screen controller based on HMS30C7202 embedded processor
1.Introduction With the rapid development of modern computer technology and Internet technology, embedded systems have begun to occupy the mainstream of the market. Because the 32-bit ARM embedded processor has high performance and low power consumption, it has been widely used in scientific research, engineering desi
[Microcontroller]
Interface design of touch screen controller based on HMS30C7202 embedded processor
Make keilMDK and C51 not conflict
How to make Keil MDK compatible with Keil C51? I used to use 51 single-chip microcomputer and Keil C51 uVision V4.02; recently I started working on STM32 and installed keilMDK uVision V4.23. Originally I installed keilC51 first and then keilMDK, and they were installed in different directories. After installation,
[Microcontroller]
Arm-Linux secondary page table problem
Previously, I saw in some documents and codes that the secondary page table of arm-linux is divided into Linux version and hardware version. I always felt that the concept was confusing and did not study it carefully. Today, I finally encountered this problem and had to learn it. During do_page_fault(), the following
[Microcontroller]
ARM interrupt vector program example
ARM interrupt handlers generally handle various exception situations through the exception vector table. Interrupts are also an abnormal event. When an exception occurs, ARM will automatically jump to the address specified by the vector table to execute the corresponding processing function. /Create vector table fil
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号