我的基于 AMD 的计算机使用小端还是大端?

发布于 2024-07-24 09:30:15 字数 417 浏览 8 评论 0原文

我正在学习计算机系统课程,并且我正在尝试确定,肯定,我的基于 AMD 的计算机是否是一台小端机器? 我相信这是因为它与英特尔兼容。

具体来说,我的处理器是 AMD 64 Athlon x2。

我知道这在 C 编程中很重要。 我正在编写 C 程序,我正在使用的方法会受到此影响。 我试图弄清楚如果我在基于 Intel 的机器上运行该程序是否会得到相同的结果(假设小端机器)。

最后,让我问一下:是否有任何和所有能够运行 Windows(XP、Vista、2000、Server 2003 等)的计算机,例如 Ubuntu Linux 桌面 是小端字节序吗?

I'm going though a computers system course and I'm trying to establish, for sure, if my AMD based computer is a little-endian machine? I believe it is because it would be Intel-compatible.

Specifically, my processor is an AMD 64 Athlon x2.

I understand that this can matter in C programming. I'm writing C programs and a method I'm using would be affected by this. I'm trying to figure out if I'd get the same results if I ran the program on an Intel based machine (assuming that is little endian machine).

Finally, let me ask this: Would any and all machines capable of running Windows (XP, Vista, 2000, Server 2003, etc) and, say, Ubuntu Linux desktop be little endian?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

终陌 2024-07-31 09:30:16

对于你的最后一个问题,答案是否定的。 Linux 能够在大端机器上运行,例如老一代的 PowerMac。

In answer to your final question, the answer is no. Linux is capable of running on big endian machines like e.g., the older generation PowerMacs.

沉鱼一梦 2024-07-31 09:30:16

您必须下载专为大端机器设计的 Ubuntu 版本。 我只知道 PowerPC 版本。 我确信您可以找到一些具有更通用的大端实现的地方。

You have to download a version of Ubuntu designed for big endian machines. I know only of the PowerPC versions. I'm sure you can find some place which has a more generic big-endian implementation.

蝶舞 2024-07-31 09:30:16
/* by Linas Samusas  */

#ifndef _bitorder 
#define _bitorder 0x0008

#if (_bitorder > 8)
#define BE
#else
#define LE
#endif

则使用它

#ifdef LE
#define Function_Convert_to_be_16(value)  real_function_to_be_16(value)
#define Function_Convert_to_be_32(value)  real_function_to_be_32(value)
#define Function_Convert_to_be_64(value)  real_function_to_be_64(value)
#else
#define Function_Convert_to_be_16
#define Function_Convert_to_be_32
#define Function_Convert_to_be_64
#endif

如果 LE

unsigned long number1 = Function_Convert_to_be_16(number2);

*macro 将调用实数函数,并且

如果 BE

unsigned long number1 = Function_Convert_to_be_16(number2);

*macro 将被定义为单词而不是函数,并且您的数字将在括号之间,

/* by Linas Samusas  */

#ifndef _bitorder 
#define _bitorder 0x0008

#if (_bitorder > 8)
#define BE
#else
#define LE
#endif

and use this

#ifdef LE
#define Function_Convert_to_be_16(value)  real_function_to_be_16(value)
#define Function_Convert_to_be_32(value)  real_function_to_be_32(value)
#define Function_Convert_to_be_64(value)  real_function_to_be_64(value)
#else
#define Function_Convert_to_be_16
#define Function_Convert_to_be_32
#define Function_Convert_to_be_64
#endif

if LE

unsigned long number1 = Function_Convert_to_be_16(number2);

*macro will call real function and it will convert to BE

if BE

unsigned long number1 = Function_Convert_to_be_16(number2);

*macro will be defined as word not a function and your number will be between brackets

抚你发端 2024-07-31 09:30:16

我们现在有了std::endian

constexpr bool is_little = std::endian::native == std::endian::little;

https://en.cppreference.com/w/cpp/types/endian

We now have std::endian!

constexpr bool is_little = std::endian::native == std::endian::little;

https://en.cppreference.com/w/cpp/types/endian

寄与心 2024-07-31 09:30:15

所有 x86 和 x86-64 机器(只是 x86 的扩展)都是小端字节序。

您可以通过以下方式确认:

#include <stdio.h>
int main() {
    int a = 0x12345678;
    unsigned char *c = (unsigned char*)(&a);
    if (*c == 0x78) {
       printf("little-endian\n");
    } else {
       printf("big-endian\n");
    }
    return 0;
}

All x86 and x86-64 machines (which is just an extension to x86) are little-endian.

You can confirm it with something like this:

#include <stdio.h>
int main() {
    int a = 0x12345678;
    unsigned char *c = (unsigned char*)(&a);
    if (*c == 0x78) {
       printf("little-endian\n");
    } else {
       printf("big-endian\n");
    }
    return 0;
}
酒绊 2024-07-31 09:30:15

用 C 语言编写与字节序无关的代码

const int i = 1;
#define is_bigendian() ( (*(char*)&i) == 0 )

An easy way to know the endiannes is listed in the article Writing endian-independent code in C

const int i = 1;
#define is_bigendian() ( (*(char*)&i) == 0 )
可爱暴击 2024-07-31 09:30:15

假设你安装了 Python,你可以运行这个单行代码,它将在小端机器上打印“little”,在大端机器上打印“big”:

python -c "import struct; print 'little' if ord(struct.pack('L', 1)[0]) else 'big'"

Assuming you have Python installed, you can run this one-liner, which will print "little" on little-endian machines and "big" on big-endian ones:

python -c "import struct; print 'little' if ord(struct.pack('L', 1)[0]) else 'big'"
蒗幽 2024-07-31 09:30:15

“兼容英特尔”并不是很准确。

英特尔曾经生产大端处理器,特别是 StrongARM 和 XScale。 这些不使用 IA32 ISA,通常称为 x86。

更早的历史上,Intel 还生产了小端 i860 和 i960,它们也不兼容 x86。

追溯历史,x86 的前身(8080、8008 等)也不兼容 x86。 作为8位处理器,字节序并不重要......

如今,Intel仍然制造Itanium(IA64),它是双字节序:正常操作是大字节序,但处理器也可以在小字节序模式下运行。 它确实能够以小端模式运行 x86 代码,但本机 ISA 不是 IA32。

据我所知,AMD 的所有处理器都与 x86 兼容,并具有一些扩展,例如 x86_64,因此必然是小端。

Ubuntu 可用于 x86(小端)和 x86_64(小端),ia64(大端)、ARM(el)(小端)、PA-RISC(大端)的移植不太完整,尽管处理器同时支持)、PowerPC(大端)和 SPARC(大端)。 我不相信有 ARM(eb)(大端)端口。

"Intel-compatible" isn't very precise.

Intel used to make big-endian processors, notably the StrongARM and XScale. These do not use the IA32 ISA, commonly known as x86.

Further back in history, Intel also made the little-endian i860 and i960, which are also not x86-compatible.

Further back in history, the prececessors of the x86 (8080, 8008, etc.) are not x86-compatible either. Being 8-bit processors, endianness doesn't really matter...

Nowadays, Intel still makes the Itanium (IA64), which is bi-endian: normal operation is big-endian, but the processor can also run in little-endian mode. It does happen to be able to run x86 code in little-endian mode, but the native ISA is not IA32.

To my knowledge, all of AMD's processors have been x86-compatible, with some extensions like x86_64, and thus are necessarily little-endian.

Ubuntu is available for x86 (little-endian) and x86_64 (little-endian), with less complete ports for ia64 (big-endian), ARM(el) (little-endian), PA-RISC (big-endian, though the processor supports both), PowerPC (big-endian), and SPARC (big-endian). I don't believe there is an ARM(eb) (big-endian) port.

陌上青苔 2024-07-31 09:30:15

下面的代码片段有效:

#include <stdio.h>

int is_little_endian() {
  short x = 0x0100; //256
  char *p = (char*) &x;
  if (p[0] == 0) {
    return 1;
  }
  return 0;
}

int main() {
  if (is_little_endian()) {
    printf("Little endian machine\n");
  } else printf("Big endian machine\n");
  return 0;
}

代码中的“短”整数是 0x0100(十进制 256),长度为 2 个字节。 最低有效字节是 00,最高有效字节是 01。小尾数顺序将最低有效字节放入变量的地址中。 所以它只是检查变量指针指向的地址处的字节值是否为0。
如果为0,则为小端字节序,否则为大端字节序。

The below snippet of code works:

#include <stdio.h>

int is_little_endian() {
  short x = 0x0100; //256
  char *p = (char*) &x;
  if (p[0] == 0) {
    return 1;
  }
  return 0;
}

int main() {
  if (is_little_endian()) {
    printf("Little endian machine\n");
  } else printf("Big endian machine\n");
  return 0;
}

The "short" integer in the code is 0x0100 (256 in decimal) and is 2 bytes long. The least significant byte is 00, and the most significant is 01. Little endian ordering puts the least significant byte in the address of the variable. So it just checks whether the value of the byte at the address pointed by the variable's pointer is 0 or not.
If it is 0, it is little endian byte ordering, otherwise it's big endian.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文