C and assembly language mixed programming of STM32 under Keil

Keywords: C stm32

1, Create project

1. Create new projects
2. Select STM32F103C8

3. Check device startup, otherwise an error may be reported

Click Ok.

2, Programming

Right click Source Group1 and add the main.c and func.s files.


The code is as follows:

main.c

# include<stdio.h>

extern void	Init_1(void);

int main(){
	
	Init_1();
	
	return 0;
}

Func.s

	AREA	My_Function,CODE,READONLY	;There must be something in this line except My_ Function You can name it yourself. Everything else is a template

	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 board 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	;On the far left is the segment name of the program segment,To execute the jump procedure

	CMP R1,#10 ; 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, which is to jump to Loop paragraph

LOOP_ END	;Written on the far left is the segment name of the program segment, which is used when executing the jump program

	NOP

	END	;You must write after a space END,Otherwise, it will be regarded as the segment name, indicating the end of the program

Select the magic wand, select Debug, check Use Simulator, change the content of Dialog DLL in the lower left corner to DARMSTM.DLL, and change the content of Parameter to = = - pSTM32F103C8==

3, Compile and debug

1. rebuild has no errors
2. Set breakpoint
Compilation and debugging

R1 is less than 10 and will continue to enter the cycle


If R1 is greater than 10, the cycle will jump out

4, Modify code

main1.c

#include<stdio.h>
extern int Init_1(int x);
int main()

	{

	 int x,y;

		x=10;

	y=Init_1(x);
return 0;

	}

Func.s

	AREA	My_Function,CODE,READONLY	;There must be something in this line except My_ Function You can name it yourself. Everything else is a template

	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 board registers, so we only need to use registers directly

Init_1

loop ;On the far left is the segment name of the program segment,To execute the jump procedure

	MOV R2,100	;set up R2 The value of the register is 100

	ADD R1,R0,R2;calculation R0 And R2 And, and store the value in R1

LOOP_END ;On the far left is the segment name of the program segment,To execute the jump procedure

	MOV  PC,LR; Return value, which is the necessary format

	END ;You must write after a space END,Otherwise, it will be regarded as the segment name, indicating the end of the program

Compile and debug

Start compilation:

5, Problems and Solutions

The following error occurred when compiling with Keil vision5

error: #5: cannot open source input file "core_cm3.h": No such file or directory

This is because the MDK version you installed is too new, such as MDK5.12/5.13. They will not find the header file from the MDK installation directory. So it leads to this error.
The solution is as follows:
Search the corresponding files according to the prompts and copy them to the include directory of keil software installation:

D:\MDK\install\ARM\PACK\Keil\STM32F1xx_DFP\2.2.0\Device\Include

It is recommended to open the location where the file exists and copy all. h files to the include directory.

6, Summary

Introduction to Arm instruction set

  1. R0-r3 is used as the parameter of the incoming function and the return value of the outgoing function. R0-r3 can be used for any purpose between subroutine calls. The called function does not have to recover r0-r3 before returning If the calling function needs to use the contents of r0-r3 again, it must keep them.
  2. r4-r11 is used to store the local variables of the function. If the called function uses these registers, it must restore the values of these registers before returning. R11 is the stack frame pointer fp.
  3. r12 is the internal call temporary register ip. It is used for this role in process link glue code (for example, interaction glue code). It can be used for any purpose between procedure calls. The called function does not have to recover r12 before returning.
  4. Register r13 is the stack pointer sp. It cannot be used for any other purpose. The value stored in SP must be the same when exiting the called function as when entering.
  5. Register r14 is the link register lr. If you save the return address, you can use r14 for other purposes between calls and restore it when the program returns
  6. Register r15 is the program counter pc. It cannot be used for any other purpose.

Through this experiment, I learned about the mixed use of assembly language and C language, learned the mutual call between C language and assembly language, and understood some methods of debugging. Some of the problems encountered still need to be solved.

7, Reference

error: #5 solution
arm programming, about the corresponding relationship of function call formal parameters and arguments in general registers and stack frames

Posted by czs on Tue, 12 Oct 2021 17:48:08 -0700