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:
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.
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
next sibling 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 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 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 reply "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
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 reply 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
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