www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - asm code and an inout function argument

reply Vladimir A. Reznichenko <kalessil gmail.com> writes:
I have a function:

void test (inout uint a)
{
	asm
	{
		mov a, 0x25;
	}
}

The trouble is that the function's call doesn't change the a variable.
Any ideas?
May 15 2009
next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Vladimir A. Reznichenko wrote:
 I have a function:
 
 void test (inout uint a)
 {
 	asm
 	{
 		mov a, 0x25;
 	}
 }
 
 The trouble is that the function's call doesn't change the a variable.
 Any ideas?

Inout variables are pointers.
May 15 2009
parent reply "Tim Matthews" <tim.matthews7 gmail.com> writes:
On Fri, 15 May 2009 22:29:07 +1200, Robert Fraser  
<fraserofthenight gmail.com> wrote:

 Vladimir A. Reznichenko wrote:
 I have a function:
  void test (inout uint a)
 {
 	asm
 	{
 		mov a, 0x25;
 	}
 }
  The trouble is that the function's call doesn't change the a variable.
 Any ideas?

Inout variables are pointers.

Why is it 'inout' and not 'ref' ?
May 15 2009
parent Vladimir A. Reznichenko <kalessil gmail.com> writes:
Tim Matthews Wrote:

 On Fri, 15 May 2009 22:29:07 +1200, Robert Fraser  
 <fraserofthenight gmail.com> wrote:
 
 Vladimir A. Reznichenko wrote:
 I have a function:
  void test (inout uint a)
 {
 	asm
 	{
 		mov a, 0x25;
 	}
 }
  The trouble is that the function's call doesn't change the a variable.
 Any ideas?

Inout variables are pointers.

Why is it 'inout' and not 'ref' ?

Aren't they the same?
May 15 2009
prev sibling next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Fri, 15 May 2009 14:24:16 +0400, Vladimir A. Reznichenko
<kalessil gmail.com> wrote:

 I have a function:

 void test (inout uint a)
 {
 	asm
 	{
 		mov a, 0x25;
 	}
 }

 The trouble is that the function's call doesn't change the a variable.
 Any ideas?

I believe your code is incorrect. This is how it should be done: import std.stdio; void test (out uint a) { asm { mov EDX, a; mov [EDX], 0x25; } } void main() { uint a = 0; test(a); writefln("0x%x", a); } Perhaps, errors like yours could be flagged at compile time? If so, an enhancement request would be nice.
May 15 2009
parent reply Vladimir A. Reznichenko <kalessil gmail.com> writes:
Denis Koroskin Wrote:

 On Fri, 15 May 2009 14:24:16 +0400, Vladimir A. Reznichenko
<kalessil gmail.com> wrote:
 
 I have a function:

 void test (inout uint a)
 {
 	asm
 	{
 		mov a, 0x25;
 	}
 }

 The trouble is that the function's call doesn't change the a variable.
 Any ideas?

I believe your code is incorrect. This is how it should be done: import std.stdio; void test (out uint a) { asm { mov EDX, a; mov [EDX], 0x25; } } void main() { uint a = 0; test(a); writefln("0x%x", a); } Perhaps, errors like yours could be flagged at compile time? If so, an enhancement request would be nice.

Thank you, Denis.
May 15 2009
parent Vladimir A. Reznichenko <kalessil gmail.com> writes:
Denis Koroskin Wrote:

 On Fri, 15 May 2009 14:41:35 +0400, Vladimir A. Reznichenko
<kalessil gmail.com> wrote:
 
 Denis Koroskin Wrote:

 On Fri, 15 May 2009 14:24:16 +0400, Vladimir A. Reznichenko  
 <kalessil gmail.com> wrote:

 I have a function:

 void test (inout uint a)
 {
 	asm
 	{
 		mov a, 0x25;
 	}
 }

 The trouble is that the function's call doesn't change the a variable.
 Any ideas?

I believe your code is incorrect. This is how it should be done: import std.stdio; void test (out uint a) { asm { mov EDX, a; mov [EDX], 0x25; } } void main() { uint a = 0; test(a); writefln("0x%x", a); } Perhaps, errors like yours could be flagged at compile time? If so, an enhancement request would be nice.

Thank you, Denis.

You are wellcome. But I stand corrected - your original code was correct, it just didn't do what you expected (I replaced inout with pointer for clarity): void test (uint* a) { writefln("0x%x", a); // prints 0x12FE88, may differ asm { mov a, 0x25; } writefln("0x%x", a); // prints 0x25, i.e. you were modifying 'a', not '*a' } The following would be correct, but it is disallowed and silently ignored: void test (uint* a) { asm { mov [a], 0x25; // no warning is issued, works as if there were no brackets around a, is that correct behavior? } }

It looks like "inout/ref uint a" is equal to "uint* a" but the situation when we write D's code "a = 5" means "*a = 5". This is not obvious, at all. So when I wrote asm code, it wouldn't work. Interesting implementation of inout arguments ) What's more interesting is that it wasn't reflected in inline asm documentation.
May 15 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Fri, 15 May 2009 14:41:35 +0400, Vladimir A. Reznichenko
<kalessil gmail.com> wrote:

 Denis Koroskin Wrote:

 On Fri, 15 May 2009 14:24:16 +0400, Vladimir A. Reznichenko  
 <kalessil gmail.com> wrote:

 I have a function:

 void test (inout uint a)
 {
 	asm
 	{
 		mov a, 0x25;
 	}
 }

 The trouble is that the function's call doesn't change the a variable.
 Any ideas?

I believe your code is incorrect. This is how it should be done: import std.stdio; void test (out uint a) { asm { mov EDX, a; mov [EDX], 0x25; } } void main() { uint a = 0; test(a); writefln("0x%x", a); } Perhaps, errors like yours could be flagged at compile time? If so, an enhancement request would be nice.

Thank you, Denis.

You are wellcome. But I stand corrected - your original code was correct, it just didn't do what you expected (I replaced inout with pointer for clarity): void test (uint* a) { writefln("0x%x", a); // prints 0x12FE88, may differ asm { mov a, 0x25; } writefln("0x%x", a); // prints 0x25, i.e. you were modifying 'a', not '*a' } The following would be correct, but it is disallowed and silently ignored: void test (uint* a) { asm { mov [a], 0x25; // no warning is issued, works as if there were no brackets around a, is that correct behavior? } }
May 15 2009
prev sibling next sibling parent Jesse Phillips <jessekphillips gmail.com> writes:
On Fri, 15 May 2009 07:14:50 -0400, Vladimir A. Reznichenko wrote:

 It looks like "inout/ref uint a" is equal to "uint* a" but the situation
 when we write D's code "a = 5" means "*a = 5". This is not obvious, at
 all. So when I wrote asm code, it wouldn't work.

Isn't that the point of a reference, that you don't have to dereference it? In fact I believe "*a = 5" would be an error when using references.
May 15 2009
prev sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Fri, 15 May 2009 19:11:55 +0400, Jesse Phillips <jessekphillips gmail.com>
wrote:

 On Fri, 15 May 2009 07:14:50 -0400, Vladimir A. Reznichenko wrote:

 It looks like "inout/ref uint a" is equal to "uint* a" but the situation
 when we write D's code "a = 5" means "*a = 5". This is not obvious, at
 all. So when I wrote asm code, it wouldn't work.

Isn't that the point of a reference, that you don't have to dereference it? In fact I believe "*a = 5" would be an error when using references.

Isn't it an error right now?
May 15 2009