-
Notifications
You must be signed in to change notification settings - Fork 9
/
disasm-cmp
executable file
·105 lines (83 loc) · 1.9 KB
/
disasm-cmp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl
if (@ARGV < 3) {
print STDERR "disasm-cmp <bits> <decompiler> <file>\n";
exit 1;
}
my $bits = (shift @ARGV) + 0;
my $deco = shift @ARGV;
my $file = shift @ARGV;
die unless -x $deco;
die unless -f $file;
open(A,"./$deco '$file' -$bits |") || die;
open(B,"ndisasm -b $bits '$file' |") || die;
sub ours_filter($) {
my @a = split(/ +/,@_[0]);
my $ret = '',$x;
# address
$ret .= sprintf("0x%04X ",hex(shift @a));
# hex opcodes
while (@a > 0 && length($a[0]) == 2 && $a[0] =~ m/[0123456789abcdef]{2}/i) {
$ret .= shift @a;
}
$ret .= " ";
# pad
while (length($ret) < 40) {
$ret .= " ";
}
# instructions
while (@a > 0) {
$x = uc(shift @a);
# $x =~ s/(DS|ES|FS|GS|SS):\((BYTE|WORD|DWORD|QWORD)\*\)/$1:/;
$x =~ s/0X(0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)/0x$1/g;
$ret .= $x;
$ret .= ' ';
}
return $ret;
}
sub nasm_filter($) {
my @a = split(/ +/,@_[0]);
my $ret = '';
# address
$ret .= sprintf("0x%04X ",hex(shift @a));
# hex opcodes
$ret .= shift @a;
$ret .= " ";
# pad
while (length($ret) < 40) {
$ret .= " ";
}
# instructions
while (@a > 0) {
# if ($a[0] =~ m/(BYTE|WORD|DWORD|QWORD)/i && $a[1] =~ m/^\[/) {
# shift @a;
# next;
# }
$x = uc(shift @a);
$x =~ s/0X(0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)/0x$1/g;
$ret .= $x;
$ret .= ' ';
}
return $ret;
}
my $line_A,$line_B;
while (1) {
$line_B = <B>;
next if $line_B =~ m/^ +-[0-9a-f]+/i;
$line_A = <A>;
last if (!$line_A && !$line_B);
chomp $line_A; # our decompiler
chomp $line_B; # NASM decompiler
# for comparison purposes massage both so they line up
# example:
# + OURS:0x0000 8B 04 MOV AX,DS:(WORD*)[SI]
# NASM:00000000 8B04 mov ax,[si]
$line_A = ours_filter($line_A);
$line_B = nasm_filter($line_B);
if ($line_A ne $line_B) {
print ">>>>>>>>>>>>>------------------------------\n";
}
print "+ OURS:$line_A\n";
print " NASM:$line_B\n";
}
close(B);
close(A);