Вопрос

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