Solved

how to write asm code in c under linux?

Posted on 2001-08-01
9
249 Views
Last Modified: 2010-05-18
pleas give me a example!
0
Comment
Question by:sandy_wu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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
idM Identity Management in Red Hat 6 - setting up 9 271
getting permission denied error on stopping a service 1 46
„dd“ vs „ddrescue“ 28 277
awk to variable in bash 2 156
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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