Thread overview
Using ref and out parameters from inline assembly code
Feb 12, 2008
Harry Vennik
Feb 13, 2008
Burton Radons
Feb 13, 2008
novice2
February 12, 2008
Hi,

What is the right way to assign a value to a ref or out parameter from inline assembly code?

Can it just be referenced by its name like any other variable? Or would

mov EAX, some_out_param

move the address of the var into EAX? (i.e. out_param being handled as a pointer, and swapping those operands would overwrite the pointer with the value from EAX?)

Harry



February 13, 2008
"Harry Vennik" <htvennik@zonnet.nl> wrote in message news:fot28m$1m2h$1@digitalmars.com...
> Hi,
>
> What is the right way to assign a value to a ref or out parameter from inline assembly code?
>
> Can it just be referenced by its name like any other variable? Or would
>
> mov EAX, some_out_param
>
> move the address of the var into EAX? (i.e. out_param being handled as a pointer, and swapping those operands would overwrite the pointer with the value from EAX?)

I'd try it if I were you and see what happens, but my guess is that the compiler won't perform any magic here and you'll end up with the address of the variable in EAX.  ref and out params are implemented as pointers, so you'll probably need to dereference them as such in ASM.


February 13, 2008
Harry Vennik Wrote:

> Hi,
> 
> What is the right way to assign a value to a ref or out parameter from inline assembly code?
> 
> Can it just be referenced by its name like any other variable? Or would
> 
> mov EAX, some_out_param
> 
> move the address of the var into EAX? (i.e. out_param being handled as a pointer, and swapping those operands would overwrite the pointer with the value from EAX?)

It's exactly as if it were a pointer in asm, so the move in your case gets the address and you'll need a second move with [EAX] to store or retrieve any value. The only other thing you'll need to watch out for with low-level manipulation of inout/out parameters is that their address is for the object itself, and not the stack:

	extern (C) void func (int a, inout int b, ...)
	{
		// Bad, because that gets the pointer of the next value after whatever b points to.
		auto args1 = cast (void *) (&b + 1);

		// Good, but odd.
		auto args2= cast (void *) (&a + 2);
	}

It seems like "&&" should be able to get the pointer to the stack, but it won't.
February 13, 2008
> Can it just be referenced by its name like any other variable? Or would

i tried and failed. it needs double derefferencing, because inout param name give address of variable

This works for me:

[code]
//this func assing value 6 to param x

void func1(inout int x)
{
  asm
  {
    mov EAX, x;    //put x address in EAX
    mov [EAX], 6;  //put 6 into x
  }
}

[/code]