How do I determine the number of x86 machine instructions executed in a C program?

前端 未结 4 1751
感情败类
感情败类 2020-11-27 22:47

I\'m currently working on a homework problem that asks me to find out the number of machine code instructions that are executed when running a short program I wrote in C.

4条回答
  •  渐次进展
    2020-11-27 23:04

    You can use Godbolt's Compiler Explorer to compile your program and display the assembly code for various compilers and options.

    Then count the number of instructions for every fragment, ie: sequence of statements upto and including the first test.

    Then instrument you code: add a global variable instruction_count, initialized to the number of instructions in the main function epilog and increment this variable at the beginning of each fragment by the number of instructions you counted in the previous step. and print this number just before returning from the main function.

    You will get the number of instructions that would be executed by the uninstrumented program for whatever input is provided to the program, for a given combination of architecture, compiler and options, but not including instructions executed in library functions nor during the startup and exit phases.

提交回复
热议问题