-2

Several years ago I wrote some significant Cpp code that accessed the hardware registers by a coding command that switches to assembler language. I lost the compiler and computer. Please tell me a Cpp compiler that allows inline asembler in the middle of the Cpp code. Intel cpu, Windows. Thank you.

It seems I lacked clarity in the question. My apologies. The answers given were a refresher of the code. Well done. The answers given today suggest the C++ compilers might not have been updated for 64 bit assemblers. Here is a clearer question which has been only partially answered. It needs an updated response.

I am thinking of buying an Intel i7 desk computer. I will write C++ code for i/o and setup. The inner loops will be written in assembler language to take advantage of the hardware register multiply and divide: two multiplicands in separate registers give a double register product. My experience years ago was that not all C++ compilers are alike. Which of the many brands of C++ software out there give a good link to assembler, __asm, and make full advantage of 64 bit machines?

I feel this question has not been asked. Thanks for the great answers so far.

  • 2
    Gcc and Clang for sure. – Thomas Lang Apr 03 '19 at 06:09
  • 1
    MSVC supports this aswell. – tkausl Apr 03 '19 at 06:10
  • 2
    I am pretty sure most compilers do. Please go through certain compiler's documentation on how to do this. Most compilers use `asm` keyword. – Diodacus Apr 03 '19 at 06:14
  • The referenced answer explains syntax issues with various compilers. The answer I commented on directly addresses the compatibility and capability of recent compiler releases. As my first question was vague, would you please remove the duplicate question indicator. Thank you. – Steve Elmore Apr 04 '19 at 05:13

2 Answers2

0

I once used Microsoft Visual Studio to write inline assembly, like this:

// --- Get current frame pointer
ADDR  oriFramePtr = 0;
_asm mov DWORD PTR [oriFramePtr], ebp

Unfortunately, this only worked for 32-bit, because at that time the 64-bit compiler of Microsoft didn't support inline assembly (didn't check recently).

Patrick
  • 23,217
  • 12
  • 67
  • 130
  • 1
    The 64-bit Microsoft C/C++ compiler doesn't support inline assembly, and won't be in the future. You can install Intel's C/C++ compiler into the VS environment and it does support MS's inline assembly AND it supports GCC's inline assembly syntax as well. – Michael Petch Apr 04 '19 at 02:30
  • This answer directly answers the question Patrick. I will buy the computer and use Intel C/C++ compiler. As I was using Unix based OS and GCC compiler back then I think. With Windows on the new machine, I will reserve GCC and Clang as backups. – Steve Elmore Apr 04 '19 at 05:03
  • Excuse my lack of experience with the site. Patrick's answer was useful. It was the answer by Michael Petch that directly and completely answered my question. Unfortunately Michael wrote a comment and not an answer that I could check. – Steve Elmore Apr 05 '19 at 04:31
0

By default, C++ provides the asm keyword for writing assembly (bolded by me):

7.4 The asm declaration [dcl.asm]
1 An asm declaration has the form
asm-definition:
    asm ( string-literal ) ;
The asm declaration is conditionally-supported; its meaning is implementation-defined. [ Note: Typically it is used to pass information through the implementation to an assembler. — end note ]

GCC appears to support asm based on the above article on asm, but I couldn't find anything besides its support in C

MSVC does support assembly, but not via the asm keyword; one must use __asm:

The __asm keyword invokes the inline assembler and can appear wherever a C or C++ statement is legal.

Visual C++ support for the Standard C++ asm keyword is limited to the fact that the compiler will not generate an error on the keyword. However, an asm block will not generate any meaningful code. Use __asm instead of asm.

Tas
  • 7,023
  • 3
  • 36
  • 51
  • 1
    MSVC only supports inline asm for 32-bit x86, not x86-64 and I think not ARM or anything else. And MSVC's `__asm` has apparently always been kind of clunky and fragile: [What is the difference between 'asm', '\_\_asm' and '\_\_asm\_\_'?](//stackoverflow.com/posts/comments/59576185) – Peter Cordes Apr 03 '19 at 06:27