4944 views|1 replies

1379

Posts

0

Resources
The OP
 

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

There are many posts about the method of calling 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 the process through a simple example, hoping to help beginners. In 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 parameter of this external function is 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.

Step 1. 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 that contains the assembly code and select "Options for ...". Click "Generate Assembler 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, 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 the CFUNC.A51 file directly through the compilation options
    ), 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 ;       ; 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 ; 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 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.

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 directly embedding assembly in KEIL C51. . . Post number: 83838 Posted by: Youth
 .................................................................................................................................
                 The assembly function in keil calls the c51 function [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, the main body
(a51func.c) is written in c51 first. In this way, the assembler program interface and segments are handled by the compiler, and 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) //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) //Assembler function calls c51 function
{
  return v_int<<2;
}

The second step is to convert the (assembly) function written in c51 into an a51 file according to steps 2, 3, and 4 of post 89852 (I tried it today and step 3 is not necessary). 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 it is explained that the contents of R6 and R7 are 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 where your assembly function is located. Modify the assembly code in the function to what you want. The required assembly function is ok. It is recommended to refer to the chapters on mixed language programming in "Microcontroller Advanced Language C51windows Environment Programming and Application" written by Xu Aijun and Peng Xiuhua or "Microcontroller C Language Application Design"

written by Ma Zhongmei  .... .................................................. .................................................. .........                 About embedding assembly code 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 method: write the C51 and assembly interface An assembly function, and then call the function in the C51 program. (This method can be searched in the forum. There have been 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 snippets 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 enable the check. The box changes 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 used as a project The final file; 4. Compile to generate the target code.





















This post is from 51mcu

Latest reply

Hahaha very good   Details Published on 2010-2-1 13:02
 

117

Posts

0

Resources
2
 
Hahaha very good
This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list