About embedding assemblies in KEIL C51 and mutual calls between C51 and A51
author [Ycong_kuang]©[youth]©
An example of how to call a assembler function in KEIL C51 (v6.21) [Ycong_kuang] The method for C51 call assembly has been a lot of posts, but generally only the main points, very few of the whole process is described in detail, for beginners is not enough, here the author through a simple example of This process is described, hoping to be helpful to beginners.
Over the past few years, in this forum I get a lot of enthusiastic people guidance, so also hope to do a little humble. In this example, a method for writing a C51 program call assembly function is described, and the entry parameter of this external function is a character variable and a bitwise variable, and the return value is an integer variable. example, first write the body of this function with C51, then compile the ASM file with src control instruction, and further modify the 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 compiling the compilation program. Step1.
According to write ordinary C51 program method, set up the project, inside import main.c file and cfunc.c file. The relevant files are as follows://main.c file #include < reg51.h > #define UCHAR unsigned char #define UINT unsigned int extern uint Afunc (UC
Har 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 TM
P_VCHR;
UINT Tp_vint;
TMP_VCHR=V_ACHR;
tp_vint= (UINT) V_bflag;
Return tmp_vchr+ (TP_VINT<<8); } step2. Right-click on the C file that contains the assembly code in the Project window, select "Options for ..." and tap on the right "Generate AssembleR src File "and" Assemble src file "Make the check box from gray to black (valid) state; Step3. Depending on the selected compilation mode, the corresponding library file (such as Small mode, is keil\c51\lib\c51s. LIB) into the project, the document must be the final document of the project; Step4. Build this project will produce a cfunc.src file, rename the file to Cfunc.a51 (you can also generate cfunc.a51 files directly through the compile option), and then remove the library files (such as C51S.Lib) and CFUNC.C in the project,
and add Cfunc.a51 to the project. Cfunc. The 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 # Ten;----Variable ' tmp_vchr?042 ' assigned to Register ' R5 '----MOV R5, AR7;
tp_vint= (UINT) V_bflag; ; SOURCE Line # One MOV c,v_bflag?041 CLR a RLC A;----Variable ' tp_vint?043 ' assigned to Registe R ' r6/r7 '----;
Return tmp_vchr+ (TP_VINT<<8); ; SOURCE Line # 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 that the main.c "Generate assembler src file" and "Assemble src file" are valid, and if valid, click to make the check box invalid; Build this project again, you've got the body of the assembly function, modify the function inside
Assembly code to get the assembler function you need. References: 1. Xu Aijun, Peng Xiuhua. MCU advanced language C51windows environment programming and application, Electronic industry publishing house 2.www.c51bbs.com, C51 programming: About in KEIL C51 directly embedded in the assembly ...
Post ID: 83838 Posted by: Youth
................................................................................................................. Keil assembly function Call c51 function [Ycong_kuang] in the keil of the wording can refer to 89852 posts, as follows: Compared with 89852 posts, the first step in the project more than a compilation called C51 The function file (c51fuNC.C), as for the assembler function or the first to write the main body (A51FUNC.C), so that the assembler interface and segments are given to the compiler processing, you just have to be in the assembly code to rewrite the assembly code according to your requirements. The routines are 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)//c51 Write the assembler function, eventually to 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)
assembly function call C51 function {return v_int<<2;} The second step is to press the step2,3,4 of the 89852 post into the A51 file (assembly) function C51 (today I tried step3 can not) The result of the routine compilation 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: C51 The generated assembly code for the function written from here starts with the 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 #: MOV tmp_vchr?042,r7;
tp_vint= (UINT) V_bflag; ; SOURCE Line #: mov c,v_bflag?041 CLR a mov r6,a RLC a mov r7,a;----VARIAB Le ' tp_vint?043 ' assigned to Register ' r6/r7 '----; This explains R6,R7 content is tp_vint;
Return Cfunc (Tp_vint); ; SOURCE Line # Lcall _cfunc; This invokes a function written in c51; }
; SOURCE Line # 17? C0001:ret;
End of _afunc End this file is the file where your assembly function is located, and the assembly code inside the function is modified to the assembly function you need to be OK.
Recommended reference Xu Aijun, Peng Xiuhua wrote the "Microcontroller Advanced language c51windows Environment programming and application" or Zhongmei wrote "Microcontroller C language Application design" related to mixed language programming related chapters
................................................................................................................. About embedding the assembly directly in the KEIL C51 ... [Youth] Sometimes need to embed some assembly code in the C51 program, it is of course possible to use the usual practice: according to C51 and assembly interface to write a assembler function, and then call the function in the C51 program. (this method can be searched in the forum, a lot of posts before, no longer repeat) The following describes the method of embedding the assembly code directly: 1, in the C file to embed the assembly code in the following way to join the assembly code: #pragma ASM;
Assembler code here #pragma endasm 2, right-click on the C file that contains the assembly code in the Project window, select "Options for ..." and tap on the right "Generate assembler SRC file" and "Assemble SRC file", so that the check box from gray to black (valid) state; 3, according to the selected compilation mode, the corresponding library files (such as Small mode, is keil\c51\lib\c51s. LIB) into the project, the file must be the final document of the project; 4. Compile to generate the target code.