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
Previous article:Implementation of FPGA Embedded System Based on ARM
Next article:Do you know the Arm Linux system call process?
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- TMC5130 is used as a driver to drive the motor. Why does the motor hum when it is stationary?
- Live broadcast at 10 am today [TI's optimized solutions in industrial multi-protocol communication applications]
- Made an LED music spectrum analyzer
- EEWORLD University Hall----TI Smart Speaker and Sound Bar- New Power Design Trend
- What parameters should be paid attention to when selecting optocouplers?
- Is there any difference between bit rate and airspeed?
- TFT LCD display realizes dial pointer clock
- Power conversion module 220V to 18V
- About DSP28335 SPI transmission
- Thirteen typical application circuits of light-emitting diode indicator lights