Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASM in C++

Posted on 2004-09-18
3
Medium Priority
?
1,360 Views
Last Modified: 2006-11-17
Is there a way to incorporate ASM in C++?  
0
Comment
Question by:fattumsdad
[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
3 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 1000 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 1000 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

Technology Partners: 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

722 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