Solved

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

Posted on 2003-11-10
2
15,695 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Arrow Electronics was searching for a KVM  (Keyboard/Video/Mouse) switch that could display on one single monitor the current status of all units being tested on the rack.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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