Februray 26th Monday (一月 二十六日 月曜日)

本文探讨了不同指令执行速度的差异,例如使用XOR指令加载寄存器为零的操作比加载指令更快。此外,文章还详细介绍了八进制与十六进制中每个数字所代表的比特数,并讨论了x86处理器上数据从寄存器到内存的字节序问题,即小端模式下数据的逆序存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  The different instructions execute at different speeds.  For example, XORing a number with itself produces 0.  Well, the XOR operation
is faster than the loading operation, so many programmers use it to load a register with zero.  For example, the code

movl $0, %eax

is often replaced by

xorl %eax, %eax

  In octal, each digit represented three bits.  In hexadecimal, each digit represents four bits.  Every two digits is a full byte, and
eight digits is a 32-bit word.

  One thing that confuses many people when dealing with bits and bytes on a low level is that, when bytes are written from registers
to memory, their bytes are written out least-significant-portion-first.  What most people expect is that if they have a word in a register,
say 0x5d 23 ef ee(the spacing is so you can see where the bytes are), the bytes will be written to memory in that order.  However, on x86
processors, the bytes are actually written in reverse order.  In memory the bytes would be 0x ee ef 23 5d on x86 processors.  The bytes are
written in reverse order from what they would appear conceptually, but the bits within the bytes are ordered normally.

  Not all processors behave this way.  The x86 processor is a little-endian processor, which means that it stores the "little end", or
least-significant byte of its words first.

  Other processors are big-endian processors, which means that they store the "big end", or most significant byte, of their words first,
the way we would naturally read a number.

  The byte-switching magic happens automatically behind the scenes during register-to-memorytransfers.  However, the byte order can cause
problems in several instances:

  * If you try to read in several bytes at a time using "movl" but deal with them on a byte-by-byte basis using the least significant byte
    (i.e.-by using %al and/or shifting of the register), this will be in a different order than they appear in memory.
  * If you read or write files written for different architectures, you may have to account for whatever order they write their bytes in.
  * If you read or write to network sockets, you may have to account for a different byte order in the protocol. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值