?
Solved

Using assembly language with C++ - Inline ASM or seperate?

Posted on 2003-11-10
2
Medium Priority
?
15,697 Views
Last Modified: 2011-08-18
Hi. I can find much information on using assembler with C++ by either using inline ASM or by writing the ASM in your own files and then custom building them into your C++ project. However, I can't find any documentation regarding reasons to use one over the other. Can anyone help explain this? I am currently using Visual C++ and then adding asm files to my project, and using NASM to assemble them. Would I be able to achieve anything more (or less) by changing to inline ASM? Thanks very much,

BBB.
0
Comment
Question by:BigBadBob
[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
2 Comments
 
LVL 1

Accepted Solution

by:
dude_1967 earned 600 total points
ID: 9714461
Bob, big and bad,

For short assembler sequences, it can be wise to inline them, embedded within the C++ code. Vicual C++ actually keeps track of the registers used in the inline assembly code and the environment handles it very nicely. However, Visual C++ does not provide any language extension for writing complete subroutines within C++ files. For this you must, as you mention, write in separate ASM files.

I use full ASM files in the following situations:
1) I have long, complicated subroutines in ASM which merit complete function bodies of their own accord and are not well suited to inlining within the context of another C++ subroutine clothing.
2) The compiler-provided frame for a subroutine is inappropriate for my development. Please note that the compiler provides a very rich function frame involving, among other things, a bsp consistency check. In debug mode, the stack is adjusted upon subroutine entry just to provide extra breating room. There are some ASM situations which can not deal well with the compiler-created subroutine frame. A simple example from my experience is in the area of multitasking kernels.

For simple tasks such as reading the CPU identification registers or obtaining a time-stamp, you should stick to inline assembler as supported by the compiler. It can also be good to optimize or unroll loops using inline assembler since the inline assembler is quite gentelmanly and has knowledge of the C++ variable names within the program.

I hope this can help you out a bit.

Please feel free to ask if you want to discuss these matters more thoroughly or want additional clarification.

Sincerely, Chris.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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