CPU寄存器中的字节序 [英] Endianness inside CPU registers

查看:242
本文介绍了CPU寄存器中的字节序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来了解x86处理器的CPU寄存器内部的字节序。我写了这个小型汇编程序:

I need help understanding endianness inside CPU registers of x86 processors. I wrote this small assembly program:

section .data
section .bss

section .text
    global _start
_start:
    nop
    mov eax, 0x78FF5ABC
    mov ebx,'WXYZ'
    nop  ; GDB breakpoint here.
    mov eax, 1
    mov ebx, 0
    int 0x80

我在GDB中以第10行的断点运行了该程序(在上面的源中进行了评论)。在此断点处, info寄存器显示 eax = 0x78ff5abc ebx = 0x5a595857

I ran this program in GDB with a breakpoint on line number 10 (commented in the source above). At this breakpoint, info registers shows the value of eax=0x78ff5abc and ebx=0x5a595857.

由于W,X,Y,Z的ASCII码分别为57、58、59、5A;而intel是little endian,则0x5a595857看起来像是正确的字节顺序(最低有效字节在前)。那么为什么不输出eax寄存器 0xbc5aff78 (首先是数字0x78ff5abc的最低有效字节),而不是 0x78ff5abc

Since the ASCII codes for W, X, Y, Z are 57, 58, 59, 5A respectively; and intel is little endian, 0x5a595857 seems like the correct byte order (least significant byte first). Why isn't then the output for eax register 0xbc5aff78 (least significant byte of the number 0x78ff5abc first) instead of 0x78ff5abc?

推荐答案

字节序仅对内存有意义,其中每个字节都有一个数字地址。当将值的MSByte放入比LSByte高的内存地址时,称为Littte字节序,这是任何x86处理器的字节序。

Endianness makes sense only for memory, where each byte have a numeric address. When MSByte of a value is put in higher memory address than the LSByte, it's called Littte endian, and this is the endianness of any x86 processor.

对于整数,LSByte和MSByte之间的区别很明显:

While for integers the distinction between LSByte and MSByte is clear:

    0x12345678
MSB---^^    ^^---LSB

未定义用于字符串文字!不清楚 WXYZ 的哪一部分应视为LSB或MSB:

It's not defined for string literals! It's not obvious what part of the WXYZ should be considered LSB or MSB:

1)最明显的方法是,

1) The most obvious way,

'WXYZ' ->  0x5758595A

将导致存储顺序 ZYXW

2)内存顺序应该与文字顺序匹配时,不是很明显:

2) The not not so obvious way, when the memory order should match the order of literals:

'WXYZ' ->  0x5A595857

汇编器必须选择其中之一,并且显然选择了第二个。

The assembler have to choose one of them, and apparently it chooses the second.

这篇关于CPU寄存器中的字节序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆