Solved

ASM in C++

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 26
Converting to C# also VS2015 5 110
TTimer no longer functioning 7 74
C++ get user from AD  (VS6) 7 48
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…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

20 Experts available now in Live!

Get 1:1 Help Now