简体   繁体   English

从Keil IDE中的C源代码调用汇编例程

[英]calling assembly routines from c source code in keil ide

I am having delay routines for 8051 micro-controller in assembly language.Ana I can use them in assembly language programs but I want to use these routines from c language as these are generate perfect delay for me. 我在汇编语言中有8051微控制器的延迟例程。我可以在汇编语言程序中使用它们,但是我想从c语言使用这些例程,因为它们对我来说是完美的延迟。

The code for delay that is delay.asm file I post just beleow... 我发布的延迟代码是delay.asm文件...

;ALL DELAYS ROUTINES HERE
DELAY_SEG SEGMENT CODE
RSEG DELAY_SEG
;DELAY OF 1MS SUBROUTINE
DELAY1MS:
     MOV R7,#250
     DJNZ R7,$
     MOV R7,#247
     DJNZ R7,$
     RET
;DELAY OF 100MS SUBROUTINE
DELAY100MS:
     MOV R6,#99;1MUS
L1:
     ACALL DELAY1MS ;99MS
     DJNZ R6,L1;198MUS
     MOV R6,#250;1MUS
     DJNZ R6,$;500US
     MOV R6,#147;1US
     DJNZ R6,$;294US
     NOP
     RET;1US
;DELAY 0F 1SEC SUB ROUTINE
DELAY1S:
     MOV R5,#9
L2:
     ACALL DELAY100MS
     DJNZ R5,L2
     MOV R5,#99
L3:
     ACALL DELAY1MS
     DJNZ R5,L3
     MOV R5,#250
     DJNZ R5,$
     MOV R5,#138
     DJNZ R5,$
     RET

I include this code in assembly language and use simply.But I want to call these routines from c source code. 我将这些代码包含在汇编语言中并简单地使用。但是我想从c源代码中调用这些例程。

I don't know about how the interface between your assembler and your C compiler works, but generally you have to tell the assembler to export functions (there should be a directive for that, look at the assembler manual). 我不知道汇编器和C编译器之间的接口如何工作,但是通常您必须告诉汇编器导出函数(对此应该有一个指令,请参阅汇编器手册)。 Normally the functions in assembler need to have an underscore before the name, like _DELAY1S . 通常,汇编器中的函数名称前必须带有下划线,例如_DELAY1S Then you need to create an extern declaration in your source code referencing the function, like 然后,您需要在源代码中创建引用该函数的extern声明,例如

extern void DELAY1S(void);

It may be different for your tools, read the documentation . 您的工具可能有所不同,请阅读文档

To properly interface your assembler functions you need to do the following steps: 要正确连接汇编程序功能,您需要执行以下步骤:

  • give your module the same NAME as it's file name without extension (I assume that your source file has name delays.a51 ): 为您的模块提供与文件名相同的名称(不带扩展名)(我假设您的源文件具有名称delays.a51 ):
 MODULE DELAYS 
  • prepend each function name that you want to be visible in C modules with underscore. 在每个要在C模块中显示的函数名称前加下划线。

  • for each function you need to declare a separate code segment with 对于每个函数,您需要声明一个单独的代码段
    the following naming convention: 以下命名约定:

?PR?FunctionName?ModuleName ?PR?FunctionName?模块名

  • put each function into it's own segment. 将每个函数放入自己的段中。
  • also each function name should be made PUBLIC 每个函数的名称也应该设为PUBLIC

Thus, for your DELAY1MS function you have the following: 因此,对于DELAY1MS函数,您需要:

?PR?_DELAY1MS?DELAYS    SEGMENT CODE
    RSEG        ?PR?_DELAY1MS?DELAYS
    PUBLIC      _DELAY1MS
_DELAY1MS:
    ...
    ...
    RET

To make functions available to the C compiler you should declare them without any mangling (no underscore), so for DELAY1MS you have: 为了使函数可用于C编译器,应声明它们时不加任何修饰(不带下划线),因此对于DELAY1MS,您应具有:

void Delay1ms(void);

All this tricky games with names & segments are required for the linker as it builds the graph of calls to allocate memory statically for local variables at link time. 链接器需要使用所有带有名称和段的棘手游戏,因为它会建立调用图以在链接时为本地变量静态分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM