I’m trying to implement x86 cpuid in Zig, and the inline assembly feature doesn’t seem to be very well documented. How can I return EDX:ECX to a packed struct?
It is included in the standard library, but it is not public:
const CpuidLeaf = packed struct {
eax: u32,
ebx: u32,
ecx: u32,
edx: u32,
};
fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf {
// valid for both x86 and x86_64
var eax: u32 = undefined;
var ebx: u32 = undefined;
var ecx: u32 = undefined;
var edx: u32 = undefined;
asm volatile ("cpuid"
: [_] "={eax}" (eax),
[_] "={ebx}" (ebx),
[_] "={ecx}" (ecx),
[_] "={edx}" (edx),
: [_] "{eax}" (leaf_id),
[_] "{ecx}" (subid),
);
return .{ .eax = eax, .ebx = ebx, .ecx = ecx, .edx = edx };
}
3 Likes
This works, but I was hoping that there was some way to return a 64-bit field from 2 separate registers so I could query features to a packed struct easily.
The example is showing that it is already returning you a packed struct.
I want a return directly into a packed struct. This is just constructing a packed struct from the outputs. My struct has fields that are not just the registers being used.
Well, you can easily wrap this into something that pokes what you want into your struct.