News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Short / far jump confusion!

Started by srod, December 10, 2006, 10:25:22 PM

Previous topic - Next topic

srod

Hi,

just a quick question if you don't mind (a very quick one!  :wink)

I don't want to clutter up the forum too much with too many irrelevant questions!

I've been experimenting with creating jump tables and am finding out the difference between short and far jumps the hard way!

Basically, are far jumps absolute and others relative?

If I try to jump indirectly via a memory location, e.g.

MOV [mem], ADDR dest
JMP [mem]


does this force a far jump?

The reason I ask is that I cannot get this to work when I switch 'dest' in the above code for an unscoped reusable label and so I figure it must be because I'm forcing an absolute jump and of course such a label will not have an absolute address.

Does CALL work in a similar way? I mean are there short and far versions etc?

Thanks for all the help. I'm getting there - slowly.  :bg

MichaelW

Far, with respect to jumps, calls, and returns, is a 16-bit concept, and GoAsm does not create 16-bit code. To answer your question, far jumps and calls are absolute, the other jumps and calls are relative (that is, encoded as a displacement), and returns will pop a 16-bit offset address from the stack (and a 16-bit segment address for a far return). This DEBUG output demonstrates the jumps and calls:

-a
0B07:0100 jmp 150
0B07:0102 jmp short 150
0B07:0104 jmp near 150
0B07:0107 jmp far 150
0B07:010C call 150
0B07:010F call near 150
0B07:0112 call far 150
0B07:0117
-u
0B07:0100 EB4E          JMP     0150
0B07:0102 EB4C          JMP     0150
0B07:0104 E94900        JMP     0150
0B07:0107 EA5001070B    JMP     0B07:0150
0B07:010C E84100        CALL    0150
0B07:010F E83E00        CALL    0150
0B07:0112 9A5001070B    CALL    0B07:0150


AFAIK for 32-bit code, of the sort the GoAsm produces, jumps are short (8-bit displacement) or near (32-bit displacement), calls are near (32-bit displacement), and returns pop a 32-bit offset address from the stack.
eschew obfuscation

srod

I see, nice example thanks.

So in 32 bit code, a near jump is a 32 bit offset from the current instruction - not from the beginning of the code section then?

Stephen.

MichaelW

You're close, but the offset is relative to the next instruction. The encoded displacement is a signed value that is added to the value in EIP (the offset address of the next instruction), and the result is the offset address of the destination. Because the displacement is a signed value, the jump or call can be forwards (to a higher address) or backwards (to a lower address). Displacements work the same in 32-bit code as they do in 16-bit code. For the first instruction in the example, the encoded displacement is 4Eh and the offset address of the next instruction is 102h, so 102h + 4Eh = 150h. And for the third instruction 107h + 0049h = 150h.
eschew obfuscation

srod

Aye, took me a while to see what was going on in your listing, but I understand now. The texts I'm using are still based within 16 bit architectures and is a little confusing at times when talking about short, near and far jumps.  Makes me want to jump out the window and scream!  :bg

Having just looked at a GoAsm produced list file I can see immediately that jumps are relative to the next instruction.

It's good stuff.

Thanks again.