Mips 数据布局计算

发布于 2024-08-25 18:04:25 字数 712 浏览 2 评论 0原文

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

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

发布评论

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

评论(2

迷乱花海 2024-09-01 18:04:25

第二个链接显示MIPS无法打包变量,因此它们占用的地址必须落在字边界上。

如果short是半字对齐,则占用两个字节,int是字对齐,所以占用4个字节,double必须是双字对齐,所以占用8个字节。

为了在这些位置对齐。

最低有效位 (LSB) 中的零表示每隔一个或每 2 个字节(半字对齐),2 个零表示每第 4 个字节,每 8 个字节有 3 个零。

Address (4 LSBs)
    XXX0 - half word aligned (2 bytes)
    XX00 - Word aligned (4 bytes)
    X000 - Double word aligned (8 bytes)

double 必须双字对齐,因此它不能从 308 (100110100) 开始,因为它只是字对齐 (2 LSB = 0),它必须从下一个双字对齐 312 (100111000) 开始

[Addr]   [Binary]    [Alignment]  
300      100101100   Word, Half-Word
301      100101101
302      100101110   Half-Word
303      100101111   
304      100110000   Double-Word, Word, Half-Word
305      100110001
306      100110010   Half-Word
307      100110011 
308      100110100   Word, Half-Word
309      100110101   
310      100110110   Half-Word
311      100110111  
312      100111000   Double-Word, Word, Half-Word

The second link shows that MIPS cannot pack variables, therefore the addresses they take up must fall on word boundaries.

if short is half word aligned, it takes up two bytes, int, is word aligned so it takes up 4 bytes, double must be doubleword aligned so it takes up 8 bytes.

In order to align at these places..

A zero in the Least Significant bit (LSB) would indicate every other or every 2 bytes (half word aligned), 2 Zeros indicates every 4th byte, and 3 zeros every 8th byte.

Address (4 LSBs)
    XXX0 - half word aligned (2 bytes)
    XX00 - Word aligned (4 bytes)
    X000 - Double word aligned (8 bytes)

The double must be double word aligned so it can not start at 308 (100110100) because it is only word aligned (2 LSBs = 0) it must start at the next double word alignment 312 (100111000)

[Addr]   [Binary]    [Alignment]  
300      100101100   Word, Half-Word
301      100101101
302      100101110   Half-Word
303      100101111   
304      100110000   Double-Word, Word, Half-Word
305      100110001
306      100110010   Half-Word
307      100110011 
308      100110100   Word, Half-Word
309      100110101   
310      100110110   Half-Word
311      100110111  
312      100111000   Double-Word, Word, Half-Word
月竹挽风 2024-09-01 18:04:25

MIPS 上的内存访问是字对齐的,这意味着一次读取内存 32 位/4 字节。由于变量“b”是单个字节,因此它实际上读取地址300-303。如果变量“c”从 301 开始,处理器必须知道“b”只是一个字节,并将其他字节清零,并可能将其移至 LSB 位置(或者编译器必须这样做)。不管怎样,在 4 字节边界(4 的倍数)上进行所有内存访问会更有效。

有关详细信息,请参阅数据结构对齐

Memory access on the MIPS is word aligned which means that is reads the memory 32 bits/4 bytes at a time. Since variable "b" is a single byte, it actually reads addresses 300-303. If variable "c" were to start at 301, the processor would have to know that "b" is only a byte and zero out the other bytes and possibly shift it to the LSB position (or the compiler would have to do it). Either way, it's more efficient to just alight all memory access on 4 byte boundaries (multiple of 4).

See Data Structure Alignment for more info.

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