问题描述
我是在这个过程中,和我真的很挣扎理解指令 .align伪
的概念。
I'm taking this course, and I'm really struggling understanding the directive .align
concept.
下面是一个例子,这是我无法理解:
Here's an example, which I couldn't understand:
我知道数据段里面,有地址,从0x10010000,0x10010020等。
I know that inside the data segment, there are addresses, starting with 0x10010000,0x10010020,etc.
我知道,每个地址里面,有8个内存领域,每个都有32位。
And I know, that inside each address, there are 8 memory fields, each has 32bit.
现在,我不明白的是,如何和为什么 VAR2
地址0x10010010里面呢? STR1
是地址0x10010003,因为我们保留3位为里面的 VAR1
。
Now, what I don't understand is, how and why var2
inside the address 0x10010010? str1
is inside the address 0x10010003 because we reserved 3 bits for the var1
.
的最后一件事是,究竟该指令 .align伪'在做什么?当我在Mars4测试它,它只能将数据移入下一个存储领域,当我用
对齐3`和,但我真的不明白这一点。
The last thing is, what exactly the directive .align' doing? when I tested it in Mars4, it only shifted the data into the next memory field when I used
align 3` and up, but I don't really get it.
我很抱歉,如果这是非常混乱的家伙,我有点绝望这里。
I'm sorry if this is very confusing guys, I'm kind of desperate here.
推荐答案
对齐是一个MIPS处理器重要的是,它只是喜欢在那的数据大小倍数的地址从存储器中读取多字节值。
Alignment is important for a MIPS processor, it only likes to read multi-byte values from memory at an address that's a multiple of the data size.
由于字符串被一次读取一个字节的.ASCIIZ字段可放置在任何地方。因此,将它放在0x10010003是好的。
The .ASCIIZ field can be placed anywhere since a string is read one byte at a time. So putting it at 0x10010003 is fine.
的.WORD字段必须对齐到4的倍数因此,它不能在0x1001000E,串后的下一个可用位置放。该汇编故意转移的价值,留下了两个字节使用。到这是4的倍数的下一个地址,0x10010010
The .WORD field must be aligned to a multiple of 4. So it can't be put at 0x1001000E, the next available location after the string. The assembler intentionally shifts the value and leaves two bytes unused. To the next address that's a multiple of 4, 0x10010010.
.align伪指令是覆盖默认的对齐规则的一种方式。该指令之后的下一个字段将对准的2的倍数n的功率,其中n是.align伪值。你的情况这是战俘(2,3)= 8个字节。
The .ALIGN directive is a way to override the default alignment rules. The next field after the directive will be aligned to a multiple of 2 to the power of n where n is the .ALIGN value. In your case that's pow(2, 3) = 8 bytes.
这就是你所看到的情况发生,如果没有.align伪指令的.HALF场将被存储在0x10010014。不是8的倍数,因此被移动到0x10010018。
Which is what you see happening, without the .ALIGN directive the .HALF field would be stored at 0x10010014. Not a multiple of 8 so it is moved to 0x10010018.
这个例子是人为的,否则,没有明显的理由在这里使用.align伪指令,因为.HALF只需要一个aligment至2日0x10010014所以其存储的倍数会被罚款。
The example is otherwise artificial, no obvious reason to use the .ALIGN directive here since .HALF only requires an aligment to a multiple of 2 so storing it at 0x10010014 would have been fine.
这篇关于了解大会MIPS .align伪和内存寻址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!