Page 1 of 1

intel docs

Posted: Tue Nov 05, 2002 7:50 am
by elias
ive been reading the intel docs, and have a question. it says something that to creat a stack, it should be aligned on natural boundaries. can someone explain this?

Re:intel docs

Posted: Tue Nov 05, 2002 8:41 am
by Schol-R-LEA
What that means is that the address of the stack pointer should be set to an even multiple of 4. Memory accesses which cross word and word (in 16-bit code) or doubleword (in 32-bit code) boundaries are slower, because the memory bus actually has to make two or three separate accesses rather than one. For example, if you do

Code: Select all

   mov EAX, [aligned_variable] 
   mov EBX, [unaligned_variable]

align 4   ; ensures that the next variable is dword aligned
aligned_variable dd 0x01234567
nodata db 0               
;adds a byte, causing the next variable to begin on an odd-numbered address 
unaligned_variable dd 0x01234567
When the move copies aligned_variable, it is accessed as a single 32-bit value (76453201); but because unaligned_variable is not aligned on a doubleword boundary, the addressing unit has to first fetch the beginning part (765432) and then do a second fetch for the least significant byte (01).

Since the stack always pushes and pops in word (real mode) or doubleword (p-mode) increments, it is important that it be properly algined fro the start, or else every subsequent use of the stack will take twice or even three times as many memory accesses.

For more flavor, see chapter 11 of the GPBB, in the section 'Data Alignment'.