Solved

ASM in C++

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

679 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