There are many posts about how to call assembly in C51, but they usually only talk about the key points, and rarely describe the whole process in detail, which is not enough for beginners. Here, I will describe the process through a simple example, hoping to help beginners. Over the past few years, I have received guidance from many enthusiastic people in this forum, so I also hope to do my best.
In this example, a method of writing a C51 program to call an assembly function is described. The entry parameter of this external function is a character variable and a bit variable, and the return value is
An integer variable. In this example, first use c51 to write the body of this function, then use the SRC control instruction to compile and generate an asm file, and further modify this asm file to get the assembly function we want. This method allows the compiler to automatically complete the arrangement of various segments, improving the efficiency of writing assembly programs.
Step 1. Create a project according to the method of writing ordinary C51 programs, and import the main.c file and CFUNC.c file into it.
The relevant documents are as follows:
//main.c file
#include < reg51.h >
#define uchar unsigned char
#define uint unsigned int
extern uint AFUNC(uchar v_achr,bit v_bflag);
void main()
{
bit BFLAG;
uchar mav_chr;
uint mvintrslt;
mav_chr=0xd4; BFLAG=1;
mvintrslt=AFUNC(mav_chr,BFLAG);
}
//CFUNC.c file
#define uchar unsigned char
#define uint unsigned int
uint AFUNC(uchar v_achr,bit v_bflag)
{
uchar tmp_vchr;
uint tp_vint;
tmp_vchr=v_achr;
tp_vint = (uint)v_bflag;
return tmp_vchr+(tp_vint<<8);
}
step2. In the Project window, right-click the C file that contains the assembly code, select "Options for...", and click "Generate Assembler SRC
File" and "Assemble SRC File", change the check box from gray to black (valid);
step3. According to the selected compilation mode, add the corresponding library file (such as Keil\C51\Lib\C51S.Lib in Small mode) to the project. This file must be the last file of the project.
Step 4. After building this project, a CFUNC.SRC file will be generated. Rename this file to CFUNC.A51 (you can also generate CFUNC.A51 file directly through the compilation option).
Then remove the library files (such as C51S.Lib) and CFUNC.c in the project, and add CFUNC.A51 to the project.
//CFUNC.SRC file is as follows
.\CFUNC.SRC generated from: CFUNC.c
NAME CFUNC
?PR?_AFUNC?CFUNC SEGMENT CODE
?BI?_AFUNC?CFUNC SEGMENT BIT OVERLAYABLE
PUBLIC?_AFUNC?BIT
PUBLIC _AFUNC
RSEG ?BI?_AFUNC?CFUNC
?_AFUNC?BIT:
v_bflag?041: DBIT 1
; #define uchar unsigned char
; #define uint unsigned int
;
; uint AFUNC(uchar v_achr,bit v_bflag)
RSEG ?PR?_AFUNC?CFUNC
_AFUNC:
USING 0
; SOURCE LINE # 5
;---- Variable 'v_achr?040' assigned to Register 'R7' ----
; {
; SOURCE LINE # 6
; uchar tmp_vchr;
; uint tp_vint;
;
; tmp_vchr=v_achr;
; SOURCE LINE # 10
;---- Variable 'tmp_vchr?042' assigned to Register 'R5' ----
MOV R5,AR7
; tp_vint=(uint)v_bflag;
; SOURCE LINE # 11
MOV C,v_bflag?041
CLR A
RLC A
;---- Variable 'tp_vint?043' assigned to Register 'R6/R7' ----
; return tmp_vchr+(tp_vint<<8);
; SOURCE LINE # 12
MOV R6,A
MOV R4,#00H
CLR A
ADD A,R5
MOV R7,A
MOV A,R4
ADDC A,R6
MOV R6,A
; }
; SOURCE LINE # 13
?C0001:
RET
; END OF _AFUNC
END
step5. Check whether "Generate Assembler SRC File" and "Assemble SRC File" of main.c are valid. If they are valid, click to disable the check box; build the project again. Now you have obtained the main body of the assembly function. Modify the assembly code in the function to get the assembly function you need.
references:
1. Xu Aijun, Peng Xiuhua. Single-chip high-level language C51windows environment programming and application, Electronic Industry Press
2. www.c51bbs.com, C51 Programming: About embedding assembly directly in KEIL C51. . . Post number: 83838 Posted by: Youth
................................................................. ................................................................. .............
Assembly function calls C51 function in Keil [ycong_kuang]
For the writing method in Keil, please refer to post 89852, as follows:
Compared with post 89852, the first step is to add a c51 function file (c51func.c) called by the assembly in the project. As for the assembly function, you should first use c51 to write the main body (a51func.c). In this way, the assembly program interface and segments are handled by the compiler. You only need to rewrite the assembly code according to your requirements after compiling it into assembly code.
The routine is as follows:
//main.c
#include < reg51.h >
#define uchar unsigned char
#define uint unsigned int
extern uint AFUNC(uchar v_achr,bit v_bflag);
void main()
{
bit BFLAG;
uchar mav_chr;
uint mvintrslt;
mav_chr=0xd4; BFLAG=1;
mvintrslt=AFUNC(mav_chr,BFLAG);
}
//a51FUNC.c
#define uchar unsigned char
#define uint unsigned int
extern uint CFUNC(uint);
uint AFUNC(uchar v_achr,bit v_bflag) //Assembly function written in c51, which will eventually become assembly code
{
uchar tmp_vchr;
uint tp_vint;
tmp_vchr=v_achr;
tp_vint = (uint)v_bflag;
return CFUNC(tp_vint); //Call a c51 function here
}
//c51FUNC.c
#define uchar unsigned char
#define uint unsigned int
uint CFUNC(uint v_int) //Calling c51 function by assembly function
{
return v_int<<2;
}
The second step is to convert the (assembler) function written in c51 into a51 file according to steps 2, 3, and 4 of post 89852 (I tried step 3 today and it works). The compilation result of the routine is as follows:
; .\a51func.SRC generated from: a51func.c
NAME A51FUNC
?PR?_AFUNC?A51FUNC SEGMENT CODE
?DT?_AFUNC?A51FUNC SEGMENT DATA OVERLAYABLE
?BI?_AFUNC?A51FUNC SEGMENT BIT OVERLAYABLE
EXTRN CODE (_CFUNC)
PUBLIC?_AFUNC?BIT
PUBLIC _AFUNC
RSEG ?DT?_AFUNC?A51FUNC
?_AFUNC?BYTE:
tmp_vchr?042: DS 1
RSEG ?BI?_AFUNC?A51FUNC
?_AFUNC?BIT:
v_bflag?041: DBIT 1
; //a51FUNC.c
;
; #define uchar unsigned char
; #define uint unsigned int
;
; extern uint CFUNC(uint);
;
; uint AFUNC(uchar v_achr,bit v_bflag)
RSEG ?PR?_AFUNC?A51FUNC
_AFUNC: ;The assembly code generated by the function written in c51 starts here
USING 0
; SOURCE LINE # 8
;---- Variable 'v_achr?040' assigned to Register 'R7' ----
; {
; SOURCE LINE # 9
; uchar tmp_vchr;
; uint tp_vint;
;
; tmp_vchr=v_achr;
; SOURCE LINE # 13
MOV tmp_vchr?042,R7
; tp_vint=(uint)v_bflag;
; SOURCE LINE # 14
MOV C,v_bflag?041
CLR A
MOV R6,A
RLC A
MOV R7,A
;---- Variable 'tp_vint?043' assigned to Register 'R6/R7' ----
; Here, the content of R6 and R7 is tp_vint
; return CFUNC(tp_vint);
; SOURCE LINE # 16
LCALL _CFUNC ; This is where the function written in c51 is called
; }
; SOURCE LINE # 17
?C0001:
RET
; END OF _AFUNC
END
This file is where your assembly function is located. Just modify the assembly code in the function into the assembly function you need.
It is recommended to refer to " Microcontroller Advanced Language C51windows Environment Programming and Application" written by Xu Aijun and Peng Xiuhua or "Microcontroller Advanced Language C51windows Environment Programming and Application" written by Ma Zhongmei.
Chapters on mixed language programming in " C language application design for single chip microcomputers "
................................................................. ................................................................. .............
About embedding assembly directly in KEIL C51. . . [Youth]
Sometimes you need to embed some assembly code in a C51 program. In this case, you can use the usual approach:
Write an assembly function according to the interface between C51 and assembly, and then call the function in the C51 program. (This method can be searched in the forum. There are many posts about it before, so I won’t repeat it here.)
Here is how to embed assembly code directly:
1. To embed the assembly code snippet in the C file, add the assembly code in the following way:
#pragma ASM
; Assembler Code Here
#pragma ENDASM
2. In the Project window, right-click the C file containing the assembly code, select "Options for...", and click "Generate Assembler SRC File" on the right.
and "Assemble SRC File" to change the check box from gray to black (enabled);
3. According to the selected compilation mode, add the corresponding library file (such as Keil\C51\Lib\C51S.Lib in Small mode) to the project. This file must be the last file of the project.
4. Compile to generate the target code.
Previous article:uC/OS-II transplantation steps on C51
Next article:RS232 serial port driver (C51)
- Popular Resources
- Popular amplifiers
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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Classic time series
- STM32 built-in DAC output voltage amplified to drive piezoelectric ceramics
- TI - Delivering Ultra-High Power Density for 100W USB Power Delivery Adapters
- What challenges does the epidemic bring to engineers?
- Seeking routines for driving LPS33HW pressure sensor of STM32F103 series
- Just to tell you a joke, I spent the whole afternoon trying to write a for loop properly.
- Three solutions for implementing motor control design, which one is better?
- 【GD32307E-START】+Serial communication function test
- [HPM-DIY] HPM6750 openmv transplantation is successful, using cherryusb as repl interaction
- The software engineer said he doesn't know how to use GPIO to simulate I2C. Is he a newbie?