如何从 C 程序中调用汇编函数? [英] How do you call an assembly function from C program?

查看:27
本文介绍了如何从 C 程序中调用汇编函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am very new to Microcontroller programming particularly PIC18F87J11, and I am using MPLAB C18 compiler. I was reading the datasheet for various topics such as saving to memory or working with timers. I noticed the examples are written in assembly language, but I have minimum experience with it. Is there a way to use the power of Assembly and C together in my project?

I do not understand assembly that good or else I would try to convert the code to C, in this case, I rather just use the assembly code with few changes.

Now can someone show me an example of how you would use an assembly function in a C program with MPLAB C18 compiler?

The assembly function can be as simple as hello world. I just need to understand the process of combing and using two different languages on MPLAB. I tried to create a quick function for testing, but I did not know where to put the assembly code. Do assembly codes need their own files included in the project?

I hope someone can enlighten me with a full simple example just to be able to see the big picture. I appropriate the help in advance, thanks!

解决方案

Without access to the compiler I can't test this, but the MPLAB C18 C Compiler User's Guide and Release Notes which I found here states:

2.8.2 Inline Assembly

MPLAB C18 provides an internal assembler using a syntax similar to the MPASM assembler. The block of assembly code must begin with _asm and end with _endasm. The syntax within the block is:

[label:] [<instruction> [arg1[, arg2[, arg3]]]]

...

So, it would seem to me that one solution might be to embed something like this in your .C file

void myAsmFunction()
{
  _asm
  // The asm code you've been looking at
  _endasm
}

Then call myAsmFunction from wherever appropriate. If you're wanting to include parameters and return values then it gets a little trickier.

这篇关于如何从 C 程序中调用汇编函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆