I'm taking this course, and I'm really struggling understanding the directive .align
concept.
Here's an example, which I couldn't understand:
I know that inside the data segment, there are addresses, starting with 0x10010000,0x10010020,etc.
And I know, that inside each address, there are 8 memory fields, each has 32bit.
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
.
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.
The MIPS assembler allows users to control data alignment using the . align directive. If the programmer does not use the directive, then the data will be automatically aligned in memory at the proper boundaries. Since all instructions are the same size (word), they must be aligned in memory.
2. 3. MIPS Memory. MIPS memory is byte-addressable, which means that each memory address references an 8-bit quantity. The MIPS architecture can support up to 32 address lines.
The MIPS ―load byte‖ instruction lb transfers one byte of data from main memory to a register. lb $t0, 20($a0) # $t0 = Memory[$a0 + 20] ▪ The ―store byte‖ instruction sb transfers the lowest byte of data from a register into main memory.
Alignment refers to the arrangement of data in memory, and specifically deals with the issue of accessing data as proper units of information from main memory. First we must conceptualize main memory as a contiguous block of consecutive memory locations. Each location contains a fixed number of bits.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With