www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Inline assembler] Sequential `asm` blocks and return via EAX

reply Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
1. Is there any guaranties that no code will be added between sequential 
inline assembler blocks, e.g.:
---
void f()
{
     static if(x)
         asm { mov EBX, 3; }
     else
         asm { mov EBX, 7; }

     asm { mov EAX, EBX; } // Is EBX value defined here?
}
---
Is it documented?


2. Such question about return via EAX (is the following use 
legal/documented):
---
int g()
{
     asm { mov EAX, 4; }
}
---
Such use of `asm` to return a value is used here and there in 
http://dlang.org/iasm.html
Mar 08 2012
next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 08-03-2012 10:42, Denis Shelomovskij wrote:
 1. Is there any guaranties that no code will be added between sequential
 inline assembler blocks, e.g.:
 ---
 void f()
 {
 static if(x)
 asm { mov EBX, 3; }
 else
 asm { mov EBX, 7; }

 asm { mov EAX, EBX; } // Is EBX value defined here?
 }

I don't think you can rely on this.
 ---
 Is it documented?

Probably not.
 2. Such question about return via EAX (is the following use
 legal/documented):
 ---
 int g()
 {
 asm { mov EAX, 4; }
 }
 ---
 Such use of `asm` to return a value is used here and there in
 http://dlang.org/iasm.html

As long as you follow whatever ABI your function uses (cdecl, stdcall, the D ABI, whatever), I don't see anything wrong with this. -- - Alex
Mar 08 2012
parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
08.03.2012 14:00, Alex Rønne Petersen пишет:
 On 08-03-2012 10:42, Denis Shelomovskij wrote:
 1. Is there any guaranties that no code will be added between sequential
 inline assembler blocks, e.g.:
 ---
 void f()
 {
 static if(x)
 asm { mov EBX, 3; }
 else
 asm { mov EBX, 7; }

 asm { mov EAX, EBX; } // Is EBX value defined here?
 }

I don't think you can rely on this.
 ---
 Is it documented?

Probably not.
 2. Such question about return via EAX (is the following use
 legal/documented):
 ---
 int g()
 {
 asm { mov EAX, 4; }
 }
 ---
 Such use of `asm` to return a value is used here and there in
 http://dlang.org/iasm.html

As long as you follow whatever ABI your function uses (cdecl, stdcall, the D ABI, whatever), I don't see anything wrong with this.

--- int g() { asm { mov EAX, 4; } } --- will work and --- int g() { asm { mov EAX, 4; } asm { } } --- will not. I'm pretty sure returning value is like relying on saving registers between sequential inline assembler blocks and even worse because "end of function" is and action and there are no actions between sequential inline assembler blocks.
Mar 09 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
On 8 March 2012 22:42, Denis Shelomovskij <verylonglogin.reg gmail.com> wro=
te:
 1. Is there any guaranties that no code will be added between sequential
 inline assembler blocks, e.g.:
 ---
 void f()
 {
 =C2=A0 =C2=A0static if(x)
 =C2=A0 =C2=A0 =C2=A0 =C2=A0asm { mov EBX, 3; }
 =C2=A0 =C2=A0else
 =C2=A0 =C2=A0 =C2=A0 =C2=A0asm { mov EBX, 7; }

 =C2=A0 =C2=A0asm { mov EAX, EBX; } // Is EBX value defined here?
 }
 ---
 Is it documented?


 2. Such question about return via EAX (is the following use
 legal/documented):
 ---
 int g()
 {
 =C2=A0 =C2=A0asm { mov EAX, 4; }
 }
 ---
 Such use of `asm` to return a value is used here and there in
 http://dlang.org/iasm.html

For 1. I'd say that it would probably work, but you can't necessarily rely on that, you may have to live with a bit of code duplication. For 2. It seems that it should be fine, I can't check it right now, but I would be surprised if it didn't compile and run. -- James Miller
Mar 08 2012
prev sibling parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Denis Shelomovskij" <verylonglogin.reg gmail.com> wrote in message 
news:jj9uv1$8o$1 digitalmars.com...
 1. Is there any guaranties that no code will be added between sequential 
 inline assembler blocks, e.g.:
 ---
 void f()
 {
     static if(x)
         asm { mov EBX, 3; }
     else
         asm { mov EBX, 7; }

     asm { mov EAX, EBX; } // Is EBX value defined here?
 }
 ---
 Is it documented?

I'm pretty sure dmd splits up all asm block into bunches of asm statements internally, so you should be able to rely on this. (dmd only) I don't think it's documented anywhere.
Mar 08 2012
parent =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 08-03-2012 14:48, Daniel Murphy wrote:
 "Denis Shelomovskij"<verylonglogin.reg gmail.com>  wrote in message
 news:jj9uv1$8o$1 digitalmars.com...
 1. Is there any guaranties that no code will be added between sequential
 inline assembler blocks, e.g.:
 ---
 void f()
 {
      static if(x)
          asm { mov EBX, 3; }
      else
          asm { mov EBX, 7; }

      asm { mov EAX, EBX; } // Is EBX value defined here?
 }
 ---
 Is it documented?

I'm pretty sure dmd splits up all asm block into bunches of asm statements internally, so you should be able to rely on this. (dmd only) I don't think it's documented anywhere.

I don't think it should be a formal language requirement either. It would be a rather harsh limitation on inline assembly implementations. -- - Alex
Mar 08 2012