Solved

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

Posted on 2003-11-10
2
15,688 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
2 Comments
 
LVL 1

Accepted Solution

by:
dude_1967 earned 200 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

22 Experts available now in Live!

Get 1:1 Help Now