Difference between long and short jump (x86)
Asked Answered
E

1

11

I've read that short jumps are to be used when the relative jump is less than 124 in address, and long jumps should be used otherwise.

What is the difference in terms of operations performed in the CPU / performance between the two types of jumps on x86?

Eggnog answered 28/3, 2015 at 10:11 Comment(4)
And you cannot find any documentation on individual instructions for this extremely well documented CPU?Proximate
The Intel manuals can be found here (PDF warning). Look for 3-440 in Vol. 2A, it describes JMP in detail. Near and far jumps are covered in the same document. The difference in performance you can measure, or refer to the efforts of people who have, e.g. Agner Fog's page.Reclusion
@Proximate I really couldn't. Thanks Michael. Since I couldn't find the documetation, I would appreciate if you could tell where you looked for it so that in later cases I'd be able to find it myself. I mean mostly how did you know the answer is located thereEggnog
Short jumps save instruction length.Digital
J
19

There are actually three types of JMP instructions; short, near and far (long).

A short JMP is the relative JMP that you refer to. It is encoded as a two bytes; the actual JMP and the number of bytes +/- relative to the current IP.

A near jump allows you to jump within the current "segment" (using real mode terms) or within the currently selected memory area in the CS selector.

A long or Far JMP additionally includes a selector (or segment in real mode)

You can look up the timings for yourself. The biggest difference related to time is caused by the different numbers of bytes that must be read to accomplish the JMP.

Jez answered 28/3, 2015 at 11:31 Comment(3)
What it this selector and how does this selector comes in practice? To my understanding in the long jump I simply supply an addressEggnog
The address is composed of a selector and an offset. As in CS:1234. CS is used to define a selector out of the GDT or LDT when in protected mode. It would typically be something pretty small, especially when compared to real mode segmented memory since it is simply selecting an entry out of the table rather than defining an actual memory address. The memory address is defined in the GDT/LDT.Jez
I should add, it effectively looks the same whether you're in real mode or protected mode... it's the actual values that are different under the hood and the work that's done up front to set up the GDT (which is not needed in real mode since you're using segmented memory)Jez

© 2022 - 2024 — McMap. All rights reserved.