Solved

ASM in C++

Posted on 2004-09-18
3
1,328 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

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