Solved

how to write asm code in c under linux?

Posted on 2001-08-01
9
248 Views
Last Modified: 2010-05-18
pleas give me a example!
0
Comment
Question by:sandy_wu
  • 4
  • 4
9 Comments
 
LVL 6

Expert Comment

by:graham_k
ID: 6343562
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
0
 

Author Comment

by:sandy_wu
ID: 6347024
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 !
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6347546
>  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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sandy_wu
ID: 6347707
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!
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6347988
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.
0
 

Author Comment

by:sandy_wu
ID: 6348012
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!
0
 
LVL 6

Accepted Solution

by:
graham_k earned 200 total points
ID: 6348227
There are some very good web based resources on this - for instance

IBM's "
 Inline assembly for x86 in Linux"  http://www-106.ibm.com/developerworks/linux/library/l-ia.html?dwzone=linux
0
 

Author Comment

by:sandy_wu
ID: 6357900
Thank u for your patient! I have some idea about asm in linux, thans very much!
0
 

Expert Comment

by:ksc3
ID: 6362937
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help Linux centos 5.7 3 95
troubleshooting a process that crashes within 3 mins 11 99
App holding yum lock unable to update my rpm package 1 86
bash file 10 62
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question