Thread overview
Access class fields from asm
Jun 22, 2007
Carl Volhard
Jun 22, 2007
BCS
Jun 28, 2007
Carl Volhard
Jun 28, 2007
Deewiant
Jun 22, 2007
Manfred Hansen
Jun 26, 2007
Carl Volhard
Jun 26, 2007
Deewiant
June 22, 2007
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'
June 22, 2007
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
}  


June 22, 2007
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;
}


June 26, 2007
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
June 26, 2007
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.
June 28, 2007
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
> }
> 
> 

June 28, 2007
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.