I'm still fighting with GCC - compiling the following inline assembly code (with -fasm-blocks, which enables Intel style assembly syntax) nets me a strange error Cannot take the address of 'this', which is an rvalue expression...
MyClass::MyFunction()
{
_asm
{
//...
mov ebx, this // error: Cannot take the address of 'this', which is an rvalue expression
//...
mov eax, this // error: Cannot take the address of 'this', which is an rvalue expression
//...
};
}
Why can I store pointers to different objects in registers, but can't use pointer to MyClass instance?
That's because the compiler might decide on its own to store this
in a register (generally ECX
) instead of a memory cell, for optimization purposes, or because the calling convention explicitly specifies it should do that.
In that case, you cannot take its address, because registers are not addressable memory.
You can use something like this:
#include <stdio.h>
class A{
public:
void* work(){
void* result;
asm( "mov %%eax, %%eax"
: "=a" (result) /* put contents of EAX to result*/
: "a"(this) /* put this to EAX */
);
return result;
}
};
int main(){
A a;
printf("%x - %x\n", &a, a.work());
}
See more details on operands passing to inline asm here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With