www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to mov EAX, &func?

reply "Mehrdad" <wfunction hotmail.com> writes:
How do get the address of a function in naked assembly code?

void foo()
{
	asm
	{
		naked;
		mov EAX, &foo;
	}
}

This doesn't work...
May 15 2012
next sibling parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d0401fb13eefe1204c0195dc7
Content-Type: text/plain; charset=UTF-8

I think I had that one before. Try this one:
void foo()
{
    void* foopt = cast(void*)&foo;
    asm
    {
        naked;
        mov EAX fooptr;
    }
}

On Tue, May 15, 2012 at 11:33 PM, Mehrdad <wfunction hotmail.com> wrote:

 How do get the address of a function in naked assembly code?

 void foo()
 {
        asm
        {
                naked;
                mov EAX, &foo;
        }
 }

 This doesn't work...

-- Bye, Gor Gyolchanyan. --f46d0401fb13eefe1204c0195dc7 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I think I had that one before. Try this one:<div>void foo()</div><div>{</di= v><div>=C2=A0 =C2=A0 void* foopt =3D cast(void*)&amp;foo;</div><div>=C2=A0 = =C2=A0 asm</div><div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 = naked;</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 mov EAX fooptr;</div><div> =C2=A0 =C2=A0 }<br>}<br><div><br><div class=3D"gmail_quote">On Tue, May 15,= 2012 at 11:33 PM, Mehrdad <span dir=3D"ltr">&lt;<a href=3D"mailto:wfunctio= n hotmail.com" target=3D"_blank">wfunction hotmail.com</a>&gt;</span> wrote= :<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-le= ft:1px #ccc solid;padding-left:1ex"> How do get the address of a function in naked assembly code?<br> <br> void foo()<br> {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0asm<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0{<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0naked;<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0mov EAX, &amp;foo;<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0}<br> }<br> <br> This doesn&#39;t work...<br> </blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Bye,<br>Gor = Gyolchanyan.<br> </div></div> --f46d0401fb13eefe1204c0195dc7--
May 15 2012
parent =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
On 15-05-2012 22:51, Gor Gyolchanyan wrote:
 I think I had that one before. Try this one:
 void foo()
 {
      void* foopt = cast(void*)&foo;
      asm
      {
          naked;
          mov EAX fooptr;
      }
 }

 On Tue, May 15, 2012 at 11:33 PM, Mehrdad <wfunction hotmail.com
 <mailto:wfunction hotmail.com>> wrote:

     How do get the address of a function in naked assembly code?

     void foo()
     {
             asm
             {
                     naked;
                     mov EAX, &foo;
             }
     }

     This doesn't work...




 --
 Bye,
 Gor Gyolchanyan.

That's not naked inline asm, though. Something like this might work: void foo() { asm { naked; mox EAX, dword ptr foo; /* whatever... */ } } -- Alex Rønne Petersen alex lycus.org http://lycus.org
May 15 2012
prev sibling next sibling parent reply deadalnix <deadalnix gmail.com> writes:
Le 15/05/2012 21:33, Mehrdad a écrit :
 How do get the address of a function in naked assembly code?

 void foo()
 {
 asm
 {
 naked;
 mov EAX, &foo;
 }
 }

 This doesn't work...

As it is naked, mov EAX, EIP; is enough.
May 15 2012
next sibling parent ponce <spam spam.org> writes:
Le 15/05/2012 23:27, deadalnix a écrit :
 mov EAX, EIP;

 is enough.

I think this is illegal in assembly, try to call a label in your function then pop. void* selfAddress() // actually tested { asm { naked; call my_label; my_label: pop EAX; sub EAX, 5; ret; } }
May 15 2012
prev sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
On 15-05-2012 23:27, deadalnix wrote:
 Le 15/05/2012 21:33, Mehrdad a écrit :
 How do get the address of a function in naked assembly code?

 void foo()
 {
 asm
 {
 naked;
 mov EAX, &foo;
 }
 }

 This doesn't work...

As it is naked, mov EAX, EIP; is enough.

I suspect he wanted a solution where he could load an arbitrary function's address. -- Alex Rønne Petersen alex lycus.org http://lycus.org
May 15 2012
parent ponce <spam spam.org> writes:
Le 15/05/2012 23:58, Alex Rønne Petersen a écrit :
 I suspect he wanted a solution where he could load an arbitrary
 function's address.

I'm also searching for a way to get a label address from inline assembly. This would be useful for hard-coded jumptables.
May 15 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Tuesday, 15 May 2012 at 21:58:56 UTC, Alex Rønne Petersen 
wrote:
 On 15-05-2012 23:27, deadalnix wrote:
 Le 15/05/2012 21:33, Mehrdad a écrit :
 How do get the address of a function in naked assembly code?

 void foo()
 {
 asm
 {
 naked;
 mov EAX, &foo;
 }
 }

 This doesn't work...

As it is naked, mov EAX, EIP; is enough.

I suspect he wanted a solution where he could load an arbitrary function's address.

indeed
May 15 2012
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 5/15/2012 12:33 PM, Mehrdad wrote:
 How do get the address of a function in naked assembly code?

 void foo()
 {
 asm
 {
 naked;
 mov EAX, &foo;
 }
 }

 This doesn't work...

I know, but also be aware that even if it did work, it would not work in the presence of PIC code and shared libraries. It's best if you stick to using D code to get the address of a function, which accounts for all these variations and wierdness.
May 16 2012
parent Walter Bright <newshound2 digitalmars.com> writes:
On 5/16/2012 2:40 AM, Mehrdad wrote:
 The trouble with 'sticking to D code' is that, well, I can't... it's naked...

Just insert an asm call to a function that returns the address of foo.
May 16 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Wednesday, 16 May 2012 at 07:05:10 UTC, Walter Bright wrote:
 On 5/15/2012 12:33 PM, Mehrdad wrote:
 How do get the address of a function in naked assembly code?

 void foo()
 {
 asm
 {
 naked;
 mov EAX, &foo;
 }
 }

 This doesn't work...

I know, but also be aware that even if it did work, it would not work in the presence of PIC code and shared libraries. It's best if you stick to using D code to get the address of a function, which accounts for all these variations and wierdness.

Yeah I know, but currently shared libraries are the least of my worries. The trouble with 'sticking to D code' is that, well, I can't... it's naked...
May 16 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Wednesday, 16 May 2012 at 09:40:08 UTC, Mehrdad wrote:
 Yeah I know, but currently shared libraries are the least of my 
 worries.

Or position-independent code for that matter, although I'd be more concerned with this than the former.
May 16 2012
prev sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Wednesday, 16 May 2012 at 18:17:50 UTC, Walter Bright wrote:
 On 5/16/2012 2:40 AM, Mehrdad wrote:
 The trouble with 'sticking to D code' is that, well, I 
 can't... it's naked...

Just insert an asm call to a function that returns the address of foo.

Hmmm... interesting idea. Not a real fan of this solution, since it's extra code, and it involves a function call (and I was trying to avoid function calls in this code, since it's going to be in a boot-sector-like thing). But I think it'll work, thanks...
May 16 2012