从 C++ 访问 ASM 文件中的函数;程序?

发布于 2024-08-03 09:17:09 字数 627 浏览 4 评论 0原文

在这里我询问了有关将 ASM 文件转换为 C 的问题,并从从回应来看,似乎没有合理的方法可以做到这一点。美好的。因此,其中一个回复建议我按原样使用这些功能并完成它。听起来不错。

但如何呢?

天啊我已经尝试了我能想到的一切!我使用的是 Microchip 品牌的 MCU (PIC18F4480) 和 Microchip 品牌的 IDE (MPLAB IDE),并且 ASM 文件是 Microchip 编写的...所以您可能会认为我会找到一些利用它们的方法!到目前为止还没有运气。

我对ASM(汇编)一无所知。我用 C++ 编写代码,坦率地说,这不会改变。必须有一种方法可以访问 Microchip ASM 文件中的函数,而无需用新语言重写所有代码。

如果有人想看一下,ASM 文件和应用笔记为 这里

Over here I asked about translating an ASM file to C, and from the responses it looked like there was no reasonable way to do it. Fine. So one of the responses suggested I just make use of the functions as-is and be done with it. Sounds good.

But how?

Holy crap I've tried everything I can think of! I'm using a Microchip brand MCU (PIC18F4480) and a Microchip brand IDE (MPLAB IDE) and the ASM files are Microchip authored... so you'd think I'd find some way of making use of them! So far no luck at all.

I don't know anything about ASM (Assembly). I code in C++ and frankly that's not going to change. There has got to be a way to access the functions in the Microchip ASM files without rewriting all my code in a new language.

If anyone wants to take a look, the ASM files and application notes are here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒七 2024-08-10 09:17:09

查看 PIDInt.asm,从 C/C++ 调用函数应该相当简单。
首先,为 VARIABLE DEFINITIONS 中列出的所有内容声明 extern 变量:

extern unsigned char error0, error1; /* etc */

然后,为具有“Function:”注释的所有内容声明 extern 函数,不带参数且不返回结果:

extern void Proportional(); // extern "C" for C++

要调用其中之一,请填写输入变量,调用函数,并读取输出变量。

Looking at PIDInt.asm, it should be fairly straight-forward to call the functions from C/C++.
First, you declare extern variables for everything listed in VARIABLE DEFINITIONS:

extern unsigned char error0, error1; /* etc */

Then, you declare extern functions for all the things that have a "Function:" comment, taking no arguments and returning no result:

extern void Proportional(); // extern "C" for C++

To call one of them, you fill the input variables, call the function, and read the output variables.

怪异←思 2024-08-10 09:17:09

我希望您需要以某种方式从汇编代码构建一个对象 (.o) 文件,然后将您的 C++ 文件与该对象文件链接。您可能需要将这些函数声明为 extern "C" 才能从 C++ 调用它们。

不确定这里的细节,C++ 和汇编之间的调用约定可能存在问题,特别是对于 PIC 等微控制器。

您是否真的在微控制器上使用完整的 C++ 功能集,即对象、虚函数、重载等等?这……还不错,但我认为大多数 PIC 开发仍然是用纯 C 语言(当然还有汇编语言)。

I would expect you to need to somehow build an object (.o) file from the assembly code, and then link your C++ file with that object file. You are likely going to need to declare those functions as extern "C" to call them from C++.

Not sure about the details here, there might be issues with calling conventions between C++ and assembly, especially for a microcontroller such as the PIC.

Are you really using a full C++ feature set, i.e. objects, virtual functions, overloading, and so on, for a microcontroller? That's ... not bad, but I thought most PIC development was still in plain C (and assembly, of course).

思慕 2024-08-10 09:17:09

通常的方法是使用具有汇编器和 C/C++ 编译器的构建环境和工具。编译器(和汇编器)获取源文件并生成中间目标文件格式。在 Microsoft Windows 上,使用 Microsoft Tools,这些将是 .obj 文件。

然后使用链接器将 .obj 文件(可能由多种语言(fortran、cobol、c++、c、objective-c、汇编等)生成)链接到单个应用程序二进制文件中。

对象文件基本上是导出符号的集合,标记小字节块和未解析的符号,标记在链接期间需要修补以指向不同对象文件中的内容的字节块。

现在,常见的问题是,在我经历过的与您的平台不接近的平台上,C++ 编译器并未考虑到任何类型的跨语言二进制兼容性。另一方面,C 编译器是。因此,C++ 编译器会生成高度损坏的符号,将有关参数和返回类型的各种信息合并到符号名称中。这种机制使得操作符重载成为可能。

不管怎样,关键是,

  • 你需要构建 asm 文件。如果你没有汇编器,那你就运气不好了。它可能需要来自与您的 c/c++ 工具集相同的供应商
  • 您需要告诉您的 c++ 程序外部符号的名称。以交流兼容方式。这就是头文件的典型用途。

因此,创建一个头文件来保存 pid 代码的函数声明,并在其中放置类似的内容:

extern "C" void PidMain(void);

现在,在您的 C++ 程序中#include 该头文件,并确保汇编器生成的目标文件包含在您的环境链接步骤中,您应该是黄金了。

可能涉及某种调用约定。有些环境对于将事物推入堆栈的顺序和/或谁负责堆栈清理有不同的标准。大多数 ms windows 工具集至少支持 __pascal 和 __cdecl 调用约定,因此声明看起来像

extern "C" void __pascal PidMain(void);

我不知道您的特定环境中是否存在多个调用约定。所以我不知道这有多大帮助。我想祝你好运。

The usual method is to use a build environment and tools that has an assembler as well as a C/C++ compiler. Compilers (and assemblers) take source files and produce an intermediate object file format. On Microsoft Windows, with Microsoft Tools, these would be .obj files.

A linker is then used to link the .obj files - which were potentially produced by a wide variety of languages - fortran, cobol, c++, c, objective-c, assembly etc, into a single application binary.

Object files are basically a collection of exported symbols, labeling little chunks of bytes, and unresolved symbols, labeling chunks of bytes that need to be patched during linking to point to something in a different object file.

Now, the usual problem - on the platforms Ive experienced which are not close to yours, are, c++ compilers are not made with any kind of cross language binary compatibility in mind. C compilers on the other hand, are. So c++ compilers make highly mangled symbols that incorporate all sorts of information about parameter and return types into the symbol names. This mechanism is what makes operator overloading possible.

Anyway, the crux is,

  • You need to build the asm files. If you dont have an assembler, you're outa luck. And it probably needs to come from the same vendor as your c/c++ toolset
  • You need to tell your c++ program the names of the external symbols. In a c compatible way. This is what header files are typically used for.

So create a header file to hold function declarations for the pid code and place Something like this in them:

extern "C" void PidMain(void);

Now, #include that header file in your c++ program, and ensure that the object files produced by the assembler are included in your environments link step and you should be golden.

There might be some kind of calling convention involved. Some environments have different standards as to the order things are pushed onto the stack, and/or who is responsible for stack cleanup. Most ms windows toolsets support at least a __pascal and __cdecl calling convention, so the declration would look like

extern "C" void __pascal PidMain(void);

I have no specific knowledge if there are multiple calling conventions in your specific environment. So I don't know how helpful this has been. Good luck I guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文