I have written and cross compiled a small c++ program, and I could run it in an ARM or a PC. Since ARM and a PC have different instruction set architectures, I wanna to compare them. Is that possible for me to get the number of executed instructions in this c++ program for both ISAs?
-
1Usually, you can either disassemble (`objdump` with GNU binutils tools) or use a program like [`size`](https://en.wikipedia.org/wiki/Size_%28Unix%29). Answers depend on compiler, compiler version, command line arguments, output format, etc. – artless noise Jun 26 '15 at 15:34
-
you will find that same code same target same compiler will have different results based on optimization and debug and other compiler options, and then as you change versions of compilers (same code same target) even more variation, and then when you change targets it varies again. Basically you should end up with hundreds of different numbers if instructions needed for the same source code across the two targets. – old_timer Jun 26 '15 at 19:40
-
1Do you mean a) size of program in instructions, or b) number of instructions executed when the program runs? For example, a little loop could have a small (a) and a large (b). – Mike Dunlavey Jun 28 '15 at 00:54
-
@MikeDunlavey Actually I meant b), the number of instructions :D – Cong Wang Jun 28 '15 at 13:08
-
Then you want the answer from @VAndrei – Mike Dunlavey Jun 28 '15 at 13:24
4 Answers
What you need is a profiler. perf would be one easy to use. It will give you the number of instructions that executed, which is the best metric if you want to compare ISA efficiency.
Check the tutorial here.
You need to use: perf stat ./your binary
Look for instructions metric. This approach uses a register in your CPU's performance monitoring unit - PMU - that counts the number of instructions.

- 5,420
- 18
- 43
Are you trying to get the number of static instructions or dynamic instructions? So, for instance, if you have the following loop (pseudocode):
for (i 0 to N):
a[i] = b[i] + c[i]
Static instruction count will be just under 10 instructions, give or take based on your ISA, but the dynamic count would depend on N, on the branch prediction implementation and so on.
So for static count I would recommend using objdump, as per recommendations in the comments. You can find the entry and exit labels of your subroutine and count the number of instructions in between.
For dynamic instruction count, I would recommend one of two things:
- You can simulate running that code using an instruction set simulator (there are open source ISA simulators for both ARM and x86 out there - Gem5 for instance implements both of them, there are others out there that support one or the other.
- Your second option is to run this natively on the target system and setup performance counters in the CPU to report dynamic instruction count. You would reset before executing your code, and read it afterwards (there might be some noise here associated with calling your subroutine and exiting, but you should be able to isolate that out)
Hope this helps :)

- 655
- 6
- 22
objdump -dw mybinary | wc -l
On Linux and friends, this gives a good approximation of the number of instructions in an executable, library or object file. This is a static count, which is of course completely different than runtime behavior.

- 4,512
- 1
- 30
- 46
-
This objdump based command just print the count of disassembly lines which gives an approximation of instructions in the binary, not executed instructions(it not include into account loops, branches, ...) – Zskdan Jun 15 '17 at 12:30