Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to mix C language with ARM assembly language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how the C language and ARM assembly language mixed programming, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

A brief introduction to ARM assembly language

What is assembly language? Assembly language is any low-level language suitable for electronic computers, microprocessors or other programmable devices. Although it is called a "low-level language", it does not mean that assembly language is really "low-level". Specific assembly language and specific machine language instruction set are one-to-one corresponding, and can not be directly transplanted between different platforms. Assembly language mainly includes instructions for transmission, logic operation, shift instruction, bit operation, control transfer, string operation, input and output and so on.

Second, C language calls assembly language 1. Call with no parameter

Creating project files in Keil has been introduced in my previous blog, and I won't say much about it here.

VScode + keil development environment build installation and use process https://www.yisu.com/article/218432.htm

Using VSCode and VS2017 to compile and debug STM32 programs https://www.yisu.com/article/186069.htm

First create the project file, and then add CORE and Starup

Then right-click Source Group in the project file on the left and select Add New Item.

Add Fun.s and main.c files in turn

Func.s

AREA My_Function,CODE,READONLY; this line must be a template EXPORT Init_1 except that My_Function can name itself; associated with the Init_1 function defined in the C file The declaration and usage variables in the high-level language are actually the use of registers, so we only need to use registers directly, so we can Init_1 MOV R1 registers as I MOV R2 registers and R2 registers as j LOOP. Written on the far left is the name of the program segment, which is used to execute the jump program using CMP R1 BHS LOOP_END 10; compare the size of R1 with 10 BHS LOOP_END; if R1 is greater than or equal to 10, jump to the LOOP_END segment, otherwise ignore the statement, and directly execute the following statements: ADD R2 language 1; jacks + ADD R1 paragraph 1 After executing a loop, enter the loop judgment again unconditionally, that is, jump to the LOOP segment LOOP_END NOP END; you must leave a space before writing END, otherwise it will be considered as the segment name, indicating the end of the program.

Main.c

# include extern void Init_1 (void); int main () {Init_1 (); return 0;}

Note: you need to check the software debugging, and then modify the parameters. The chip selected at the beginning is stmf103c8, so enter-pSTM32F103C8 here in Parameter.

Set breakpoint

You can observe the operation of the program through register changes.

two。 Call with parameters

Simply modify the code

Func.s

AREA My_Function,CODE,READONLY; everything in this line is a template EXPORT Init_1 except that My_Function can name it itself; associated with the Init_1 function defined in the C file The declaration and usage variables in the high-level language are actually the use of registers, so we only need to use registers directly to Init_1 ADD R0 LOOP 100; add the passed value + 100 MOV PC,LR; return R0 LOOP; what is written on the far left is the segment name of the program segment, which is used when executing the jump program. Compare the size BHS LOOP_END of R1 and 10; if R1 is greater than or equal to 10, jump to the LOOP_END section, otherwise ignore the statement, and directly execute the following statements: ADD R2 ADD 1; jacks + ADD R1 LOOP 1; iBesides + B program After executing a loop, enter the loop judgment again unconditionally, that is, jump to the LOOP segment LOOP_END NOP END; you must leave a space before writing END, otherwise it will be considered as the segment name, indicating the end of the program.

Main.c

# includeextern int Init_1 (int x); int main () {int x = Init_1; printf ("% d", x); return 0;}

Debugger observes register changes

At this point, the value of the R1 register is 0x208, which is 520, and the call is successful.

III. Assembly language calls C language

Modify the code

Func.s

AREA My_Function,CODE,READONLY; all that is required in this line is the template EXPORT Init_1 except that My_Function can name itself; IMPORT return1 is associated with the Init_1 function defined in the C file; declares return1 as an external reference The declaration and usage variables in the high-level language are actually the use of registers, so we only need to use registers directly to Init_1 ADD R0 LOOP 100; add the passed value + 100 MOV PC,LR; return R0 LOOP; what is written on the far left is the segment name of the program segment, which is used when executing the jump program. Compare the size BHS LOOP_END of R1 and 10; if R1 is greater than or equal to 10, jump to the LOOP_END section, otherwise ignore the statement, and directly execute the following statements: ADD R2 ADD 1; jacks + ADD R1 LOOP 1; iBesides + B program After executing a loop, enter the loop judgment again unconditionally, that is, jump to the LOOP segment LOOP_END NOP END; you must leave a space before writing END, otherwise it will be considered as the segment name, indicating the end of the program.

Main.c

# includeextern int Init_1 (int x); int return1 (void); int main () {int x = Init_1; printf ("% d", x); return 0;} int return1 (void) {return1;}

Compile and debug

You can see that the value of R0 changes to 1, which shows that the C function is called successfully.

Thank you for reading this article carefully. I hope the article "how to mix C language with ARM assembly language" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 213

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report