I've seen various comments on the internet about xchg ax, ax
being used for alignment purposes (I know there are other as well, but let's forget about them for now), e.g. loop alignment. If stuff I've read is true, what is the reasoning for such alignment and what exactly code is aligned for? Is it a kind of optimization to make sure following code falls into as less cache lines as possible or something else?
Asked
Active
Viewed 276 times
0

Peter Cordes
- 328,167
- 45
- 605
- 847

Etki
- 2,042
- 2
- 17
- 40
-
2See https://agner.org/optimize/ for some of the reasons, e.g. that CPU front-ends often fetch instructions in *aligned* 16-byte blocks, so jumping near the end of a 16-byte block would get fewer useful bytes in the first fetch. But yes, reducing the footprint of functions is a possible benefit. – Peter Cordes Nov 04 '20 at 10:23
-
1Another possible duplicate: [x86 opcode alignment references and guidelines](https://stackoverflow.com/q/2485721) has pretty straightforward explanations of simple reasons, some of which are outdated on modern x86. Actually going to swap it for [Performance optimisations of x86-64 assembly - Alignment and branch prediction](https://stackoverflow.com/q/18113995) in the duplicate list. – Peter Cordes Nov 04 '20 at 10:48
-
Opcode 90h is machine code for the instructions `xchg ax, ax` (in 16 bit CS) or `xchg eax, eax` (in 32 bit CS). In 64 bit CS `nop` is not handled as `xchg eax, eax` however (because that would zero-extend into `rax`). But that's the origins of the encoding, it is the single-byte `xchg`-with-ax opcode. – ecm Nov 04 '20 at 11:14
-
@ecm you're probably answering another question – Etki Nov 28 '20 at 10:00