Skip to content

The 25 days of C++mas - December 18 - C++Builder XE3 64-bit inline assembler

I was asked by a C++Builder customer, Peter Evans, for an example of how to use inline assembler code with our new C++Builder XE3 64-bit compiler.  Our Embarcadero DocWiki talks about C++Builder XE3 differences in the 64- bit compiler at http://docwiki.embarcadero.com/RADStudio/XE3/en/C%2B%2BBuilder_64-bit_Windows_Differences. In the section on inline assembler says "BCC64 does not support inline assembly in the style that is supported by BCC32. (Clang does support inline assembly, but with line-by-line AT&T syntax, not the more familiar block-of-Intel syntax. Furthermore, Win64’s exception handling places additional burdens on hand-written code intermixed with C++.) Functions written entirely in assembly (with a separate assembler like NASM or MASM) can be linked into your BCC64 program. However, there are restrictions (potential exception handling and debugging problems) that you should know."

BCC64, Clang and GCC use the AT&T inline assembler syntax - http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html.  After some more research I found an inline assembler article on Intel’s developer web site that helped me understand more about the new syntax and has an example that I used to test our C++Builder 64-bit compiler.

Inline Assembler Example Code

To start, I created a C++ console application using File | New | Other | C++Builder Projects | Console Application.  Next I added 64-bit Windows as the Target Platform in my Project Manager View.  Finally I replaced the console application code (generated by the New Project wizard), compiled and ran the Intel example.  If you are new to C++Builder XE3 you can follow the new project steps in my recent blog post, "Building and running your first C++Builder XE3 64-bit C++11 application", at http://blogs.embarcadero.com/davidi/2012/12/20/42150.

The following is the code from Intel’s article that adds two 128-bit integers together (note:  I added one line of code I felt was missing from Intel’s code to set the lo64 part of val2, "val2.lo64 = 1;" otherwise, the code is the same). The code declares a macro, ADD128, that contains the assembler code using the __asm__ keyword.

#ifdef _WIN64
#define INT64_PRINTF_FORMAT "I64"
#else
#define __int64 long long
#define INT64_PRINTF_FORMAT "L"
#endif
#include <stdio.h>
typedef struct {
    __int64 lo64;
    __int64 hi64;
} my_i128;

#define ADD128(out, in1, in2)                      \
	__asm__("addq %2, %0; adcq %3, %1" :           \
            "=r"(out.lo64), "=r"(out.hi64) :       \
            "emr" (in2.lo64), "emr"(in2.hi64),     \
            "0" (in1.lo64), "1" (in1.hi64));

extern int
main()
{
    my_i128 val1, val2, result;
    val1.lo64 = ~0;
    val1.hi64 = 0;
    val2.lo64 = 1;
    val2.hi64 = 65;

    ADD128(result, val1, val2);

    printf("0x%016" INT64_PRINTF_FORMAT "x%016"   INT64_PRINTF_FORMAT "x\n",
            val1.hi64, val1.lo64);

	printf("+0x%016" INT64_PRINTF_FORMAT "x%016" INT64_PRINTF_FORMAT "x\n",
            val2.hi64, val2.lo64);

    printf("------------------------------------\n");
    printf("0x%016" INT64_PRINTF_FORMAT "x%016" INT64_PRINTF_FORMAT "x\n",
			result.hi64, result.lo64);
	getchar();
    return 0;
}

The code compiles using C++Builder XE3 Update #1 (which contains the Win64 compiler) and produces the following output in a command line window:

Additional Inline Assembly Information Resources

Here are a few additional links with information about using inline assembler in your 64-bit C++ applications:

Watch/Download the C++Builder 64-bit Compiler Preview Video

Additional details about the 64-bit C++Builder compiler are available in the preview video.  Watch the C++ 64-bit compiler preview video on YouTube at http://www.youtube.com/watch?v=PwwMpBUoR6Y. You can also download the MP4 video file at http://cc.embarcadero.com/item/29197.  The preview video is 9 minutes long.

Try the C++Builder 64-bit compiler

The C++Builder XE3 and RAD Studio XE3 free trial downloads have been updated to include the new C++Builder 64-bit compiler.  Trial downloads are available at https://downloads.embarcadero.com/free/c_builder

Happy C++mas!

During the first 25 days of December leading up to Christmas, I will blog about the release of the C++Builder 64-bit compiler for Microsoft Windows and C++Builder XE3 support for building VCL and FireMonkey 2 applications.

{ 1 } Comments

  1. Tomohiro Takahashi | December 23, 2012 at 8:12 pm | Permalink

    David-san
    Please take a look at my simple bcc64 inline assembler tutorial.
    http://blogs.embarcadero.com/teamj/2012/12/24/3657/

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 4 access attempts in the last 7 days.

Close