Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15700
  • Last Modified:

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

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
BigBadBob
Asked:
BigBadBob
1 Solution
 
dude_1967Commented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now