I was browsing the Intel SDM recently and something jumped out at me.
...
15 RFLAGS.OF := if Is_Zero (result[operand_size+:operand_size]) then 0b0 else 0b1;
16 RFLAGS.SF := UNSPECIFIED : Bit;
17 RFLAGS.ZF := UNSPECIFIED : Bit;
18 RFLAGS.AF := UNSPECIFIED : Bit;
19 RFLAGS.PF := UNSPECIFIED : Bit;
20 RFLAGS.CF := if Is_Zero(result[operand_size+:operand_size]) then 0b0 else 0b1;The flags SF, ZF, AF and PF are all "Unspecified" which got me thinking:
There are a few reasons you might want to be able to do that:
I fuzzed various instructions with unspecified outputs across a few different CPUs and found a few places where you can distinguish microarchitectures apart:
Here's good one for if you ever need to tell AMD and Intel apart:
int is_intel(void) {
int tmp = 0x0800;
asm volatile(
"bextr %%rax, %%rax, %%rax \n\
pushf \n\
pop %%rax"
: "=a"(tmp));
return ((tmp >> 4) & 1) == 0;
}It turns out, for BEXTR
And if you can't remember if your CPU is AMD Zen3 vs Zen5:
int is_zen5(void) {
int tmp = 0;
asm volatile(
"movb $0x7F, %%al\n\
addb $1, %%al \n\
bsf %%eax, %%eax\n\
pushf \n\
pop %%rax"
: "=a"(tmp));
return ((tmp >> 11) & 1) == 0;
}The BSF/BSR instructions:
Some other examples:
Most Intel CPUs I tested seemed to agree here, except for one. On a Hetzner VPS that claims a "Skylake Xeon":
OF of SHL is reversedCode: https://gist.github.com/CUB3D/9f887a205af58ffb1d1d032dbcddaf2d
CPUs tested: