CPUID implementations in C++

Accessing raw CPUID information is actually very easy, here is a C++ class for that which works in Windows, Linux and OSX: #ifndef CPUID_H #define CPUID_H #ifdef _WIN32 #include <limits.h> #include <intrin.h> typedef unsigned __int32 uint32_t; #else #include <stdint.h> #endif class CPUID { uint32_t regs[4]; public: explicit CPUID(unsigned i) { #ifdef _WIN32 __cpuid((int *)regs, (int)i); … Read more

Should pointer comparisons be signed or unsigned in 64-bit x86?

TL:DR: intptr_t might be best in some cases because the signed-overflow boundary is in the middle of the “non-canonical hole”. Treating a value as negative instead of huge may be better if wrapping from zero to 0xFF…FF or vice versa is possible, but pointer+size for any valid size can’t wrap a value from INT64_MAX to … Read more

Why does printf print random value with float and integer format specifier

It’s undefined behaviour, of course, to pass arguments not corresponding to the format, so the language cannot tell us why the output changes. We must look at the implementation, what code it produces, and possibly the operating system too. My setup is different from yours, Linux 3.1.10-1.16-desktop x86_64 GNU/Linux (openSuSE 12.1) with gcc-4.6.2. But it’s … Read more

Number of executed Instructions different for Hello World program Nasm Assembly and C

The number of instructions executed in program 1) is high because of linking the program with system library’s at runtime? Yep, dynamic linking plus CRT (C runtime) startup files. used -static and which reduces the count by a factor of 1/10. So that just left the CRT start files, which do stuff before calling main, … Read more

Why is RCX not used for passing parameters to system calls, being replaced with R10? [duplicate]

X86-64 system calls use syscall instruction. This instruction saves return address to rcx, and after that it loads rip from IA32_LSTAR MSR. I.e. rcx is immediately destroyed by syscall. This is the reason why rcx had to be replaced for system call ABI. This same syscall instruction also saves rflags into r11, and then masks … Read more

Is it possible to decode x86-64 instructions in reverse?

An x86 instruction stream is not self-synchronizing, and can only be unambiguously decoded forward. You need to know a valid start-point to decode. The last byte of an immediate can be a 0x90 which decodes as a nop, or in general a 4-byte immediate or displacement can have byte-sequences that are valid instructions, or whatever … Read more