Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need help sorting som assembler code (instruction set profiling (static)), using awk.
A piece of the code looks like this....
44000040 <R_Do_Startup_DetectAndInitializeMemoryOrganization>:
44000040: b5f0 push {r4, r5, r6, r7, lr}
44000042: b08a sub sp, #40
44000044: 2000 mov r0, #0
44000046: 4669 mov r1, sp
44000048: 9001 str r0, [sp, #4]
4400004a: 7308 strb r0, [r1, #12]
4400004c: 7348 strb r0, [r1, #13]What I want to do with this code is to uniquely sort the instructions (strb, push, sub, mow etc) and present it like this
push "number of times push is present in this file"
strb "number of times strb is present in this file"
mov "number of times mob is present in this file"and so on
Greatly appreciate any help I can get
/RP

Based on the sample, this code processes only lines where word1 ends with a colon, and assumes word3 is the instruction.
awk '{
if (match($1,".*:"))
instr[$3]++ }
END {
for (i in instr)
printf " %-8s %4d occurrences\n",i,instr[i]
}' program.txt | sortmov 2 occurrences
push 1 occurrences
str 1 occurrences
strb 2 occurrences
sub 1 occurrences

Is there any easy way of adding the functionality of say counting 32 bits and 16 bits instructions
like this
44000046: 4669 mov r1, sp
4400003c: 44000c58 strmi r0, [r0], #-3160should give two parts:
16 bit instructions:
mov 1 occurences32 bit instructions:
strmi 1 occurencesRegards,
/RP

I also want to be able to sort out those that are numbers (just throw them away).
Rows like this:
4404c51c: 000003ac 420002e8 .........Should not be counted!!
Regards,
/RP

I don't know one set from the other. Let me know if there is some way to programmatically determine 16-bit from 32-bit.
The solution below takes a hard-coded approach where all 16-bit instructions are loaded into an array. But if one set is substantially shorter than the other, that would be the best set to identify and control on.
For elimination of numerical instructions, I eliminate those that begin with a digit. If there are any like mov32, it would not eliminate those.
For this solution, I had to eliminate the sort. You can see what a sort would do to the output below - it would sort the two groups together.
But you probably want each group sorted, so I will follow up with a solution for that, but I wanted to post this basic code first.
awk 'BEGIN \
{ array16["mov"]=1
array16["sub"]=1
array16["push"]=1
}{ if (!match($1,":"))
nextinstr=$3
if (match(instr,"^[0-9]"))
nextif (instr in array16)
instr16[instr]++
else
instr32[instr]++ }END \
{ print "16-bit instructions"
for (i in instr16)
printf "%-8s %4d occurrences\n",i,instr16[i]print "\n32-bit instructions"
for (i in instr32)
printf "%-8s %4d occurrences\n",i,instr32[i]}' program.txt
16-bit instructions
mov 2 occurrences
sub 1 occurrences
push 1 occurrences32-bit instructions
mov42 1 occurrences
strb 2 occurrences
str 1 occurrences

This solution adds 9 leading characters to the front of each line for sort control (assumming a maximum instruction length of 8). After the sort, these 9 characters are cut. For ease of understanding, I posted the unsorted
intermediate output followed by the actual final output.awk 'BEGIN \
{ array16["mov"]=1
array16["sub"]=1
array16["push"]=1
}{ if (!match($1,":"))
nextinstr=$3
if (match(instr,"^[0-9]"))
nextif (instr in array16)
instr16[instr]++
else
instr32[instr]++ }END \
{ print "A 16-bit instructions"
for (i in instr16)
printf "A%-10s%-8s %4d occurrences\n",i,i,instr16[i]print "B"
print "B 32-bit instructions"
for (i in instr32)
printf "B%-10s%-8s %4d occurrences\n",i,i,instr32[i]}' program.txt |
sort | cut -c10-A 16-bit instructions
Amov mov 2 occurrences
Asub sub 1 occurrences
Apush push 1 occurrences
B
B 32-bit instructions
Bstrb strb 2 occurrences
Bstr str 1 occurrences
16-bit instructions
mov 2 occurrences
push 1 occurrences
sub 1 occurrences32-bit instructions
str 1 occurrences
strb 2 occurrences

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |