Solved

how to write asm code in c under linux?

Posted on 2001-08-01
9
243 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
Comment Utility
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
Comment Utility
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
Comment Utility
>  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
 

Author Comment

by:sandy_wu
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:graham_k
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank u for your patient! I have some idea about asm in linux, thans very much!
0
 

Expert Comment

by:ksc3
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now