__asm__

__asm__

Post by Phong » Tue, 14 May 2002 11:51:38



Hi everyone,

Is there a C book or online documentation that shows how to use
__asm__ to write a function with inline assembly code?

I did try to read the source in /usr/src/linux but I can't make my
code to compile.

This piece of code compiled on Windows but do not under Linux:
int power2( int num, int power )
{
   __asm
   {
      mov eax, num    ; Get first argument
      mov ecx, power  ; Get second argument
      shl eax, cl     ; EAX = EAX * ( 2 to the power of CL )
   }
   /* Return with result in EAX */

Quote:}

So I changed the code to this:

int power2( int num, int power )
{
   __asm__ __volatile__
     (
      "mov eax, num"   /*  ; Get first argument     */
      "mov ecx, power"   /*  ; Get second argument    */
      "shl eax, cl"      /*  ; EAX = EAX * ( 2 to the power of CL ) */
      );

Quote:}

But I  could not compile...

Just show me a C book about __asm__ and I can read by myself.

Thanks in advance.
Peter Ho

 
 
 

__asm__

Post by Stephane SOPPER » Tue, 14 May 2002 14:34:29


have a look to the sources of mplayer (
http://www.mplayerhq.hu/homepage/ ). Look for __asm__ in the sources.
For example you'll find assembler in cpudetect.c for example.

Good luck.

--
Stephane SOPPERA
http://stephane.soppera.free.fr

 
 
 

__asm__

Post by Joe Leherbau » Tue, 14 May 2002 16:21:25



> Hi everyone,

> Is there a C book or online documentation that shows how to use
> __asm__ to write a function with inline assembly code?

> I did try to read the source in /usr/src/linux but I can't make my
> code to compile.

> This piece of code compiled on Windows but do not under Linux:
> int power2( int num, int power )
> {
>    __asm
>    {
>       mov eax, num    ; Get first argument
>       mov ecx, power  ; Get second argument
>       shl eax, cl     ; EAX = EAX * ( 2 to the power of CL )
>    }
>    /* Return with result in EAX */
> }

> So I changed the code to this:

> int power2( int num, int power )
> {
>    __asm__ __volatile__
>      (
>       "mov eax, num"   /*  ; Get first argument     */
>       "mov ecx, power"   /*  ; Get second argument    */
>       "shl eax, cl"      /*  ; EAX = EAX * ( 2 to the power of CL ) */
>       );
> }

> But I  could not compile...

> Just show me a C book about __asm__ and I can read by myself.

Go to http://www.linuxassembly.org/

Your example written for GNU C:

int power2 ( int num, int power )
{
  int result;

  __asm__ ("shl %2, %0"
           : "=r" (result)   /* output arg %0 in any register */
           : "0"  (num),     /* input arg %1 in same place as arg %0 */
             "c"  (power)    /* input arg %2 in ECX */
           );

  return result;

Quote:}

---
Joe Leherbauer             Leherbauer at telering dot at

"Somewhere something incredible is waiting to be known."
                                 -- Isaac Asimov

 
 
 

__asm__

Post by Randall Hyd » Wed, 15 May 2002 01:03:33



Quote:>Hi everyone,

>Is there a C book or online documentation that shows how to use
>__asm__ to write a function with inline assembly code?

>I did try to read the source in /usr/src/linux but I can't make my
>code to compile.

>This piece of code compiled on Windows but do not under Linux:
>int power2( int num, int power )
>{
>   __asm
>   {
>      mov eax, num    ; Get first argument
>      mov ecx, power  ; Get second argument
>      shl eax, cl     ; EAX = EAX * ( 2 to the power of CL )
>   }
>   /* Return with result in EAX */
>}

>So I changed the code to this:

>int power2( int num, int power )
>{
>   __asm__ __volatile__
>     (
>      "mov eax, num"   /*  ; Get first argument     */
>      "mov ecx, power"   /*  ; Get second argument    */
>      "shl eax, cl"      /*  ; EAX = EAX * ( 2 to the power of CL ) */
>      );
>}

>But I  could not compile...

>Just show me a C book about __asm__ and I can read by myself.

You should avoid inline assembly for exactly the reasons you're
discovering - it's poorly documented and it's not portable (from
compiler to compiler, obviously it's not portable across architectures).

Granted, your simple example begs for inline (though I'd ask why
you don't simply use the C "<<" operator for this specific example;
I hope it's just a test case).   Still, if it's worth doing in assembly it's
usually worth writing a complete function in assembly and calling
that function from your C code.

Duntemann's text ("Assembly Step-By-Step") and Paul  Carter's
on-line tutorials describe how to interface NASM with C/C++.
The Art of Assembly describes how to interface HLA (the High
Level Assembler) with C/C++.  Both assemblers are available
on Windows and Linux, so your assembly code will assemble
as-is under both OSes.

The Art of Assembly and HLA can be found at
http://webster.cs.ucr.edu.
There is also a link to Paul Carter's assembly tutorial
from the assembly links page on Webster (as well
as a link to Jeff Duntemann's page, his text is available
in most techical bookstores).
Randy Hyde

 
 
 

1. what is wrong with __asm__ ("int 3")

gcc complains that "operands given don't match any known 386 instruction"

I am trying to ask it to insert an interrupt 3.

Any idea what is wrong (or where the documentation is for gnu C's __asm__
for i386)?

Bill

2. #cola IRC channel

3. The meaning of "__asm__" & "__volatile_()"???

4. X on Linux Sparc

5. __asm__ inline assembler problem in GCC

6. Messed-up ext2 filesystem

7. GNU __asm__

8. __SVR4_I386_ABI_L1__ ?

9. __asm__ where is it and what is the syntax?

10. Need a good book or document for x86 __asm__ syntax!

11. help: __asm__ __volatile__ sysntax?

12. __asm__ usage ????