Mixed programming of C and assembly language

This paper mainly describes the mixed programming of C and assembly language. Assembly language refers to arm assembly language. This paper is used to help understand the assembler in ARM embedded system and how to mix C language with assembler.
Main software: keiL μ Vision5

1, 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 "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 transplanted directly between different platforms. Assembly language mainly includes transmission instruction, logic operation, shift instruction, bit operation, control transfer, string operation, input and output and so on.

2, C language calls assembly language

1. No parameter call

Creating project files in Keil has been introduced in my previous blog. I won't introduce it too much here.

Blog: Keil environment configuration and simulation debugging of stm32 program_ Jiangnan smoke, pus and rain blog - CSDN blog

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

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


Add Fun.s and main.c files successively

Func.s

	AREA My_Function,CODE,READONLY		;There must be something in this line except My_Function You can choose your own name. All the others are templates
	EXPORT Init_1		;With in C Defined in the file Init_1 Function Association
		
;The declaration and use of variables in high-level languages are actually the use of registers, so we only need to use registers directly

Init_1
	
	MOV R1,#0 		; Let R1 register be i
	MOV R2,#0 		; Let R2 register be j
	
LOOP		;Written on the far left is the segment name of the program segment, which is used when executing the jump program
	CMP R1,#ten 		; Compare the sizes of R1 and 10
	BHS LOOP_END	;If R1 Greater than or equal to 10, jump to LOOP_END On the contrary, ignore the statement and directly execute the following statement
	ADD R2,#1		;j++
	ADD R1,#1		;i++
	B LOOP			;After executing a cycle, unconditionally enter the cycle judgment again, that is, jump to LOOP paragraph
LOOP END
	NOP
	
	END				;Write after the required space END,Otherwise, it will be regarded as the segment name, indicating the end of the program

main.c

#include<stdio.h>
 extern void Init_1(void);
 
 int main()
{
	 Init_1();
	 return 0;	 
}

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

Set breakpoint


You can observe the program operation through register changes

2. Parameter call

Simply modify the code

Func.s

	AREA My_Function,CODE,READONLY		;There must be something in this line except My_Function You can choose your own name. All the others are templates
	EXPORT Init_1		;With in C Defined in the file Init_1 Function Association
		
;The declaration and use of variables in high-level languages are actually the use of registers, so we only need to use registers directly

Init_1
	
	ADD R0,#one hundred 		; Add the passed in value + 100
	MOV PC,LR		;return R0
	
LOOP		;Written on the far left is the segment name of the program segment, which is used when executing the jump program
	CMP R1,#ten 		; Compare the sizes of R1 and 10
	BHS LOOP_END	;If R1 Greater than or equal to 10, jump to LOOP_END On the contrary, ignore the statement and directly execute the following statement
	ADD R2,#1		;j++
	ADD R1,#1		;i++
	B LOOP			;After executing a cycle, unconditionally enter the cycle judgment again, that is, jump to LOOP paragraph
LOOP_END
	NOP
	
	END				;Write after the required space END,Otherwise, it will be regarded as the segment name, indicating the end of the program

main.c

#include<stdio.h>
extern int Init_1(int x);
 
 int main()
{
	 printf("%d",Init_1(420));
	 return 0;
}

The debugger observes register changes

At this time, the value of R1 register is 0x208, i.e. 520, and the call is successful.

3, Assembly language calls C language

Modify code

Func.s

	AREA My_Function,CODE,READONLY		;There must be something in this line except My_Function You can choose your own name. All the others are templates
	EXPORT Init_1		;With in C Defined in the file Init_1 Function Association
	IMPORT 	return1		;statement return1 Is an external reference
;The declaration and use of variables in high-level languages are actually the use of registers, so we only need to use registers directly

Init_1
	
	ADD R0,#one hundred 		; Add the passed in value + 100
	MOV PC,LR		;return R0
	
LOOP		;Written on the far left is the segment name of the program segment, which is used when executing the jump program
	CMP R1,#ten 		; Compare the sizes of R1 and 10
	BHS LOOP_END	;If R1 Greater than or equal to 10, jump to LOOP_END On the contrary, ignore the statement and directly execute the following statement
	ADD R2,#1		;j++
	ADD R1,#1		;i++
	B LOOP			;After executing a cycle, unconditionally enter the cycle judgment again, that is, jump to LOOP paragraph
LOOP_END
	NOP
	
	END				;Write after the required space END,Otherwise, it will be regarded as the segment name, indicating the end of the program

main.c

#include<stdio.h>
extern int Init_1(int x);
int return1(void);
int main()
{
	 int x = Init_1(420);
	 printf("%d",x);
	 return 0;
}

int return1(void)
{
	return 1;
}

Compile and debug

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

4, Summary

This experiment needs to learn the relevant knowledge of C language and assembly language, as well as how to call the parameters of C language and assembly language, so as to lay a certain foundation for embedded in the future.

5, References

Assembly language (machine oriented programming language)_ Baidu Encyclopedia (baidu.com)

Summary of commonly used arm assembly instructions_ zb861359 blog - CSDN blog_ arm assembly instruction

Tags: C stm32 ARM

Posted on Wed, 13 Oct 2021 12:03:38 -0400 by Zanus