Search code examples
assemblyarchitecturecompiler-constructionmachine-code

What is the use of relocatable machine code which is generated by Assembler? and why its get converted into absolute afterwards?


What is the use of relocatable machine code which is generated by Assembler? And if generated then whats the use of getting that relocatable machine code to convert it into absolute code?

I watched this video. https://www.youtube.com/watch?v=Qkwj65l_96I&t=309s

In that he mentioned about the Absolute and Relative Machine code


Solution

  • so the relocatable code uses relative addresses?

    No. At least not necessarily.

    Or do you mean "position-independent code" instead of "relocatable code"?

    What is the use of relocatable machine code which is generated by Assembler?

    Theoretically you could assemble a whole program at once. (Indeed I already did this when I wrote an assembler for a historic CPU.)

    However this has one main disadvantage:

    Think of the following line of code:

    mov [myVariable], eax
    

    Let's say the variable myVariable is located at address 0x1234560. Then in the machine code you'll have the following instruction:

    mov [0x1234560], eax
    

    Now you modify one file in your project which consists of ~200 files (which is typical for projects in the automotive industry). Let's say you added some instructions to some file at the start of the project.

    This means the addresses of all elements (files) that follow that file in the project will change. Let's say the address of myVariable now is no longer 0x1234560 but 0x1234870.

    This means our line of code must now be translated into the following instruction:

    mov [0x1234870], eax
    

    Because of this all files in your project must be assembled again!

    If you have relocatable code the following instruction is generated:

    mov [0], eax
    

    ... and some information that the address 0 must be replaced by the address of myVariable.

    This means only the addresses must be replaced when one of 200 files changes.