flat assembler
Message board for the users of flat assembler.

flat assembler > Compiler Internals > How FASM resolves labels?

Author
Thread Post new topic Reply to topic
eX_ploit



Joined: 31 Oct 2018
Posts: 2
I read that FASM does multiple passes to determine all labels correctly, but what is the actual algorithm used?
Post 31 Oct 2018, 21:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16097
Location: Transylbonia
The basic procedure is that fasm just keeps doing another pass until all labels are marked as resolved, or until the pass limit is reached, whichever comes first.

Maybe Tomasz will clarify some of the details, but the code is always there for inspection.
Post 31 Oct 2018, 21:59
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6981
Location: Kraków, Poland
First we need to define what it means that the labels are resolved. This is delicate matter, because no label can be considered resolved on its own, the entire assembly must be resolved as a whole. Even if a label seems to have a stable value, it may suddenly change when some other value reaches a critical point and causes a cascade of changes throughout the code.

Therefore, fasm searches for a resolved state of entire assembly, which is reached only when every prediction it makes during the assembly is fulfilled exactly.

The assembler has to make a prediction every time a symbol is used before being defined. Usually the value is predicted based on the value of the same symbol from the previous (unsuccessful) pass, but there are some additional heuristics that fasm uses to guess what the actual value is going to be.

If even a single value has been mispredicted, the pass is considered unsuccessful and assembler proceeds to make another one. Hopefully this time the predictions are going to be better, based on new knowledge gathered.

This general process is implemented in its pure form in my fasmg assembler, and there resolving applies to all kinds of entities that can be defined - including macroinstructions.

However, if you are interested more in how the process applies to specific machine-code-related cases, like resolving labels in x86 code, I recommend reading my article on types of multi-pass assembly.
Post 01 Nov 2018, 11:04
View user's profile Send private message Visit poster's website Reply with quote
eX_ploit



Joined: 31 Oct 2018
Posts: 2
Okay, thanks.
Post 02 Nov 2018, 02:40
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.