Getting PA of a structure/inside a class

Help and discussing programming in C and C++.

Moderator:Moderators

Post Reply
Insightsoft
Posts:63
Joined:Wed Jul 22, 2009 6:44 am
Getting PA of a structure/inside a class

Post by Insightsoft » Tue Oct 26, 2010 6:11 pm

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?
_____________
Think it, build it, bit by bit...

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Getting PA of a structure/inside a class

Post by Andyhhp » Wed Oct 27, 2010 6:03 pm

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
Image

Insightsoft
Posts:63
Joined:Wed Jul 22, 2009 6:44 am

Re: Getting PA of a structure/inside a class

Post by Insightsoft » Wed Oct 27, 2010 11:26 pm

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?
_____________
Think it, build it, bit by bit...

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Getting PA of a structure/inside a class

Post by Andyhhp » Wed Oct 27, 2010 11:53 pm

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
Image

Insightsoft
Posts:63
Joined:Wed Jul 22, 2009 6:44 am

Re: Getting PA of a structure/inside a class

Post by Insightsoft » Thu Oct 28, 2010 2:25 am

Thanks...

Code: Select all

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

Thanks anyway...
_____________
Think it, build it, bit by bit...

Post Reply