Get 1 byte from a 4 byte variable

Nov 24, 2009 at 1:40pm
I need to get the last byte (bits 24:31 inclusive) from an unsigned int variable into a char variable. I have a variable to store all 32 bits from ecx. According to AMD; the value I want (L1 cache size) is stored in bits 24:31.

How can I put these into another variable? I don't want the rest of the information (TLB stuff) :(
Nov 24, 2009 at 1:59pm
For which endianness?
If you want to do tricky stuff with bytes maintaining the endianness of the computer you are working on, you can make a char pointer pointing to the integer.
If you want to preserve a mathematical meaning, you should use bitwise operations ( >> & )
Nov 24, 2009 at 2:31pm
Just x>>24. If the variable is signed, you'll have to do (x>>24)&0xFF.
Last edited on Nov 24, 2009 at 2:32pm
Nov 24, 2009 at 7:19pm
Thanks!
Nov 25, 2009 at 1:10pm
Like this:
1
2
3
4
5
6
7
8
9
10
11
    unsigned ecx;

    asm volatile(
        "cpuid\n\t"
        :"=c"(ecx)
        :"a"(0x80000005)
    );

    /* The cache size is now stored in bytes 24:31 of ecx */

    char l1_sz = (ecx >> 24);

?
Last edited on Nov 25, 2009 at 1:11pm
Topic archived. No new replies allowed.