简体   繁体   English

如何将参数传递给从Assembly到C的函数

[英]How to pass parameter to a function from Assembly to C

I am using HCS08 and Code Warrior. 我正在使用HCS08和Code Warrior。 I am calling a C function from assembly. 我正在从汇编中调用C函数。 How can I pass parameter to this C function? 如何将参数传递给此C函数?

What you want is the ABI, or Application Binary Interface for your platform. 您需要的是平台的ABI或应用程序二进制接口 That will explain things like how to pass arguments to functions (registers, stack, a mix), which registers are caller saves and which are callee saves, special purposes for certain registers, and so on. 这将解释诸如如何将参数传递给函数(寄存器,堆栈,混合),哪些寄存器是调用者保存,哪些是被调用者保存,某些寄存器的特殊用途之类的事情。 By following a common ABI you are able to link libraries built by different compilers, mix high-level languages and so on. 通过遵循通用的ABI,您可以链接由不同的编译器构建的库,混合高级语言等等。

For "big" platforms it's usually easy to find a document specifying the ABI. 对于“大型”平台,通常很容易找到一个指定ABI的文档。 For others you may have to rely on disassembling a C function and looking at what it does. 对于其他人,您可能不得不依靠分解C函数并查看其功能。 Pay attention to which registers it saves in its prologue and which ones it might smash. 注意它保存在序言中的寄存器以及可能破坏的寄存器。 Also note how the prologue saves the stack pointer (or frame pointer) on entry because you will have to mimic that if you want debuggers to work. 还要注意序言如何在入口时保存栈指针(或帧指针),因为如果要调试器工作,则必须模仿。

It looks like the calling convention for HCS08 is documented by Freescale in an appnote . 看起来Freescale在一个应用笔记中记录了HCS08的调用约定。

Well, i found the solution. 好吧,我找到了解决方案。

In assembly file declare the variable as: 在汇编文件中,将变量声明为:

XREF   varaible1

and use it as memory location 并将其用作存储位置

In C file declare the variable as global. 在C文件中,将该变量声明为global。

extern char variable1;

Write c version (may be just a stub only passing arguments) for your function and analyse the code. 为您的函数编写C版本(可能只是仅传递参数的存根)并分析代码。 Do your function same way. 以相同的方式执行功能。 Calling method for specific function (in most cases) is uniquely defined by its set of arguments. 特定功能的调用方法(在大多数情况下)由其参数集唯一定义。

The common way is that first some arguments if they are integers or pointers passed in registers and the others are pushed on stack. 常见的方式是,首先将某些参数(如果它们是传递给寄存器的整数或指针),然后将其他参数压入堆栈。 But also note that varargs are often passed in different way than non-varargs. 但也请注意,可变参数通常以与非可变参数不同的方式传递。

Specially for 6808 which have few registers all parameters are likely passed through stack. 特别是对于很少有寄存器的6808,所有参数都可能通过堆栈传递。

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

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