• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1022
  • Last Modified:

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

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
rongz
Asked:
rongz
  • 5
  • 3
1 Solution
 
stefan73Commented:
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
 
rongzAuthor Commented:
HI,
Thanks. But how to prevent the  gcc compiler to generate prolog and epilog code for this function?
0
 
stefan73Commented:
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.

 
rongzAuthor Commented:
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
 
stefan73Commented:
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
 
rongzAuthor Commented:
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
 
stefan73Commented:
Think about re-writing the dispatcher, so that it no longer expects the routine in EAX, but as a regular argument.
0
 
stefan73Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now