#!/usr/bin/python """ * Author: Tiago Maluta * Info: www.coding.com.br * Usage: $ python stats_opcode.py file * How to generate assembly (.s) output? 1) gcc -S hello.c 2) gcc --save-temps hello.c // will create more file, check 'man' * Licence: Public Domain """ import os import sys try: output = open(sys.argv[1],"r+").readlines() except: exit(-1) inst = [] # instructions list for line in output: if not line.startswith('\t.') and line.startswith('\t'): if not line.startswith('\t@'): inst.append(line.split('\t')[1].replace('\n','')) D = {} for i in inst: D[i] = inst.count(i) total_instructions = len(inst) unique_instructions = len(D.keys()) print "Your program has %s instructions (%s unique)" % ( total_instructions, unique_instructions ) print "+-------+-------+---------------+" print "|Opcode\t| Times\t| Average(%)\t|" print "+-------+-------+---------------+" for opcode in D.keys(): print "|%s\t| %s\t| %0.2f %%\t|" % ( opcode, D.get(opcode) , float(D.get(opcode)) / total_instructions * 100) print "+-------+-------+---------------+"