Solved

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

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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

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

Suggested Solutions

Title # Comments Views Activity
find our where the exe is being aborted with abort() 11 4,642
How do I defuse  Phase_2 in bomblab 12 1,022
DOS Dongle bypassing 10 1,625
Divide by zero in C and Javascript 13 490
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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