0

How do I know whether MASM encodes my JMP instruction using a relative or absolute offset?

I know that x86 provides JMP opcodes for relative and absolute offsets.

I want to be certain that my jumps are relative, however I cannot find any proper MASM documentation telling me whether JMP @label actually translates into a relative jump.

Please, if possible, give a link to documentation in the answer.


For the opposite issue: See How to code a far absolute JMP/CALL instruction in MASM? if you're trying to get MASM to emit a direct absolute far jmp

Community
  • 1
  • 1
Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • 1
    Disassemble the object file? (maybe some MASM guru will provide easier way how to specify the jmp should be relative, either by marking it so in source, or by setting up compilation to PIC (position-independent code), but as last resort disassembly is always here). – Ped7g Jul 18 '16 at 11:33
  • Yeah, but I'd hoped for some rigorous documentation. – Shuzheng Jul 18 '16 at 11:46
  • 2
    `JMP label` is always relative, unless it's a far jump and you'd know that ;) – Jester Jul 18 '16 at 12:03
  • @Jester: I could imagine an assembler that had a "huge code model" option which assembled jumps into memory-indirect jumps from a literal pool. Of course, no existing assemblers implement this imaginary feature that I know of, so I agree that there's nothing to worry about here. – Peter Cordes Jul 18 '16 at 12:35

1 Answers1

2

The only machine encodings for direct near jmps use relative displacements, not absolute. See also the tag wiki for more links to docs / manuals, including the official MASM manual. I don't think there's any point wading through it for this issue, though.


There is no near-jmp that takes an immediate absolute displacement, so there's no risk of an assembler ever using a non-PIC branch unexpectedly.

An absolute near jump would require the address in a register or memory operand, so MASM can't just assemble jmp target into

section .rodata
pointer_to_target dq target          ; or dd for a 32bit pointer

section .text
jmp    [pointer_to_target]

because that would be ridiculous. IDK if you'd ever find documentation stating that this specific piece of insanity is specifically impossible.

The only way I could imagine an assembler doing anything like this for you is if there was some kind of "Huge code model" where all jump targets were referenced with 64 bit pointers instead of 32 bit relative displacements.

But AFAIK, if you want to do that, you have to do it yourself in all existing assemblers.

Documentation gets bloated enough without mentioning every weird thing that a program doesn't do, so I wouldn't expect to find anything specific about this. This is probably one of those things that's assumed to be obvious and goes without saying. (Or that the syntax matches what Intel uses in their instruction reference manual.)


As Jester says, you won't get a far jump without asking for it, so there's no risk of any assembler using the JMP ptr16:32 encoding (EA imm16(segment) imm32(offset)) for a normal jmp.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Thanks! Do you have a good reference where I can look these issues up myself? – Shuzheng Jul 18 '16 at 12:30
  • 1
    A MASM 6.1 manual can be found [here](http://people.sju.edu/~ggrevera/arch/references/MASM61PROGUIDE.pdf) but It seems a re-post. – Margaret Bloom Jul 18 '16 at 12:36
  • @NicolasLykkeIversen: The [instruction set reference](http://www.felixcloutier.com/x86/). See the x86 tag wiki for more links (e.g. to Intel's official PDF that Felix Cloutier converts to HTML. – Peter Cordes Jul 18 '16 at 12:37
  • @MargaretBloom: I guess I'd never even tried to google for a MASM manual. Microsoft's official docs (https://msdn.microsoft.com/en-us/library/afzk3475.aspx) are online, and were the first hit. x86 tag wiki updated (with that and the old PDF, in case that's interesting for anyone). – Peter Cordes Jul 18 '16 at 12:46