Solved

convert a VC inline assembly code (in windows) to gcc inline assembly code (in linux).

Posted on 2004-10-06
8
1,002 Views
Last Modified: 2007-12-19
Hello,
I need to port a program from windows to Linux enironment. Because the inline assembly syntax difference between VC and gcc ,  I need to convert some inline assembly code. Actually I almost knows nothing about asembly things. Can somebody tell me how can I convert the following code (which is for vc compiler) to the inline assembly code for gcc? The problem is that I can not find the equivalence of __declspec( naked ) in gcc inline assembly syntax. Thanks.

/*the following code is for vc compiler, and the compiler generates code without prolog and epilog code */

#define __NAKED__ __declspec( naked )

/* the FullName and DesC are class defined in other place. DExecChunkNext are constant defined in header*/
__NAKED__ int chunkNext(FullName & /*aName*/,int & /*aFindHandle*/,const DesC & /*aMatch*/)
      {
      _asm mov eax, DExecChunkNext
      _asm jmp dword ptr dispatch
      }
0
Comment
Question by:rongz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 12237355
Hi rongz,
simple:

inline int chunkNext(){
asm volatile("jmp dispatch" :: "a"(DExecChunkNext));
}

...although I don't know if the function's parameters are dummy, or if they're used for real.

Cheers!

Stefan
0
 

Author Comment

by:rongz
ID: 12237398
HI,
Thanks. But how to prevent the  gcc compiler to generate prolog and epilog code for this function?
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 250 total points
ID: 12237813
rongz,
You mean the function's epilogue/prologue, right?

You can use a #define (although you won't get a value back). But normally an inline function shouldn't have prologue or epilogue when compiled with -O or -O2.

You could do:



The only exception is when the function's address is used. In case you need to create a jump table, you can do something like:

extern void exitv(void) __attribute__ ((noreturn));
int dummy (int) __attribute__ ((noreturn));

int dummy(int x)
{
        exitv();
}

This won't create a ret statement at the end of dummy(). It will ALWAYS keep the stack adjusted by 16 bytes, though (performance? Paging? Cache?).

0
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 

Author Comment

by:rongz
ID: 12238828
Hi,
Isn't the "dispatch" (in the above code) a function address?  

//-----------------code for VC environment
void dispatch()
{
 // function body

}

// i think parameters have used though i don't know how they used:-)
__declspec( naked )
int chunkNext(FullName & /*aName*/,int & /*aFindHandle*/,const DesC & /*aMatch*/)
     {
     _asm mov eax, DExecChunkNext
     _asm jmp dword ptr dispatch
     }

// wrapper function without "naked" keyword
int getNext(FullName & aName,int & aHandle, const DesC & aMatch)
{
   return chunkNext(aName, aHandle, aMatch);
}

//------------------------end of VC code ---------------------------
 
According you, the equivalence of the above code should be like the following:


//-----------------the code for gcc -----------------------
void dispatch()
{
 // function body

}

inline int chunkNext(){
asm volatile("jmp dispatch" :: "a"(DExecChunkNext));
}

int getNext(FullName & aName,int & aHandle, const DesC & aMatch)
{
   return chunkNext(aName, aHandle, aMatch);
}

// -------------- end of gcc code-----------------------

And still I don't know if and how i need to use  "noreturn" attribute to create jumb table? Thanks.
 
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12246634
rongz,
In case the parameters are used, GCC is screwing up the stack. I think the ASM routine is just adding something to EAX, and then jumping to the final routine, which will then use the EAX value to jump (dispatcher) and return to the original caller.

I *strongly* recommend that you rewrite this in C without any ASM. I don't see any added value in using ASM here.
0
 

Author Comment

by:rongz
ID: 12248719
Actually thses inline assembly code are part of kernel code in the software. And some of these functions (written in inline assembly) use  "naked" attribute ,  some not. And most of them are really similar to the code I presented above. So if rewritting in C, how should code should look like?:-) Thanks.  
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12285250
Think about re-writing the dispatcher, so that it no longer expects the routine in EAX, but as a regular argument.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12285269
I think GCC's inline assembly is about as powerful as inline assembly can get, both in flow integration and in efficient C/ASM data interchange. It should make sense to rewrite the C->ASM part.

Also check if the ASM part has calls not supported by Linux. If yes, drop ASM.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
In this blog we highlight approaches to managed security as a service.  We also look into ConnectWise’s value in aiding MSPs’ security management and indicate why critical alerting is a necessary integration.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question