Using Intel 80x86 Instructions in C/C++ programs with g++ 2.95.2

Using Intel 80x86 Instructions in C/C++ programs with g++ 2.95.2

Post by Kiril Avdei » Sun, 06 May 2001 00:52:13



Dear All,

I'm now customizing SGI STL for my own purposes on my Linux box. And I'd like to efficiently implement an atomic (thread safe) swap between two integers size of a general purpose register.

Below is the Assembly code in the Intel format, which gcc doesn't compile at all:

unsigned long  _Atomic_swap(unsigned long* __p, unsigned long __q)
{  
   asm("mov ecx, dword ptr [esp+4]"); // Load ECX with __p
   asm("mov edx, dword ptr [esp+8]"); // Load EDX with __q
   asm("mov eax, dword ptr [ecx]");   // Load EAC with
                                      // the current value
                                      // in __p

   asm("label: lock cmpxchg dword ptr [ecx], edx");
                                      // if (EAX == [ECX]),
                                      // [ECX] = EDX

   asm("jne short label");            // if (EAX != [ECX]),
                                      // repeat last
                                      // instruction

   asm("retn 8");                     // return to caller

Quote:}

Can anyone help me out with the correct syntax.

Thanks a lot in advance.

Kiril.

--
Posted from 172.16.27.62, 172.16.17.153 via proxy [212.72.53.70] by way of mx5.port.ru [194.67.23.40]
via Mailgate.ORG Server - http://www.Mailgate.ORG

 
 
 

Using Intel 80x86 Instructions in C/C++ programs with g++ 2.95.2

Post by Nate Eldredg » Sun, 06 May 2001 03:46:26



> Dear All,

> I'm now customizing SGI STL for my own purposes on my Linux box. And
> I'd like to efficiently implement an atomic (thread safe) swap
> between two integers size of a general purpose register.

> Below is the Assembly code in the Intel format, which gcc doesn't
> compile at all:

Yes, there are several problems.

1. The syntax.  Apparently recent versions of as support Intel
   assembly syntax, so you could try that, but it might still be
   subtly different than MASM's.  I happen to like the AT&T syntax,
   though, so my example will use that.

2. You have your instructions in separate asm blocks.  This means gcc
   is free to rearrange them, insert things in between, etc.

3. You could use gcc's "extended asm" features to avoid depending on
   the layout of the stack.  This is documented in the gcc manual.

4. You are using a global label.  If gcc decides to inline your
   function, the label will appear multiple times in the program, and
   it won't compile.  GNU as supports local labels (read the manual)
   which you should use instead.

5. Pedant point: Names starting with underscores are reserved for the
   library.

Here's how I would write your function.  I'm not sure what you're
trying to return; I'll assume it's the value in eax.  I'm afraid this
combines a lot of new ideas and may be hard to understand, but once
you understand it, you'll know a lot about effectively using GCC and
assembler together.  Note: This has been tested.

unsigned long Atomic_swap (unsigned long *p, unsigned long q)
{
  unsigned long retval;
  unsigned long dummy;
  /* GCC sets up the registers for us.  See below.
  asm("0:  lock; cmpxchgl %1, (%2) ;"
      "jne 0b"
      /* Here are the constraints (telling GCC what to do with various
         pieces of data) */
    : "=a" (retval), /* When done, store eax into retval. */
      "=r" (dummy)   /* This is an operand that might be modified
                        during the asm.  We can't use the "clobber" notation
                        because we also need this operand to contain something
                        on input (namely, q).  But we don't care about the
                        value afterwards, so we store it into a dummy variable.
                        The actual store should be optimized away.  This is
                        operand %1. */
    : "r" (p),       /* Before starting, load p into a register; it will
                        not be changed.  This is operand %2. */
      "1" (q),       /* Load q into the same register as is used for
                        operand %1. */
      "0" (*p)       /* Load *p into the same place as operand %0
                        (i.e. eax) */
    : "cc", "memory" /* This asm modifies the flags (condition code) and
                        arbitrary places in memory. */
        );
  return retval;

Quote:}

--

Nate Eldredge


 
 
 

1. Plug-Ins using C++

Hi folks,
        I hope I got the right forum for this: im a developer of a new
open-source project (http://threedsia.sourceforge.net) and have a problem
writing plug-ins:
Because we want to use mainly C++ as language I'd like to have C++ plug-ins, or
at least user C++ classes. While we already wrote some plug-ins with C and they
worked fine, I renamed a working plug-in from textonlyFrontEnd.c to *.cpp,
forcing make to use g++ as compiler. Compiles fine, but the resulting library
can't be accessed with dlsym... I already consulted what-ever-doc-I-could-find,
but I've not a suitable solution.
Thnx for any information,
        Marc Haisenko

2. Lockout after 3 failed login attempts

3. Which instruction sets used by WABI on Sun and Intel Solaris?

4. How do i set up 2 ethernet cards?

5. Calling a java program from a C++ program using execl

6. filtering bridge?

7. the Interphase SAN driver for the 5526 PCI Fibre channel Adapter for Linux for Intel 80x86

8. Questions, questions...

9. Using intel C++ icc with autotools

10. Anyone used the Intel C++ Compiler?

11. learning c++ with gcc-2.95...clear screen?

12. New Intel Supercomputer [Was: Re: Windows 95 ( Want a Mac

13. AD: TCAT C/C++ for Windows 95, NT and 3.1x Available by FTP