Link to home
Start Free TrialLog in
Avatar of sandy_wu
sandy_wu

asked on

how to write asm code in c under linux?

pleas give me a example!
Avatar of graham_k
graham_k

int double(int val)
{

  asm
  {
    lds bx, val
    shl val, 1
  }

  return val;
}


or similar, it's been a few years since I coded any assembler. However, I presume taht you can handle that part of it. The Ansi C standard says that you use either

asm <statement>;

of

asm
{
  <multiple staements>
}

which is what you want to know
Avatar of sandy_wu

ASKER

thank you graham_k ; but it seems not right, actully I write my code like below:


void fun(long inputVar, long *eax, long *ebx, long *ecx, long *edx)
{
    long a, b, c, d;
   
    asm (
        mov eax, inputVar    
........
        movl a, eax        
        mov b, ebx        
        mov c, ecx        
        mov d, edx        
    );

    *eax = a;
    *ebx = b;
    *ecx = c;
    *edx = d;
}

there are on input argument, and four output argument, but it doesnot work in gcc, of cause it work under win32. some guy told me in linux cant use intel format asm, must use motolora format, So how can i translate this code to motolora format? thank !
>  linux cant use intel format asm, must use motolora format


don't listen to him, he doesn't know what he is talking about!  I don't know much, but I know enough to admit when I don't know, this guy just sounds stupid!

An intel processor won't understand Motorola instrcutions & Vice Versa!!   The only way he would be right is if you Linux is on a Motorla machien, like a Power PC (but then your windows Asm would have to be motorola too). Otherwise, he is 100% dead wrong.


Now, to your problem....

can you print the compiler errors here, please?  Then we will see if someone can help you (but stop listeing to your friend. If he is that dumb, you must have noticed that he says a lot of stupid things).


Hmmm, I don't do mixed asm & C, so will ask a maybe stupid quesyion.  You seem to be accessing eax etc outside of your asm staement. Can that be done???? Is that where your erro message is?
Thank U graham_k for you reply, I just want to write a program under linux to detech intel cpu info, the  "......" part in my last post is "CPUID", it seems that the format is different bettween linux and windows:
here is the error msg:  
----------------------------------------
checkcpu2.c: In function `fun':
checkcpu2.c:5: parse error before `{'
checkcpu2.c: At top level:
checkcpu2.c:13: `a' undeclared here (not in a function)
checkcpu2.c:13: warning: data definition has no type or storage class
checkcpu2.c:14: `b' undeclared here (not in a function)
checkcpu2.c:14: warning: data definition has no type or storage class
checkcpu2.c:15: `c' undeclared here (not in a function)
checkcpu2.c:15: warning: data definition has no type or storage class
checkcpu2.c:16: `d' undeclared here (not in a function)
checkcpu2.c:16: warning: data definition has no type or storage class
checkcpu2.c:17: parse error before `}'

---------------------------------------------

thanks again!
hmmm, it looks like the error is *before* your asm insert.   Comemnt all asm stuff out & recompile. I think taht you are looking at a simple compiler problem - either soemthing wrong with the parameter line, or missing } in the function above.

Try that & let us know.
Thank U graham_k for you reply, I just want to write a program under linux to detech intel cpu info, the  "......" part in my last post is "CPUID", it seems that the format is different bettween linux and windows:
here is the error msg:  
----------------------------------------
checkcpu2.c: In function `fun':
checkcpu2.c:5: parse error before `{'
checkcpu2.c: At top level:
checkcpu2.c:13: `a' undeclared here (not in a function)
checkcpu2.c:13: warning: data definition has no type or storage class
checkcpu2.c:14: `b' undeclared here (not in a function)
checkcpu2.c:14: warning: data definition has no type or storage class
checkcpu2.c:15: `c' undeclared here (not in a function)
checkcpu2.c:15: warning: data definition has no type or storage class
checkcpu2.c:16: `d' undeclared here (not in a function)
checkcpu2.c:16: warning: data definition has no type or storage class
checkcpu2.c:17: parse error before `}'

---------------------------------------------

thanks again!
ASKER CERTIFIED SOLUTION
Avatar of graham_k
graham_k

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u for your patient! I have some idea about asm in linux, thans very much!
hey graham_k,

>>  linux cant use intel format asm, must use motolora format
 >   don't listen to him, he doesn't know what he is talking about!  I don't know much, but I know enough
       to admit when I don't know, this guy just sounds stupid!

as I know, gcc doesn't support intel asm syntax, but motorola syntax. and I think this is what the guy wanted to tell sandy_wu. you misunderstood the statement, it is not about the cpu instruction, but the syntax.