An example of how to call an assembly function in KEIL C51 (v6.21) [ycong_kuang]
There are many posts about how to call assembly from C51, but generally only the key points are mentioned, and few of them describe the whole process in detail, which is not enough for beginners. Here, the author
describes this process through a simple example, hoping to help beginners. Over the past few years, the author has 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 explained. The entry parameters of this external function are a character variable and a bit variable, and the return value is
an integer variable. In the example, first use C51 to write the body of this function, and then use the SRC control instruction to compile and generate an asm file. 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, which improves the efficiency of writing assembly programs.
step1. According to the method of writing ordinary C51 programs, create a project and import the main.c file and CFUNC.c file into it.
The relevant files 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 on the C file containing the assembly code, select "Options for ...", click "Generate Assembler SRC
File" and "Assemble SRC File" on the right, and 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;
step4. 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 file (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
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
; g
?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 make the check box invalid
. 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. [page]
References:
1. Xu Aijun, Peng Xiuhua. Single-chip high-level language C51windows environment programming and application, Electronic
Industry
Press
.................
...
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) //The assembly function written in c51 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) //Call c51 function by assembly function
{
return v_int<<2;
}
The second step is to convert the (assembly) function written in c51 into a51 file according to step 2, 3, and 4 of post 89852 (I tried it today and step 3 is not needed). The routine compilation result 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 by 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\' ----
; This indicates that the content of R6, R7 is tp_vint
; return CFUNC(tp_vint);
; SOURCE LINE # 16
LCALL _CFUNC ; Here the function written in c51 is called
; }
; SOURCE LINE # 17
?C0001:
RET
; END OF _AFUNC
END
This file is the file where your assembly function is located. Just modify the assembly code in the function to the assembly function you need.
It is recommended to refer to
the chapters on mixed language programming
in
"Programming and Application of Single-Chip Microcomputer Advanced Language C51windows Environment" written by Xu Aijun and Peng Xiuhua or "C Language Application Program Design for Single-Chip Microcomputer" written by Ma
Zhongmei
.................
...
(This method can be searched in the forum. There are many posts about it before, so I won’t
repeat it here.)
Here is a method to embed assembly code directly:
1. To embed assembly code in a C file, add the assembly code as follows:
#pragma ASM
; Assembler Code Here
#pragma ENDASM
2. 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" on the right to change the check box from gray to black (valid);
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:System-on-chip design and static timing analysis
Next article:A simple and practical design scheme for angle measuring encoder
Recommended ReadingLatest update time:2024-11-16 18:00
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How to make the buzzer sound when pressing K1 and the indicator light on when pressing K2D1
- AM437x Processor PoM to Create Industrial Gateway Based on IoT (IoT-SDK)
- Coordinate transformation in motor FOC (CLARK+PARK+formula derivation)
- Which one is better, 3-axis, 6-axis or 9-axis?
- Zhengdian Atom Elite STM32F103ZET6 Development Board Kit
- Fresh and hot evaluation information is here~~
- Please recommend a chip that can realize a 0.01hz function signal generator
- Does using a high switching frequency converter affect efficiency at higher loads?
- The use of three brackets in C language in keil: (); []; {}
- Share CCS7.3 installation steps