intel docs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
elias

intel docs

Post 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?
Schol-R-LEA

Re:intel docs

Post 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'.
Post Reply