Search code examples
assemblyx86

What is the difference, if any, between LONG and FAR jumps in Assembly?


I'm looking at some practice code for assembly, and the assignment is basically to replace one jump point with another.

The original jmp is a SHORT jmp, and the end point I need to approach cannot be reached with this instruction.

I have three options now, I either remove 'SHORT', I insert 'LONG' or I insert 'FAR'.

If there's documentation anywhere that indicates the differences between them, I haven't found it yet. Can anyone be of assistance here?


Solution

  • I'm assuming your question pertains to the x86 architecture; you haven't specified in your question.

    A SHORT jump is a jump to a particular offset from the current instruction pointer address. A NEAR jump can use a larger offset value, and so can jump further away from the current instruction pointer address. Both of these jump types are usually relative - that is, the operand is an offset from the current instruction pointer (though in assembly source, you normally provide the target label - the assembler or linker then computes the offset). Neither of them jump to a different code segment.

    A FAR jump specifies both a segment and offset, which are both absolute in the sense that they specify the required code segment and instruction pointer, rather than an offset relative to the current code segment / instruction pointer.

    A "long" jump is usually another name for a FAR jump (eg the AT&T syntax uses ljmp as an equivalent to jmp far), but your question implies use of an assembler where it equates to a NEAR jump.

    To summarise, there are three types of direct jump: short and near, which are both jumps capable of jumping different relative distances with the same code segment, and far (or long), which can jump to any absolute address (segment and offset).

    (Note that it is also possible to perform an indirect absolute jump, where you specify an operand that holds the absolute address that you wish to jump to. In this case the jump can either be near or far - i.e. it can include or not include the required code segment).

    If you don't specify the jump 'distance', it is up to the assembler whether you get a short, near or far jump. Most modern assemblers are "two-pass" and will use a short jump if possible, or a near or far jump otherwise - the latter only if required.

    See wikipedia's entry on x86 memory segmentation if you need help with understanding what I mean by 'segment'.

    See this description of the x86 JMP instruction for full details of the possible JMP instruction addressing modes.