Thursday 28 February 2013

C/C++ Puzzles: PART - 24

Changing the Output Without Changing the C Program

Consider the Simple program given below.

#include<stdio.h>

int main( )
{
   printf("\n1\n");
   printf("\n2\n);
   printf("\n3\n);
}

The Output of the Program is Shown Below



The task is to get the output shown below without making any change in the "C Program".


Solution:-

  The idea is to assemble the C Program into assembly file and make changes there. To learn step by step compilation of C Programs in Linux please visit my previous post. I have shown the process in the below. 
  • Let the C Program be named as a.c.
The gcc -S a.c will give you the assembly file a.s. The assembly file is shown below.


  • Edit the file a.s. The changed program is shown below.
  • Now compile the a.s file by typing the command gcc a.s. The output file a.out is generated.
  • Run the program by typing ./a.out. The Output is shown below.

This Post also gives you an idea about optimizing C Programs by making changes in the Assembly language file.

No comments:

Post a Comment