www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Access class fields from asm

reply Carl Volhard <CarlVolhard web.de> writes:
How can i access non-static class fields?
The Code in the D-Specification doesn't work for me.
I've tried:

public class FooBar {
	public int b = 8;
}

FooBar foo = new FooBar();
FooBar *bar = &foo;
asm{
	mov EBX, [bar];
	mov EAX, FooBar.b[EBX]; 	//<- Line 65
}

and got following compile errors:


...Examples.d(65): Error: 'this' is only allowed in non-static member
...Examples.d(65): Error: this for b needs to be type FooBar not type int
...Examples.d(65): bad type/size of operands 'this.b'
Jun 22 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Carl,

 How can i access non-static class fields?

try somthing like this (I haven't even tried to compile this) public class FooBar { public int b = 8; } FooBar foo = new FooBar(); // foo is a pointer asm{ mov EBX, foo; // load by value mov EAX, [EBX+FooBar.b.offsetof]; //offset from pointer }
Jun 22 2007
parent reply Carl Volhard <CarlVolhard web.de> writes:
Curiously this way works
    foo.b.offsetof

although the documentation says
    FooBar.b.offsetof
is the correct way. But this code generates the following compile errors:

 Error: 'this' is only allowed in non-static member functions, not main
 Error: this for b needs to be type FooBar not type int

BCS Wrote:
 Reply to Carl,
 
 How can i access non-static class fields?

try somthing like this (I haven't even tried to compile this) public class FooBar { public int b = 8; } FooBar foo = new FooBar(); // foo is a pointer asm{ mov EBX, foo; // load by value mov EAX, [EBX+FooBar.b.offsetof]; //offset from pointer }

Jun 28 2007
parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Carl Volhard wrote:
 Curiously this way works
     foo.b.offsetof
 
 although the documentation says
     FooBar.b.offsetof
 is the correct way. But this code generates the following compile errors:
 
 Error: 'this' is only allowed in non-static member functions, not main
 Error: this for b needs to be type FooBar not type int


Ah, that'll be this then: http://d.puremagic.com/issues/show_bug.cgi?id=515 -- Remove ".doesnotlike.spam" from the mail address.
Jun 28 2007
prev sibling parent reply Manfred Hansen <m.hansen kielnet.net> writes:
Carl Volhard wrote:

 How can i access non-static class fields?
 The Code in the D-Specification doesn't work for me.
 I've tried:
 
 public class FooBar {
 public int b = 8;
 }
 
 FooBar foo = new FooBar();
 FooBar *bar = &foo;
 asm{
 mov EBX, [bar];
 mov EAX, FooBar.b[EBX];       //<- Line 65
 }
 
 and got following compile errors:
 
 
 ...Examples.d(65): Error: 'this' is only allowed in non-static member
 ...Examples.d(65): Error: this for b needs to be type FooBar not type int
 ...Examples.d(65): bad type/size of operands 'this.b'

I think it should work too. Here is my solution: import tango.io.Stdout; public class FooBar { public int b = 8; } void main() { int c; FooBar foo = new FooBar(); asm { mov EBX,[foo]; mov ECX,dword ptr[EBX+8]; mov c,ECX; } Stdout("c: ")(c).newline; }
Jun 22 2007
parent reply Carl Volhard <CarlVolhard web.de> writes:
Hello and thanks so far.

The first version with the offset does not work. I got the same compile errors.
The second version works. But I don't understand why I have to add 8 Bytes to
the address? What information fills the first 2 words?

Is there an option to access the fields without counting the offset? (like in
the first Version, only for non-static fields?)

greetings
Carl Volhard
Jun 26 2007
parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Carl Volhard wrote:
 Hello and thanks so far.
 
 The first version with the offset does not work. I got the same compile errors.
 The second version works. But I don't understand why I have to add 8 Bytes to
the address? What information fills the first 2 words?
 

This looks like a bug to me, as 8 is the offsetof of the field. You should file it in the 'Zilla. You should be able to find information on class structure at abi.html in the documentation.
Jun 26 2007