views:

157

answers:

1

I have a project written in C that originally was being done on Linux, but now must be done on Windows. Part of the code include this line in several places

asm("movl temp, %esp");

But that causes an "undefined reference to `temp'" error.

This has no problem compiling on Linux using the gcc 4.3.2 compiler (tested on another machine), which is the version I have on Cygwin. Is there another way to accomplish what this line is doing?

+3  A: 

You need to change the cygwin version from

asm("movl temp, %esp");

to

asm("movl _temp, %esp");

Yes, it's the same compiler and assembler but they are set up differently for compatibility with the host system.

You can isolate the system-dependent symbol prefixing by simply telling gcc a specific name to use:

int *temp asm("localname");
...
__asm__("movl localname,%esp");

This avoids an #if of some sort and removes a host OS dependency but adds a compiler dependency. Speaking of compiler extensions, some people would write (see info as) something like:

#ifdef __GNUC__
    __asm__("movl %[newbase],%%esp"
                :
                : [newbase] "r,m" (temp)
                : "%esp");
#else
#error haven't written this yet
#endif

The idea here is that this syntax allows the compiler to help you out, by finding temp, even if it's lying about in a register or takes a few instructions to load, and also to avoid conflicting with you, it case it was using a register you clobbered. It needs this because a plain __asm__() is not parsed in any way by the compiler.

In your case, you seem to be implementing your own threading package, and so none of this really matters. Gcc wasn't about to use %esp for a calculation. (But why not just use pthreads...?)

DigitalRoss
Thanks, but this gives me the same error: "undefined reference to `_temp'"
Scotty
Just tried __asm__("movl _temp, %esp"); and everything works now.
Scotty