About embedding assembly in KEIL C51 and calling between C51 and A51

Publisher:灵感火花Latest update time:2012-06-29 Source: 21ic Keywords:KEIL  C51  Assembly Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.





















Keywords:KEIL  C51  Assembly Reference address:About embedding assembly in KEIL C51 and calling between C51 and A51

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

Digital tube display electronic clock C51 program
/************************************************************* Function:     Realize electronic clock display (C) definition:     P3 port position selection, P1 port segment selection, P0.0, P0.1, P0.2 are timing ports time:     2010—11—26 illustrate:     There is a problem with the simulation. This may be a problem
[Microcontroller]
Common Errors of Keil C51 and Keil RealView MDK
1. Installation sequence:    You must install Keil C51 first and then install RVMDK, otherwise there will be a series of compilation problems. 2. Common Keil error: last line of file ends without a newline Causes and solutions    When compiling with Keil, a warning message like this pops up: main.c(7): warning:  #1-D:
[Microcontroller]
C51 Timer Calculation Tool
◆Test code: #include reg52.h void main(void) {  TMOD = 0x01; //Timer T0 working mode 1 16-bit timing  TH0=0x3c;  //50ms 65536-50000us  TL0=0xb0;  ET0 = 1; //Enable timer T0 interrupt  EA = 1; //Enable general interrupt Breakpoint: TR0 = 1; //Start the timer, you can place it anywhere^_^ TMOD←→while(1)  while(1); //In
[Microcontroller]
How to call proteus in keil to simulate MCU peripheral devices
How to call proteus in keil to simulate MCU peripheral devices!                Proteus versions before 6.9   1. Install keil c51 and proteus   2. Copy the VDM51.dll file in the MODELS folder in the proteus installation directory to the C51BIN    directory in the Keil installation directory.   3. Modify the Tools.i
[Microcontroller]
How to call proteus in keil to simulate MCU peripheral devices
Solution to using printf function in Keil MDK environment
The printf() function can directly format the output to the window, which greatly facilitates our debugging of the program. However, in the Keil MDK environment, directly using the printf() function will fall into a software interrupt, resulting in an infinite loop; therefore, the main() function cannot be entered. Wh
[Microcontroller]
Function pointers and reentrant functions in Keil C51
Overview Function pointers are one of the few difficult parts of C. Due to the unique requirements of the 8051 C compiler, function pointers and reentrant functions have more challenges to overcome, mainly due to the passing of function variables. Typically (on most 8051 chips) function variables are passed via stac
[Microcontroller]
The use of interrupt and using in C51 interrupt
The format of C51 interrupt function is: void FuncIr(void) interrupt x Both interrupt and using are keywords of C51. The C51 interrupt process is implemented by using the interrupt keyword and the interrupt number (0 to 3). The interrupt number indicates the entry address of the interrupt program. With this declarati
[Microcontroller]
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号