Embedded Assembly in Keil C51 and mutual call between C51 and A51

Source: Internet
Author: User

Embedded Assembly in Keil C51 and mutual call between C51 and A51

Author [ycong_kuang] [youth]

 
There have been many posts on how to call an assembly function in Keil C51 (v6.21) [ycong_kuang] for the methods of calling the Assembly by the C51, but generally only focus on the points, I rarely describe the entire process in detail, but it 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 many enthusiastic guidance from this forum, so I hope to do my best. In this example, we explain how to compile Program A Method for calling an assembly function. The entry parameter of this external function is a struct variable and a single-bit variable, and the return value is an integer variable. In this example, the main body of the function is first written in the C51, and then compiled and generated the ASM file using the SRC control command. Then, the ASM file is further modified to obtain the Assembly function we want. This method enables the compiler to automatically complete the arrangement of various segments, improving the compilation efficiency of assembler programs. Step 1. Follow the common methods of writing the C51 Program to create a project and import the main. c file and the cfunc. c file. The related files are as follows: // Main. c file # Include <reg51.h> # define uchar unsigned char # define uint unsigned intextern uint afunc (uchar v_achr, bit v_bflag); void main () {bit bflag; uchar listener; uint mvintrslt; mav_chr = 0xd4; bflag = 1; mvintrslt = afunc (mav_chr, bflag );} // Cfunc. c file # Define uchar unsigned char # define uint unsigned intuint afunc (uchar v_achr, bit v_bflag) {uchar random; uint tp_vint; random = v_achr; tp_vint = (uint) v_bflag; return tmp_vchr + (tp_vint <8);} step2. include the assembly in the project window Code Right-click the c file and select "Options... ", click" generate aggreger SRC file "and" Assemble SRC file "on the right to change the check box from Gray to Black (valid). step3. according to the Selected Compilation mode, set the corresponding library file (for example, in small mode, it is Keil \ C51 \ Lib \ c51s. lib) in the project, the file must be used as the final file of the job; step4. after the project is built, a cfunc will be generated. SRC file, rename this file to cfunc. a51 (you can also use the compilation option to directly generate cfunc. a51 files), and then remove the library files (such as c51s. lib) and cfunc. c, and the cfunc. add A51 to the project.// The 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 ; ---- Variable 'tp _ Vint? 043 'assigned to register 'r6/r7' ----; return tmp_vchr + (tp_vint <8); source line #12 MoV R6, a mov R4, #00 h clr a Add a, R5 mov R7, a mov A, R4 addc a, R6 mov R6, ;}; Source line #13 ? C0001: Ret ; End of _ afunc Endstep5. Check main. whether "generate extends er SRC file" and "Assemble SRC file" of C are valid. If they are valid, click "make check box invalid". Build the project again, now you have obtained the main body of the assembler function. You can modify the assembly code in the function to obtain the required assembler function. References: 1. Xu Aijun and Peng xiuhua. MCU advanced language c51windows environment programming and application, Electronics Industry Press 2.www.c51bbs.com, C51 programming: On the Keil C51 directly embedded assembly... Post No.: 83838 posting users: youth ....................................... ........................................ .................................. for details about how to compile a function in Keil to call the C51 Function [ycong_kuang] in Keil, refer to post 89852. The details are as follows: Compared with post 89852, the first step in the project is to add a program called by the compilation of the C51 Function file (c51func. c) As for the compilation function, we should first compile the subject (a51func. c) in this way, the assembler interface and segment are handed over to the compiler for processing. You just need to compile the compiled code and rewrite the assembly code as required. The routine is as follows: // Main. c # Include <reg51.h> # define uchar unsigned char # define uint unsigned intextern uint afunc (uchar v_achr, bit v_bflag); void main () {bit bflag; uchar listener; uint mvintrslt; mav_chr = 0xd4; bflag = 1; mvintrslt = afunc (mav_chr, bflag );} // A51func. c # Define uchar unsigned char # define uint unsigned intextern uint cfunc (uint); uint afunc (uchar v_achr, bit v_bflag) // The compile function written by C51 will eventually become the Assemble Code {Uchar tmp_vchr; uint tp_vint; tmp_vchr = v_achr; tp_vint = (uint) v_bflag; return cfunc (tp_vint ); // Call a C51 Function } // C51func. c # Define uchar unsigned char # define uint unsigned intuint cfunc (uint v_int) // The compiled function calls the C51 function. {Return v_int <2;} the second step is to press step 2, step 2, step 3 of the post 89852, and convert the (assembly) function written with the C51 into the A51 file (today I tried step 3, but I can't) the compiling 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 compilation code generated by the functions written by the C51 starts from 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, ; ---- Variable 'tp _ Vint? 043 'assigned to register 'r6/r7' ---- Here R6 is described, R7 content is tp_vint; return cfunc (tp_vint); source line #16 Lcall _ cfunc ; Here, we call the function written with C51;}; source line #17 ? C0001: Ret ; End of _ afunc The end file is the file where your assembler function is located. You can modify the assembly code in the function to the Assembly function you need. It is recommended that you refer to Xu Aijun, Peng xiuhua's "MCU advanced language c51windows environment programming and application" or ma Zhongmei's "MCU's C Language Application Programming section .............................. ........................................ ........................................... embedded Assembly directly in Keil C51... [Youth] Sometimes some assembly code needs to be embedded in the C51 Program. In this case, you can use the common practice: Write an assembly function according to the interface between the C51 and the Assembly, then, call this function in the C51 Program. (This method can be searched in the Forum, which has been mentioned in many previous posts and will not be repeated.) The following describes how to directly embed assembly code: 1. to embed the assembly code in the C file, add the Assembly Code as follows: # pragma ASM; extends er code here # pragma endasm2, right-click the c file that contains assembly code in the project window, and select "Options... ", click" generate extends er 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, set the corresponding library file (for example, in small mode, it is Keil \ C51 \ Lib \ c51s. lib) in the project, the file must be used as the final file of the project; 4. Compile the file to generate the target code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.