9.04.2011

Compilation and execution of programs

Compilation and execution 

  • There are many steps involved in converting a C program into an executable form, these all steps are called "build process".
  • Build process are representing in the following graphically diagram : 
            
            C Source code Programs(assume file name first.c)

                   Expanded source code(first.i)

                      Assembly code(first.asm)


     Relocatable Object code + Object code of library function

                     
                 Executable code(first.exe)
Figure : Build process


If you don't understand the build process, don't worry,read following explanations of build process i.e. how a source program make executable program. It is all internal process of machine,that's done in neno seconds.


Lets now understand the steps mentioned in above figure in detail.
  1. Editor-Type your program in editor(source code).
  2. Preprocessing-During this step, the C source code is expanded based on the preprocessor directives like as #include, #ifdef, #define etc. The expanded source code is stored in an intermediate file with .i extension.
  3. Compilation- The expanded source code is then passed to the compiler, which identifies the syntax error in the expanded source code. If the expanded source code is error free, then the compiler transfer the expanded source code in C, into an equivalent assembly language program. The assembly code is typically stored in .ASM file. So our first.c file would be changed and stored in first.ASM.
  4. Assembling - Assembler translate the .ASM program into Relocatable Object code. Thus assembler translate our first.asm file into first.OBJ. .OBJ file is one of the binary file. This object file contained header and several section.
  5. Linking - Linking is the final step of build process i.e. creating an executable program. It's do following works :
    • Find definition of all external function
    • Find definition of all global variables
    • Combine Data Section
    • Combine Code Section
  6. Loading - Once the .EXE file is created and stored on the disk,it is ready for execution. when we execute it, it is first brought from the disk into the memory (RAM) by an OS component called Program Loader.

7 comments:

  1. can any one explain output of program?
    main()
    {
    int x,arr[8]={11,22,33,44,55,66,77,88};
    x=(arr+8)[3];
    printf("%d",x);
    }

    ReplyDelete
    Replies
    1. It gives 0 as output.

      Explanation:
      I think you may aware of arrays,
      though your array name arr should has the address of index 0 (arr[0]).
      let assume your array address starts from 100. so arr[0] & arr is 100.
      you increment that location by 8.

      hence (arr+8) evaluated and has the result 108.

      you like to assign 3 rd index next to 108.
      there is no data at that location so it returns 0.

      if you like to confirm that plz give the value less that 5 instead of 8.

      x=(arr+3)[3]; ----> 77
      ( note that the array value starts from 0). 3 index next to 4th,

      Hope you have an idea.

      Delete
    2. Hi karthick,
      x=(arr+8)[0];output is garbage value
      x=(arr+8)[1];output is garbage value
      x=(arr+8)[2 or 3 or 4 or -----] output is 0
      what is the reason?

      Delete
  2. Hi karthick,
    x=(arr+8)[0];output is garbage value
    x=(arr+8)[1];output is garbage value
    x=(arr+8)[2 or 3 or 4 or -----] output is 0
    what is the reason?

    ReplyDelete
    Replies
    1. you didn't understand my explanation which i had given above...
      arr+8 -> This address it self contains only the null value.
      So you got garbage value as output when using 0. the only value.

      then how can you get an index from this,
      there should not be any value.

      try to assign the value to the (arr+8)[0] or .. (arr+8)[n] ,
      then assign the same index to x.

      eg,
      char arr[8]={'1','2','3','4','5','6','7','8'};
      //arr[7] has the value 8. last value of array.
      (arr+8)[4]='5';
      int x=(arr+8)[4];
      printf("array: %s\n",arr);
      printf("new variable : %c",x);

      then print it it should the value which you assigned.
      But this type of usage is not good, you should declare then you should use it.

      do more experimentation on the array index,

      Delete
  3. #include
    #include
    void main(){
    int num,i=0;
    num=++i+ ++i;
    printf("%d",num);
    }
    please explain how the answer becomes 4?

    ReplyDelete
    Replies
    1. Hi,
      the statement num = ++i + ++i is an assignment statement,
      first the increment are done before the add operation.
      Hence this i was incremented twice. So it becomes 2.
      Then as per your statement num = i + i ;
      So it become 4 as result.

      if you want to see the difference ,
      use j instead of i at one end.
      int j = 0 ;
      num = ++i + ++j ;
      now see the difference.

      Delete