Outputting the Manufacturer ID of a processor using CPUID with inline ASM This outputs the manufacturer string associated with your CPU using the CPUID instruction. I wrote this up for a homework assignment, then found out that I didn't need to.

When run on my (virtual) machine, I get the following output:

EAX: 16
EBX: 756e6547
ECX: 6c65746e
EDX: 49656e69

GenuineIntel


What I'd like commented on:


I'm not super-proficient yet in bit manipulation. Originally, print_register had a for loop that iterated the mask directly, like

for (int32_t mask = 0xFF;; mask &lt;&lt;= 8) {


This worked until I realized that I needed to right-shift the result of the bitwise AND, and couldn't figure out how to get the bits shifted from the mask. I ended up going for a less efficient solution of iterating bit-counts and creating the mask each iteration. This feels like a naïve approach though.
Anything else notable. I'm starting to venture into increasingly unknown territory here, so anything would help.


Note that although inline asm seems to be considered bad practice to use in many cases, using it was the point of the exercise here. I know wrapper macros exist that would clean this up a bit.

#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;


void print_register(int32_t reg_value) {
    for (int shifted = 0; shifted &lt; 0x20; shifted += 8) {
        int32_t mask = 0xFF &lt;&lt; shifted;
        int32_t matched = (reg_value &amp; mask) &gt;&gt; shifted;
        printf("%c", matched);
    }
}


int main() {
    int32_t out_eax = -1;
    int32_t out_ebx = -1;
    int32_t out_ecx = -1;
    int32_t out_edx = -1;

    int32_t leaf = 0;

    asm volatile ("cpuid"
                  :  "=a"(out_eax),  // 0
                     "=b"(out_ebx),
                     "=c"(out_ecx),
                     "=d"(out_edx)
                  :  "0"(leaf));

    printf("EAX: %x\nEBX: %x\nECX: %x\nEDX: %x\n\n",
           out_eax, out_ebx, out_ecx, out_edx);

    print_register(out_ebx);
    print_register(out_edx);
    print_register(out_ecx);

    printf("\n");
}
Use uint32_t

You have to be careful with shift operations on signed integers. In particular, right shifts on signed integers have implementation-defined behaviour, so it is hard to write portable code for it. It might work out in this case, but it is better to treat the values as unsigned integers here.

Bit shifting

While you have written a correct function to print the individual bytes of a 32-bit integer, it is a bit overkill. You can write it like so:

void print_register(uint32_t value) {
    printf("%c%c%c%c",
           (int)value,
           (int)(value &gt;&gt; 8),
           (int)(value &gt;&gt; 16),
           (int)(value &gt;&gt; 24));
}


This uses the fact that when printing a character, the value will already by truncated to 8 bits as if you would have written value &amp; 0xFF, (value &gt;&gt; 8) &amp; 0xFF, and so on. The cast to int might be necessary because the %c conversion expects an int, but int32_t might have a different size than an int on some platforms (for example, on an Arduino with an AVR CPU, int will be 16 bits).