I've been tasked to generate a benchmark program that estimates the MIPS of an x86 system using C. My approach is run an empty for loop for a large amount of iterations. I will then measure the execution time of this to determine the MIPS. However, I need to know the number of instructions found in a single for loop iteration.
#include <stdio.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
size_t max_iterations = 1000000000;
// grab start time
for(int i = 0; i < max_iterations; i )
{
// empty
}
// grab end time and calculate MIPS
printf("MIPS = %f\n", max_iterations * instruction_per_cycle / 1000000.0 / elapsed_sec);
return 0;
}
I'm unfamiliar with the x86 instruction set, however, for the for loop I've provided it seems like the following items could be instructions:
- load value i from memory to register
- load value max_iterations from memory to register
- perform comparison between i and max_iterations
- increment i
- write new value of i to memory
- jump into loop assuming
- jump back to start of loop statement
CodePudding user response:
Things I did to view the disassembly, which probably should help you get what you need...
I wrote a simple function with a mundane for
loop in it's body, and saved to a file for.c
void loop()
{
for(int i = 0; i < 10; i )
{
// empty
}
}
Then I ran
gcc -S for.c
which in turn is to ask gcc to emit the assembly code, and the resultant assembly code is generated in for.s
. After which I ran as
(GNU Assembler) asking it to produce the object file for.o
with the following command
as -o for.o for.s
which generates the object file for.o
, and further to which I asked the utility objdump
to show me the disassembly of the object file using the following command...
objdump -d for.o
which shows me an output like this...
for.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <loop>:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
b: eb 04 jmp 11 <loop 0x11>
d: 83 45 fc 01 addl $0x1,-0x4(%rbp)
11: 83 7d fc 09 cmpl $0x9,-0x4(%rbp)
15: 7e f6 jle d <loop 0xd>
17: 90 nop
18: 5d pop %rbp
19: c3 retq
But this also has instructions related to stack as I wrote the loop inside a function. Typically, only for for
loop will be fewer instructions than what we see currently in the disassembly.
x86_64 architecture that would be where I ran all these, and used gcc to compile. So, please pay attention to the tools that you are using.
There may be other ways to achieve the same, but for now I can suggest this way, if it helps you.