Reputation: 355
For example:
jmp LABEL
... # loads of instructions
jmp LABEL
.... # loads of instructions
LABEL:
.....
Without the size of jmp LABEL
instruction, the address of LABEL can't be determine because the two forms of jmp instruction (short (2 Bytes), near (3 or 5 Bytes)) have different size. Whereas, without knowing about LABEL's address, you cannot determine which form to use.
How does assembler solve it?
Upvotes: 7
Views: 1411
Reputation: 941545
It depends on the kind of assembler you use. A simple 2-pass assembler (like MASM) makes it your problem. They'll pick a long jump and you have to write JMP SHORT LABEL
explicitly to get the short one. And bitch at you when you guessed wrong.
An optimizing n-pass assembler (like TASM) sorts it out by itself. It assumes the short jump and if it discovers that it can't reach then it restarts the assembly, now with a long jump.
You can easily tell what kind of flavor you have. Just look at the code listing it generates, if you get the 5 byte long jump then you have the 2-pass kind.
Upvotes: 7
Reputation: 2172
You can always use 5 bytes (opcode + far address) so this is what assembler does initialy. After then it runs (possibly multiple times) reduction algorithm to see if it can improve the code. Once it sees no improvement is possible it stops and code is done.
Upvotes: 1