Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembler passes issue

I have an issue with my 8086 assembler I am writing. The problem is with the assembler passes.

During pass 1 you calculate the position relative to the segment for each label.

Now to do this the size of each instruction must be calculated and added to the offset.

Some instructions in the 8086 should be smaller if the position of the label is within a range. For example "jmp _label" would choose a short jump if it could and if it couldn't it would a near jump.

Now the problem is in pass 1 the label is not yet reached, therefore it cannot determine the size of the instruction as the "jmp short _label" is smaller than the "jmp near _label" instruction.

So how can I decided weather "jmp _label" becomes a "jmp short _label" or not?

Three passes may also be a problem as we need to know the size of every instruction before the current instruction to even give an offset.

Thanks

like image 358
NibbleBits Avatar asked Dec 31 '25 16:12

NibbleBits


1 Answers

What you can do is start with the assumption that a short jump is going to be sufficient. If the assumption becomes invalid when you find out the jump distance (or when it changes), you expand your short jump to a near jump. After this expansion you must adjust the offsets of the labels following the expanded jump (by the length of the near jump instruction minus the length of the short jump instruction). This adjustment may make some other short jumps insufficient and they will have to be changed to near jumps as well. So, there may actually be several iterations, more than 2.

When implementing this you should avoid moving code in memory when expanding jump instructions. It will severely slow down assembling. You should not reparse the assembly source code either.

You may also pre-compute some kind of interdependency table between jumps and labels, so you can skip labels and jump instructions unaffected by an expanded jump instruction.

Another thing to think about is that your short jump has a forward distance of 127 bytes and that when the following instructions amount to more than 127 bytes and the target label is still not encountered, you can change the jump to a near jump right then. Keep in mind that at any moment you may have up to 64 forward short jumps that may become near in this fashion.

like image 123
Alexey Frunze Avatar answered Jan 04 '26 22:01

Alexey Frunze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!