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?

nadavge
  • 590
  • 1
  • 3
  • 14
  • And you cannot find any documentation on individual instructions for this *extremely* well documented CPU? – Jongware Mar 28 '15 at 10:45
  • 5
    The Intel manuals can be found [here](http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf) (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](http://www.agner.org/optimize/). – Michael Foukarakis Mar 28 '15 at 10:49
  • @Jongware 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 there – nadavge Mar 28 '15 at 11:33
  • Short jumps save instruction length. – EOF Mar 28 '15 at 13:33

1 Answers1

18

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.

evandrix
  • 6,041
  • 4
  • 27
  • 38
David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
  • What it this selector and how does this selector comes in practice? To my understanding in the long jump I simply supply an address – nadavge Mar 28 '15 at 11:36
  • 1
    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. – David Hoelzer Mar 28 '15 at 11:38
  • 1
    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) – David Hoelzer Mar 28 '15 at 11:39