Solved

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

Posted on 2004-10-06
8
982 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
  • 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now