开发者

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. 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 clas开发者_JAVA技巧s 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?

Thank you all very much in advance.

Edit: I am working with gcc on a Fedora 14 powered x86 personal computer.


Creating a class object isn't much different from creating a normal stack variable. You just need to call the (copy-)constructor.
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 space1) and finally call the copy constructor of the class you want to create.
Then just push that local object on the stack and call your function. Afterwards you pass the address of your local object again1) and call the destructor.
How to exactly code that depends on your platform/architecture.

1) The ecx register is used to pass the this pointer on MSVC. GCC passes this as a hidden first parameter. Differences are summed up here. Only know this for x86 architecture, not for others, sorry.


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.

This doesn't seem like the most practical exercise… usually assembly is used within C++, not the other way around, via the asm keyword. In the rare exceptions, a plain C interface layer is used (via extern "C"), not the C++ application binary interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜