Thread overview
Setting class variable from inline assembly
May 20, 2006
pmoore
May 21, 2006
Thomas Kuehne
May 21, 2006
Thomas Kuehne
May 22, 2006
pmoore
May 20, 2006
Hi,

It seems that within inline assembly I can read a class variable but I can't set it. Is this a bug or is there a good reason for this?

uint myvar;

void main()
{
}

class AClass
{
uint myvar2;

this()
{
asm
{
mov EAX,myvar;
mov myvar,EAX;  // this is fine

mov EAX,myvar2; // no problem reading
mov myvar2,EAX;	// doesn't compile - bad type/size of operands 'mov'
}
}
}


May 21, 2006
pmoore schrieb am 2006-05-20:
> Hi,
>
> It seems that within inline assembly I can read a class variable but I can't set it. Is this a bug or is there a good reason for this?
>
> uint myvar;
>
> void main()
> {
> }
>
> class AClass
> {
> uint myvar2;
>
> this()
> {
> asm
> {
> mov EAX,myvar;
> mov myvar,EAX;  // this is fine
>
> mov EAX,myvar2; // no problem reading
> mov myvar2,EAX;	// doesn't compile - bad type/size of operands 'mov'
> }
> }
> }

mov EAX,myvar2; seems to move the offset of myvar2 - not the value:

# import std.stdio;
#
# class C{
#    int i;
#
#    void foo(){
#       int x;
#
#       asm{
#          mov EAX, i;
#          mov x, EAX;
#       }
#
#       writefln("%s at 0x%X -> 0x%X, %s", i, &i, x, x);
#       writefln("offset of i: %s", i.offsetof);
#    }
# }
#
# int main(){
#    C c = new C();
#    c.foo();
#
#    return 0;
# }

What you are looking for is:
#       void* p = this;
#
#       asm{
#          mov EBX, p;
#          mov EAX, [EBX+myvar2];
#          // something productive here
#          mov [EBX+myvar2], EBX;
#       }

Thomas


May 21, 2006
Thomas Kuehne schrieb am 2006-05-21:
>
> pmoore schrieb am 2006-05-20:
>> Hi,
>>
>> It seems that within inline assembly I can read a class variable but I can't set it. Is this a bug or is there a good reason for this?

> What you are looking for is:
> #       void* p = this;
> #
> #       asm{
> #          mov EBX, p;
> #          mov EAX, [EBX+myvar2];
> #          // something productive here
> #          mov [EBX+myvar2], EBX;
> #       }

The last line should have been:

mov [EBX+myvar2], EAX;

Thomas


May 22, 2006
Thomas,

You are right. Now that I think about it again it would be impossible for the compiler to know at runtime what the address of a class local variable would be so it makes sense to be an offset.

Thanks for taking the time to point this out.


In article <1qv6k3-m9f.ln1@birke.kuehne.cn>, Thomas Kuehne says...
>
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Thomas Kuehne schrieb am 2006-05-21:
>>
>> pmoore schrieb am 2006-05-20:
>>> Hi,
>>>
>>> It seems that within inline assembly I can read a class variable but I can't set it. Is this a bug or is there a good reason for this?
>
>> What you are looking for is:
>> #       void* p = this;
>> #
>> #       asm{
>> #          mov EBX, p;
>> #          mov EAX, [EBX+myvar2];
>> #          // something productive here
>> #          mov [EBX+myvar2], EBX;
>> #       }
>
>The last line should have been:
>
>mov [EBX+myvar2], EAX;
>
>Thomas
>
>
>-----BEGIN PGP SIGNATURE-----
>
>iD8DBQFEcM/F3w+/yD4P9tIRArUYAJ92IWIxHqfnsRYg86NQvRABJhXfrACePP8P
>ElcidKuVewNvYMjXQqQoOjo=
>=nNmk
>-----END PGP SIGNATURE-----