Aviv A.
Aviv A.

Reputation: 697

Jump into the middle of instruction - in IA-32

Why IA-32 enable us to jump into the middle of instruction?

how can I use this architectonic characteristic for optimization when I'm writing in Assembler? (beside the obvious of cases we like to save command's encoding inside register and afterward activate this command) ?

Upvotes: 3

Views: 1373

Answers (2)

Brian Knoblauch
Brian Knoblauch

Reputation: 21349

It would be more correct to say that it doesn't force you to only jump to instruction beginnings...

The processor doesn't know where instructions start. It just sees a large collection of bytes. You can jump to whatever byte you want. It'll attempt to start decoding starting at that point.

Jumping into the middle of an instruction seems like a bad idea. The only non-error use I can see is jumping into an instruction to avoid a prefix (such as LOCK). No idea how that would be of any real use though.

Upvotes: 3

Thomas Pornin
Thomas Pornin

Reputation: 74382

IA-32 enable jumping into the middle of an instruction due to historical reasons. The x86 instruction set is the result of successive layers over the instruction set used by the 8080, the predecessor to the first "x86" -- which brings us back to the late 70s'. At that time, RAM was very expensive, and it was worthwhile to have instructions as short as possible, even if this implied that all instructions do not have the same length. Right now, an IA-32 instruction length can be anywhere between 1 byte and more than 12 bytes. This implies that any address can potentially be the start of an instruction (no alignment requirement) but many addresses point to some byte in the middle of an instruction. Jumping into the middle of an instruction instructs the CPU to reinterpret the machine code bytes starting with the jump target byte, resulting in quite different instructions.

For optimization, the proper way to use the "jump into the middle" feature is to not use it. The CPU decodes the instruction stream by converting it internally into a sequence of micro-instructions with a much more regular structure, and works over that to speed things up (parallel execution, speculative branch follow, and so on). Middle-jumping can wreak havoc into these optimizations.

Upvotes: 3

Related Questions