Page 1 of 1

Getting PA of a structure/inside a class

Posted: Tue Oct 26, 2010 6:11 pm
by Insightsoft
I have implemented the following code with a ASM file inside my project...
but I would like to use your way... it is more easy to keep track...


The Case: I'm building my own OS with C++, based in "classes"/not linear C... however, when I need to some low-level instructions, I have included "in project" assembly files...

for example:

Code: Select all

class definition _GDT
{
    protected struct record for IDTR
    member functions
    Install();
}

Code: Select all

_GDT::Install()
{
    call my low-level asm code to perform the lidt...
}
How do I use this beauty code?

Code: Select all

_asm lidt [_idtr]

The problem is that _idtr, in my case, is inside class capsule...

Code: Select all

_asm lidt [this->_idtr]
does not work

Is there any way to take the physical address of that structure that reside inside the class?

Re: Getting PA of a structure/inside a class

Posted: Wed Oct 27, 2010 6:03 pm
by Andyhhp
Which build environment are you using? MSVC based or GCC based?

There is no standard way to do this, but there are ways to get it working in both.

~Andrew

Re: Getting PA of a structure/inside a class

Posted: Wed Oct 27, 2010 11:26 pm
by Insightsoft
Visual C++ under Visual Studio

I solved the problem using this:

Code: Select all

		_asm	push eax
		_asm	mov	eax, dword ptr [this]              ;the pa of the class
		_asm	lgdt [eax]                                         ;because the struct is the first declaration inside the class
		_asm	pop eax
if the struct is not the first declaration we will need to add eax the correct displacement

Code: Select all

		_asm	push eax
		_asm	mov	eax, dword ptr [this]              ;the pa of the class
		_asm	add eax,  ????                              ;where is the location of the struct inside the class
		_asm	lgdt [eax]                                        
		_asm	pop eax

Its working....
But is there any other way?

Re: Getting PA of a structure/inside a class

Posted: Wed Oct 27, 2010 11:53 pm
by Andyhhp
Unfortunatly that is not safe. It assumes that no extra fields are inserted at the top of the structure, which is not the case if you are doing debugging, or if you are using virtual base classes.

Anyway, the proper way to reference fields in data structure is as so:

Code: Select all

_asm {
  lea eax,<object_name> ; Load Effective Address
  mov ecx, [eax]<object_name>.<field_name>
}
This will move the field value into ecx.

For reference, see http://msdn.microsoft.com/en-us/library/fabdxz08.aspx

Anyway, for your specific problem, you have to ensure that your GDT is a packed structure.

then your code can be as simple as:

Code: Select all

lea eax,<GDT object name>
lgdt [eax]
It is safe to assume that eax is fine - all other registers are not safe to assume anything about. (Typically, ecx is the 'this' pointer)

~Andrew

Re: Getting PA of a structure/inside a class

Posted: Thu Oct 28, 2010 2:25 am
by Insightsoft
Thanks...

Code: Select all

		__asm
		{
			mov		eax, [this]_IDT._idtr
			lidt	[eax]
		}

Thanks anyway...