Skip to content

Instantly share code, notes, and snippets.

@helospark
Created May 10, 2022 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helospark/51e8837711c7a4c351be51ab829b254a to your computer and use it in GitHub Desktop.
Save helospark/51e8837711c7a4c351be51ab829b254a to your computer and use it in GitHub Desktop.
Calculates which are most/least used x64_86 asm instructions
package com.test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Calculates which are most/least used asm instructions, use it like:
*
* objdump -M intel -d /usr/local/lib/*.so /bin/* /usr/bin/* | java /{source to file}/com/test/InstructionCounter.java
*
* For me:
* Least used instructions (each used just once):
* pfadd, vpmaskmovq, pfsub, vpdpbusd, vaddsubpd, vfnmsub213pd, hsubps, vfnmsub231ps, fstenv
*
* While the most commons:
* mov, call, lea, cmp, jmp, int3, je, nop, test, add, xor
*/
public class InstructionCounter {
public static void main(String[] args) throws FileNotFoundException, IOException {
Map<String, Long> instructionToCount = new HashMap<>();
InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader bufReader = new BufferedReader(isReader);
String line;
while ((line = bufReader.readLine()) != null) {
String[] segments = line.split("\t");
if (segments.length >= 3) {
String instruction = segments[2];
instruction = instruction.split(" ")[0];
Long count = instructionToCount.get(instruction);
if (count == null) {
count = 1L;
} else {
count = count + 1;
}
instructionToCount.put(instruction, count);
}
}
List<Entry<String, Long>> list = new ArrayList<>(instructionToCount.entrySet());
list.sort(Entry.comparingByValue());
for (var element : list) {
System.out.println(element.getKey() + "\t\t" + element.getValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment