简体   繁体   English

在x86(32位)程序集中实例化c ++类(通过复制将c ++类传递给程序集中的方法)

[英]Instantiating a c++ class in x86 (32bit) assembly (Passing a c++ class by copy to a method in assembly)

In my project, I have, amongst many other things, to call a c++ method from assembly and to pass a class by copy. 在我的项目中,除了许多其他方面,我还要从汇编中调用c ++方法并按副本传递类。 It would be quite simple if I could pass it by reference, but I cannot. 如果我可以通过引用传递它会很简单,但我不能。

I assume it would look something like this: create a new instance of said class; 我假设它看起来像这样:创建一个新类的实例; copy the attributes of the class to the attributes of the new class; 将类的属性复制到新类的属性; push a pointer to the new class onto the stack; 将指向新类的指针推入堆栈; call the method; 打电话给方法; call the destructor of the class that's a copy of the other class. 调用类的析构函数,它是另一个类的副本。

So the real question is, how do you instantiate a class that was created in c++ in assembly? 所以真正的问题是,如何实例化在汇编中用c ++创建的类?

Thank you all very much in advance. 非常感谢大家。

Edit: I am working with gcc on a Fedora 14 powered x86 personal computer. 编辑:我正在使用Fedora 14驱动的x86个人计算机上的gcc。

Creating a class object isn't much different from creating a normal stack variable. 创建类对象与创建普通堆栈变量没有太大区别。 You just need to call the (copy-)constructor. 你只需要调用(copy-)构造函数。
You make space on the stack for the local class object, push the object to create the copy from on the stack (as the argument for the copy ctor), pass the address of the local space 1) and finally call the copy constructor of the class you want to create. 您在堆栈上为本地类对象创建空间,推送对象以从堆栈上创建副本(作为复制ctor的参数),传递本地空间的地址1)并最终调用复制构造函数你想要创建的课程。
Then just push that local object on the stack and call your function. 然后将该本地对象推入堆栈并调用您的函数。 Afterwards you pass the address of your local object again 1) and call the destructor. 然后再次传递本地对象的地址1)并调用析构函数。
How to exactly code that depends on your platform/architecture. 如何准确编码取决于您的平台/架构。

1) The ecx register is used to pass the this pointer on MSVC. 1) ecx寄存器用于在MSVC上传递this指针。 GCC passes this as a hidden first parameter. GCC this作为隐藏的第一个参数传递。 Differences are summed up here . 差异总结在这里 Only know this for x86 architecture, not for others, sorry. 只知道x86架构,而不是其他人,对不起。

Anything relating to assembly is dependent on the processor and platform you're writing for. 与汇编有关的任何事情都取决于您所编写的处理器和平台。

The safest way to do what you ask is to allocate space on the stack for a new object of the class, and generate a call to the class' copy constructor. 执行所要求的最安全的方法是在堆栈上为类的新对象分配空间,并生成对类的复制构造函数的调用。

General answer: instantiating a C++ class involves generating a call to a constructor. 一般答案:实例化C ++类涉及生成对构造函数的调用。

This doesn't seem like the most practical exercise… usually assembly is used within C++, not the other way around, via the asm keyword. 这似乎不是最实用的练习...通常在C ++中使用汇编,而不是通过asm关键字。 In the rare exceptions, a plain C interface layer is used (via extern "C" ), not the C++ application binary interface. 在极少数情况下,使用普通的C接口层(通过extern "C" ),而不是C ++应用程序二进制接口。

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

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