Inline Assembly with BCC64 (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example is C++ 64-bit Windows console application, that adds two 128-bit integer values using inline assembly. The example application uses code from Intel's product documentation.

Code

#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;
}

Output

Inlineassemblyoutput.png


See Also

Personal tools
Assistance