0

In a one-pass assembler, the program reads the source code line-by-line. The program assembles the line to the best of its ability, and if there is a forward reference, it leaves a placeholder and saves the location for later. Then the forward reference can be resolved, either when the undefined label is defined, or at the end when all labels have been collected.

In a two-pass assembler, the first pass just collects all the labels and their addresses. However, to know the addresses, it needs to know the sizes of the instructions. To determine the size, it needs to lexically analyze the line, parse it, and check that it is valid code. Then it can calculate the size.

If you're going to make all that effort, you might as well assemble the line completely and write the binary to the output. If you don't, you have to repeat the lexing/parsing/code generation process once we're in the 2nd pass which seems really silly and wasteful. The only lines that can't be assembled are ones with forward references, but that's the case for one-pass assemblers too. It seems like a two-pass assembler has practically the same implementation as a one-pass assembler.

Am I just misunderstanding something?

mvskoke
  • 31
  • 5
  • 4
    A one pass assembler will have trouble with relaxiations, i.e. when there are instructions with multiple encodings (often some shorter than others) depending on the value of a displacement or immediate. – fuz Apr 08 '23 at 08:26
  • 2
    The GNU Assembler describes itself as "one pass", but that's one pass over the *source*; it still does multiple passes over an internal representation of the code to optimize branch displacements and instruction encodings, solving the problem @fuz mentioned. However, its one-pass nature does reveal itself when it has to guess whether an undefined symbol is an assemble-time constant like `x=123` or not, in cases where that affects how it assembles: [Distinguishing memory from constant in GNU as .intel\_syntax](https://stackoverflow.com/q/39355188) – Peter Cordes Apr 08 '23 at 08:39
  • 2
    Assemblers boasted with one-passeness in ancient times when operational memory and computing power were scarse. – vitsoft Apr 08 '23 at 10:40

0 Answers0