在32位架构的读取短裤(例如) [英] Reading shorts in 32-bits architectures (for example)
问题描述
首先,对不起,我的英语水平。
First of all, sorry for my English.
我知道架构是非常复杂的,有的情况下一个宽s prectrum,但一种常见的概括是,如果一个计算机体系结构具有32位的话,装置的寄存器,存储器访问和公共汽车用的32位字的工作长(但我觉得有很多的变种在当前的体系结构)。
I know architectures are very complex and there's a broad sprectrum of situations, but a common generalization is if a computer architecture has 32-bits words, means registers, memory accesses and buses work with words of 32-bits long (but I think there's a lot of variants in current architectures).
好吧,让我们假设这是规则,我们的架构是一个小端之一,86。在这种情况下,如果我们要读短整型
(2字节长),内存读取,然后在4字节字包含了短
。让我们假设包含词W是 0xf1342ea0
,内存:
Ok, let's suppose this is the rule and our architecture is a little-endian one, as x86. In such a case, if we want to read a short int
(2-bytes long), the memory reads then the 4-bytes word which contains our short
. Let's suppose the containing word W is 0xf1342ea0
, in memory:
{a0, 2e, 34, f1} // a0 is the byte in the lowest address.
和我们的半字H被在W的最高部分,然后,H是0xf134。据我所知,处理器接收,从内存,一个字与短
转变:
and our half-word H is in the highest part of W, then, H is 0xf134. I understand the processor receives, from the memory, a word with the short
shifted:
{34, f1, 00, 00}
由于0x0000f134等于0xf134。
since 0x0000f134 equals 0xf134.
通过这个画面在脑海中,由于处理器是4字节长,它是这样neccesary通过各种手段包换,为什么一定要2个字节的数据在2字节字边界对齐?
With this picture in mind, since the processor is 4-bytes long and it is thus neccesary by all means a shifting, why must 2-bytes data to be aligned in 2-bytes word boundaries?
在换句话说:
为什么令人鼓舞建议不要读短
中字0xf134:
Why is encouragingly recommended not to read the short
0xf134 in the word:
{ff, 34, f1, 0a}
修改:EX $ P $的其他方式pssing同样的疑问是:为什么对齐的定义是
EDIT: Other way of expressing the same doubt is: why the definition of alignment is
A object of size N and address d is aligned if d is divisible by N.
而不是
A object of size N and direction d is aligned respect to an architecture
of B bytes if d is divisible by B, or ⌊d/B⌋ == ⌊(d+N)/B⌋ if N < B.
注意:属性⌊d/B⌋==⌊(D + N)/B⌋意味着对象属于对齐的字
NOTE: The property ⌊d/B⌋ == ⌊(d+N)/B⌋ implies the object belongs to an aligned word.
推荐答案
如果内存{FF,34,F1,0A},那么它不是一个x86处理器的一个问题。然而,如果存储器是{FF,FF,FF,34} {F1,氨基酸,氨基酸,氨基酸},处理器必须执行两个总线周期来检索短的值。 (还要注意有一些根本不支持未对齐访问的基于RISC的处理器。)
If the memory is { ff, 34, f1, 0a }, then it's not a problem for an x86 processor. However, if the memory is { ff, ff, ff, 34 } {f1, aa, aa, aa }, the processor must perform two bus cycles to retrieve the value of the short. (Also note there are some RISC-based processors that do not support misaligned accesses at all.)
这篇关于在32位架构的读取短裤(例如)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!