Code Generation (Object Code)

The optimized TAC is now used to generate assembly code.

The assembly code is converted into object code in a process  known as assembly.

This object code contains machine instructions corresponding to the source code, but may not be fully functional for execution as yet.

Consider the following example in source code in a file “main.c” :

#include <stdio.h>
//function prototype omitted for simplicity
int find_sum(int num1, int num2) {
    return num1 + num2;
}

int main() {
    int num1, num2;
    
    printf("Enter the first number: ");
    scanf("%d", &num1);

    printf("Enter the second number: ");
    scanf("%d", &num2);

    int result = find_sum(num1, num2);
    printf("The sum of %d and %d is %d\n", num1, num2, result);

    return 0;
}

The machine code for the main and find_sum function is generated, and stored in the object file, main.o . Since scanf and printf is used but exist in other libraries, the compiler creates code know as references (placeholders) to call scanf and printf, but doesn’t include the implementation of of scanf and printf in in the main.o object file. The creation of main.o was the final step of the code generation.

Now we move on to the last phase to use a linkers combine  various bits of object code to create one executable file.

© 2024  Vedesh Kungebeharry. All rights reserved. 

Leave a comment