0
$\begingroup$

While studying COMPILER-DESIGN through an online book from Google Books, referenced as Compiler Design by A.A.Puntambekar, I got stuck across a line. Actually, I am more curious to know the inner-detail.

The assembler converts the assembly-program to low-level machine language using two passes. A pass means one complete scan of the input program. The end of the second pass is the relocatable machine code.

Why the 2 passes for conversion and what are the phases involved like lexical analysis,syntax analysis,etc while conversion from assembly to machine-code??? I have very less/no idea about it.

If someone over here would like to describe those two-passes or link out to some good resources, I'd be thankful to him/her.

$\endgroup$
  • $\begingroup$ Found pretty odd that none of the tags except compilers were already existing. They had to be created by me here while posting question! Is it really Computer Science Stack Exchange??? $\endgroup$ – Am_I_Helpful Nov 25 '14 at 20:37
  • $\begingroup$ Not many compiler question --> not many compiler tags. $\endgroup$ – Raphael Nov 26 '14 at 10:45
6
$\begingroup$

The first pass can't resolve any forward jumps. For example:

    cmp r1, 0
    bne label
    add r2, r3, r4

label:
    add r3, r4, r5

On the first pass, when the assembler gets to the bne label instruction, it doesn't know how far the branch needs to jump because it hasn't seen label yet. On the second pass, it knows where all the branch targets are located and therefore it can go ahead and generate the proper branch/jump instructions at that time.

$\endgroup$
  • 3
    $\begingroup$ Of course there are other ways to handle this, such as backpatching. But some older ISAs have different branch instructions depending on the distance (e.g. the 8086 short and long jumps). Using a shorter branch instruction may in turn reveal more opportunities for short jumps. So 8086 assemblers often iterated this to fixpoint. Two passes may not be enough! $\endgroup$ – Pseudonym Nov 25 '14 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.