Understanding Assembly MIPS .ALIGN and Memory Addressing

后端 未结 2 1991
误落风尘
误落风尘 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 05:47

    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.

    The .ASCIIZ field can be placed anywhere since a string is read one byte at a time. So putting it at 0x10010003 is fine.

    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.

    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.

    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.

    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.

提交回复
热议问题