Alex Gaynor
Alex Gaynor

Reputation: 15019

What happens on an unaligned MOVSD on various CPUs?

Basically what the question says, if I execute a MOVSD that isn't 8-byte (or even 4-byte) aligned on various CPUs, what happens? Does it have a performance impact, can it segfault, etc.?

Upvotes: 3

Views: 866

Answers (1)

Scott Wisniewski
Scott Wisniewski

Reputation: 25051

From Volume 1 of the Intel Spec, Section 4.1

4.1.1 Alignment of Words, Doublewords, Quadwords, and Double Quadwords

Words, doublewords, and quadwords do not need to be aligned in memory on natural boundaries. The natural boundaries for words, double words, and quadwords are even-numbered addresses, addresses evenly divisible by four, and addresses evenly divisible by eight, respectively. However, to improve the performance of programs, data structures (especially stacks) should be aligned on natural boundaries when- ever possible. The reason for this is that the processor requires two memory accesses to make an unaligned memory access; aligned accesses require only one memory access. A word or doubleword operand that crosses a 4-byte boundary or a quadword operand that crosses an 8-byte boundary is considered unaligned and requires two separate memory bus cycles for access.

Under "normal" circumstances, an unalgined movsd will require 2 cycles to complete.

If you turn on alignment checking (in EFLAGS) then the CPU will raise an AC signal. It's primarily meant as a way to help you detect unaligned accesses.

Upvotes: 5

Related Questions