Solved

ASM in C++

Posted on 2004-09-18
3
1,335 Views
Last Modified: 2006-11-17
Is there a way to incorporate ASM in C++?  
0
Comment
Question by:fattumsdad
3 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 250 total points
ID: 12094383
In Microsoft VC++ you can use inline Assembly blocks __asm:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_core_assembler_.28.inline.29_.topics.asp

For example:

void SomeFunction()
{
    int x = 2;

    __asm
   {
        mov al, x
   }
}
0
 
LVL 11

Expert Comment

by:avizit
ID: 12094394
http://cs.wwc.edu/~aabyan/Unix/Assembly.html  

example code if you are doing in g++
0
 
LVL 3

Accepted Solution

by:
teratoma earned 250 total points
ID: 12094702
asm is a standard C and C++ keyword.  However, the format of the assembler code is undefined by the C/C++ standards, as it's dependent on the target CPU.  That's how to embed assembler code within C code.  To call a function defined in assembler from C, you just declare it "extern":

extern void func();

In C++ you have to declare it extern "C" to turn off name mangling:

extern "C" void func();

In the assembler it'll look something like

PUBLIC func
...
PROC func
...
END PROC

You can do the same with data:

extern "C" mydata;

in the asm file:

PUBLIC mydata
...
LABEL mydata
...

or even

STRUC mydata

Where the data structure is defined in parallel in asm and C++.  The syntax depends on what assembler you're using.

You can also call a C function from an assembler function, at least with Intel processors, using the "call" instruction.  This is highly non-portable if parameters need to be passed.  In typical applications this kind of thing is usually done by passing a function pointer at runtime to the low-level code.  This is usually for interrupt service routines (ISRs) which don't take or return values.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

831 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