سؤال

I understand that Intel x86 is little endian. However, how would I write assembly to programmatically determine the endianess of a processor?

هل كانت مفيدة؟

المحلول

Store a word that has different values for each of it’s two bytes (0x0001, for example). Load a byte from the same address and check to see which byte of the original word it matches.

نصائح أخرى

Implement it in C because C is portable to other architectures. We already know Intel is little endian. There is no use of implement it in assembly. This example return 1 if Little endian ditected otherwise return 0.

int host_endian()
{
    static const union {
        uint16_t    i;
        uint8_t     c[2];
    } test = { 0x1234 };

    return (test.c[0] == 0x34) ? 1 : 0;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top