Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-06
8
Medium Priority
?
1,017 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 750 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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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