News:

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

FAR NEAR SHORT JMP In Win32

Started by msqweasm, May 29, 2011, 10:48:31 AM

Previous topic - Next topic

msqweasm

Newbie question

Most of the books about assembly talk about the 3 variants of JMP: FAR NEAR and SHORT.  But, are they really relevant in Win32?  Isn't it every address is addressable by a 32bit offset in win32?  Should I just use the NEAR JMP (1byte opcode + 4 bytes operands) version in the Wiin32 environment?

dedndave

in 32-bit flat model, most jumps are either NEAR or SHORT
FAR jumps may be used in kernel mode drivers or something similar

SHORT jumps are those whose distance is less than 128 bytes forward or 128 bytes or less backward
of course, SHORT jumps are smaller in code size
if you can keep loops smaller than 128 bytes, it helps to speed them up
i have found that, if you make a larger loop, the top of the loop wants to be 16-aligned
if they are SHORT, alignment seems to be non-critical

in 16-bit code, FAR jumps may be used in regular programs a bit more often

the assembler will pick the appropriate code for you
i try to explicitly use SHORT forward jumps, but that is just a habit from older assembler versions
the assemler tells me if the distance is to great with a "relative jump out of range" error

msqweasm

What actually is FAR JMP in win32?  Isn't NEAR is good enough to reach everywhere including the upper 2GB kernel space?  I mean near jmp uses 32bit offset already, right?


"...the top of the loop wants to be 16-aligned"

Does "wants" here mean must be or nice to have (say for better performance )?

dedndave

a far jump in win32 would be to an address outside the 4 gb addressable space
of course, it could be inside, too, if you wanted to pass the code segmet on the stack for some reason
if you were writing code that switches from real to protected mode, for example
or reverse engineering or authoring a virus   :bg

the 16-alignment is for better performance
near branches seem to execute faster, at least on older CPU's, if the target is 16-aligned
i have a pentium 4-class CPU - it may not be as important on newer cores

FORTRANS

Hi,

   Windows is set up in a "flat" memory model.  This has the
working segment registers pointing to descriptors that all map
to the same base and size (for all practical purposes).  so
you end up with a single 4 gigabyte address space.  If you
wrote your own operating system, you define separate
descriptors pointing to different address ranges.  Not that
that does any real good unless you have more than 4 gig.
of memory and your processer has enough address lines
to address that memory.

   An application running under win32 does not need a far
jump or call.

Steve

bomz

I use jmp short like in old times, but I think there is no difference.

You must find how many ticks use command. I can't find it for all commands

MichaelW

Far jumps and calls differ from short/near jumps and near calls in that they specify a destination segment. This destination segment must be a segment address for RM or a (valid) selector for PM, and it can be the current segment or some other (accessible) segment.
eschew obfuscation