Understanding Assembly MIPS .ALIGN and Memory Addressing

后端 未结 2 1998
误落风尘
误落风尘 2020-12-24 05:12

I\'m taking this course, and I\'m really struggling understanding the directive .align concept.

Here\'s an example, which I couldn\'t understand:

<
2条回答
  •  佛祖请我去吃肉
    2020-12-24 06:00

    Certain assembly directives imply that data is stored aligned, which means starting at an address that is a power of two. Recall first a couple of conventions in MIPS: 

    (1) a "word" is 4 bytes (you will sometimes see it defined as 2 bytes),

    (2) a halfword (.half) are 2 bytes, and

    (3) .asciiz null terminates the string (as in C).

    Using this, you already explained how var1 and str1 are stored. Why the buffer of 2 empty bytes before var2? Because it is declared a .word, and (by (1) above) will so be stored beginning at a memory location that is a multiple of 4. Had you declared it a .half, you'd have no 2 empty bytes between str1 and var2.

    var2 is declared a .half - it is a 16 bit (2 byte) address which fits into one. However, before declaring it the alignment is changed to 3. Now, check the first sentence: this is the power you raise 2 to; so we actually align to 8. This means that until overridden, variables will be placed as declared, but additionally their initial storage location must be a multiple of 8. Hence, the assembler inserts 4 empty bytes to store var3 at a multiple of 8. 

提交回复
热议问题