C++ Class Objects in an OS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
PlayOS

C++ Class Objects in an OS

Post by PlayOS »

Hi,

I was just wondering if it is possible to create an instance of an object anywhere in memory that you want, like specify the exact address that you want it to be put. I need to do this in my OS so what kind of C++ support qould be required, if it is possible.

Also, does anyone know where I can get some docs about how classes are created in memory, like something that explains exactly what a class looks like in memory, how it is accessed and things like that.

thanks.
Tim

Re:C++ Class Objects in an OS

Post by Tim »

PlayOS wrote:I was just wondering if it is possible to create an instance of an object anywhere in memory that you want, like specify the exact address that you want it to be put. I need to do this in my OS so what kind of C++ support qould be required, if it is possible.
Sure, for objects created on the heap, at least:

Code: Select all

operator new(size_t size)
{
    return any_address_you_want;
}
Also, does anyone know where I can get some docs about how classes are created in memory, like something that explains exactly what a class looks like in memory, how it is accessed and things like that.
Check your compiler documentation. There's no set standard. Usually an instance of a class looks like the equivalent structure. Adding virtual methods puts a virtual method table somewhere (before or after the class data). However, you shouldn't usually need to know objects' binary layout unless you're trying to interface to C++ from C or assembly.
PlayOS

Re:C++ Class Objects in an OS

Post by PlayOS »

Thanks, the reason I want to know the layout of a class in memory is because I want to be able to copy the class in memory to a different location, then use that object as a seperate instance, I am just experimenting with some ideas and wanted to see if this would work, can you tell me if a object pointer points to the start of the object in memory.

Like say I create an instance of a class:

class clsInstance;

Would &clsInstance return the address of the class in memory so that I could do something like this to copy it:

char* clsptr = (char*)0x100000;
memcpy(clsptr, (char*)&clsInstance, sizeof(class));

then cast clsptr to type class, then use it as a seperate instance.

I have tried this and it partly worked, the reason I know this is because the class method that I called actually partly executed, the result was displayed on the screen.

anyway, thanks for your help. I hope that I am clear enough that you can understand what I am trying to do.
PlayOS

Re:C++ Class Objects in an OS

Post by PlayOS »

OK, I went back to my source and realised that I forgot to cast it, now that I have casted it everything goes according to plan.

I wasn't sure if it would work but it does.

thanks again. :)
Tim

Re:C++ Class Objects in an OS

Post by Tim »

Sure, you could do that. But you'd be better off using the C++ assignment operator, which does more or less the same thing.
Tom

Re:C++ Class Objects in an OS

Post by Tom »

I've never had to use the "operator" operator ( :D hmm that sounds funny ).

The default operators work enough for me, at least, but I did read that the are a way to control your code more.

I really don't see a need for the "operator" operator, unless your writing your own lib or making a C++ kernel with the "new" operator.
Tim

Re:C++ Class Objects in an OS

Post by Tim »

That's exactly the point. If you're writing a kernel in C++, you need to overload new and delete, otherwise they won't work.
PlayOS

Re:C++ Class Objects in an OS

Post by PlayOS »

Thanks Guys,

I just have a couple more questions:

1) Does anyone know why there are 2 Constructors and Destructors created for a class?

2) How would I go about writing code for member functions in asm?

I would rather not use inline asm if possible.

I have been searching for some answers but I have not been able to find anything, maybe I am not using good search strings.

thanks.
Tom

Re:C++ Class Objects in an OS

Post by Tom »

Well..I've only made classes with 1 constructor and 1 destructor...so I don't know what your talking about...here's how to call class functions in NASM:

It's just too tricky...instead, call a "extern "C" " function and your classe's function calles that function. But if you really want, i'll look it up...
PlayOS

Re:C++ Class Objects in an OS

Post by PlayOS »

If you disassemble your code files or look at the exported names in your object file, you will see that there have been 2 constructors and destructors, even if you only have one defined.

I am not sure why, but this is what is in my code after compilation.

thanks.
whyme_t

Re:C++ Class Objects in an OS

Post by whyme_t »

ok, i think this what you need to do in your inlined asm function (this is the way I've done it :)) :-

Code: Select all

someASMmemberFunc
push ebp
   mov ebp, esp
   push esi
   push ebx
   
   ;; do what you want here

   pop ebx
   pop esi
pop ebp
ret
In your C++ code :-

Code: Select all

//asm code inlined into the C++ function
extern "C" inline void someASMmemberFunc(void);

void SomeClass::someMemberFunc(void)
{
   someASMmemberFunc() ;
}
Note: I have totally ignored leading _'s in this code
whyme_t

Re:C++ Class Objects in an OS

Post by whyme_t »

and another thing. You won't, or at least I don't think, that you can access the data members of your class in your ASM. [ebp+8] will only give the address of the parameter passed in (is this right??). So you would need to do anything with you data members before and after your asm call, and pass in and out any data that you need to use in your asm code.

Someone want to confirm this? :)
Tim

Re:C++ Class Objects in an OS

Post by Tim »

You have several options for mixing assembly and C++:

1) Don't bother; write everything in C++
2) Write any assembly using the C++ compiler's inline assembler
3) Reverse engineer your compiler to work out the function and variable name decoration scheme and the layout of classes and vtables in memory, and do everything in assembly.

I recommend (1) with a little bit of (2). You are effectively trying to do (3).
adeelmahmood1

Re:C++ Class Objects in an OS

Post by adeelmahmood1 »

If you disassemble your code files or look at the exported names in your object file
well just a question ;D how do u guys disassemble the code or may be the question is what do u mean by disassembling a code? :)
Tom

Re:C++ Class Objects in an OS

Post by Tom »

ndisasm somefile.bin # disasm somefile.bin - see the binary code in asm.
Post Reply