An example of how to call an assembly function in KEIL C51 (v6.21)

Publisher:柳絮轻风Latest update time:2016-11-22 Source: eefocusKeywords:KEIL Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:KEIL Reference address:An example of how to call an assembly function in KEIL C51 (v6.21)

Previous article:uC/OS-II transplantation steps on C51
Next article:RS232 serial port driver (C51)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号