?
Solved

how to write asm code in c under linux?

Posted on 2001-08-01
9
Medium Priority
?
266 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

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 800 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

864 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