How to count the number of instructions?

I have several * .s build files. I would like to calculate how many instructions they contain. For example, to count all the mul commands that I do:

cat *.s | grep -P '\t'"mul" | wc -l

which gives me the composite number of all mul commands.

I would like to have an output like:

mul: 893
add: 12054
sub: 2356
...

The problem is that there is no instruction table supported by the target platform. Each target platform has a different set of instructions. Supported instructions must be derived from existing assembly files. Can I do this in Bash only without Perl / Python foundry?

+3
source share
5 answers

:

grep -hP '^[ \t]+[a-z]*' *.s | sed 's/^[ \t]\+\([a-z]*\).*/\1/' | sort | uniq -c

: , , SiegeX.

:

1 call
6 mov
1 push
1 ret
2 syscall
2 xor
+5

, ( ), awk 1- :

awk '/^[[:space:]]+/{a[$1]++}END{for(op in a){print op,a[op]}}' /path/to/*.s

$ cat asm.s
section .text
global _start, write
write:
  mov al,1 ;write syscall
  syscall
  ret
_start:
  mov rax,0x0a68732f6e69622f
  push rax
  xor rax,rax
  mov rsi,rsp
  mov rdi,1
  mov rdx,8
  call write

exit: ; just exit not a function
  xor rax,rax
  mov rax,60
  syscall

$ awk '/^[[:space:]]+/{a[$1]++}END{for(op in a){print op,a[op]}}' ./*.s
push 1
xor 2
ret 1
mov 6
syscall 2
call 1
+5

Bash 4 - :

declare -A c;while IFS=$'\n' read -r t;do [[ ! $t =~ ^[[:space:]] ]] && continue;w=($t);((c[${w[0]}]++));done<asm.s;for op in ${!c[@]};do echo $op ${c[$op]};done

, while for f in *.s while done : done < "$f" while done : done < <(cat *.s) for f.

, :

declare -A c;while read -ra t;do [[ $t ]]&&((c[${t[0]}]++));done<asm.s;for op in ${!c[@]};do echo $op ${c[$op]};done

, , .

+1

, , SiegeX awk Perl

perl -n '/^\s+(\w+)/&&$a{$1}++;END{print map{"$_ $a{$_}\n"}keys%a}' ./*.s

", , , " (-:

0

grep -Eo '\ w +'/source.file | uniq -c

0

Source: https://habr.com/ru/post/1777484/


All Articles