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
Solved

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

Posted on 2003-11-10
2
15,694 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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