@@ -7,10 +7,11 @@ b mips_disassemble
77
88#include <stdio.h>
99#include <stdint.h>
10+ #include <unistd.h>
1011
1112#include "mips.h"
1213
13- int disassemble (uint32_t insword , uint64_t address , enum MipsVersion version , char * result )
14+ int disassemble (uint32_t insword , uint64_t address , MipsVersion version , char * result )
1415{
1516 int rc ;
1617 Instruction instr ;
@@ -39,42 +40,72 @@ int disassemble(uint32_t insword, uint64_t address, enum MipsVersion version, ch
3940 exit(-1); \
4041 }
4142
43+ void usage (char * * av )
44+ {
45+ printf ("usage:\n" );
46+ printf ("\t%s [instruction_words]\n" , av [0 ]);
47+ printf ("\t%s test\n" , av [0 ]);
48+ printf ("example:\n" );
49+ printf ("\t%s 3c028081 68435a50 24445a50 6c830007\n" , av [0 ]);
50+ printf ("\t%s test\n" , av [0 ]);
51+ exit (-1 );
52+ }
53+
4254int main (int ac , char * * av )
4355{
4456 char instxt [4096 ];
57+ uint32_t insword = 0 ;
58+ uint64_t baseaddr = 0 ;
59+ int instindex = 0 ;
60+ int c = 0 ;
4561
46- if (ac == 1 ) {
47- printf ("usage:\n" );
48- printf ("\t%s [<address>] <instruction_word>\n" , av [0 ]);
49- printf ("\t%s <instruction_word>\n" , av [0 ]);
50- printf ("\t%s test\n" , av [0 ]);
51- printf ("examples:\n" );
52- printf ("\t%s 0 14E00003\n" , av [0 ]);
53- printf ("\t%s 00405A58 14E00003\n" , av [0 ]);
54- printf ("\t%s test\n" , av [0 ]);
55- exit (-1 );
62+ while ((c = getopt (ac , av , "a:" )) != -1 )
63+ {
64+ switch (c )
65+ {
66+ case 'a' :
67+ baseaddr = strtoull (optarg , NULL , 0x10 );
68+ break ;
69+ default :
70+ usage (av );
71+ goto cleanup ;
72+ }
5673 }
5774
58- if (ac == 2 && !strcmp (av [1 ], "test" )) {
75+ if (optind >= ac )
76+ {
77+ usage (av );
78+ goto cleanup ;
79+ }
80+
81+ instindex = optind ;
82+
83+ if (ac == 2 && !strcmp (av [1 ], "test" ))
84+ {
5985 disassemble (0x14E00003 , 0 , MIPS_32 , instxt );
6086 ASSERT (!strcmp (instxt , "bne\t$a3, $zero, 0x10" ));
61- disassemble (0x14E00003 , 0x405a58 , MIPS_32 , instxt );
87+ disassemble (0x14E00003 , 4 , MIPS_32 , instxt );
6288 ASSERT (!strcmp (instxt , "bne\t$a3, $zero, 0x405a68" ));
6389 exit (0 );
6490 }
6591
66- uint64_t address = 0 ;
67- uint32_t insword = 0 ;
68- if (ac == 2 ) {
69- address = 0 ;
70- insword = strtoul (av [1 ], NULL , 16 );
71- }
72- else if (ac == 3 ) {
73- address = strtoul (av [1 ], NULL , 16 );
74- insword = strtoul (av [2 ], NULL , 16 );
75- }
92+ while (instindex < ac )
93+ {
94+ insword = strtoul (av [instindex ], NULL , 16 );
7695
77- if (0 == disassemble (insword , address , MIPS_32 , instxt )) {
78- printf ("%08llX: %08X %s\n" , address , insword , instxt );
96+ if (0 == disassemble (insword , baseaddr , MIPS_32 , instxt ))
97+ {
98+ printf ("%08llX: %08X %s\n" , baseaddr , insword , instxt );
99+ }
100+ else
101+ {
102+ printf ("%08llX: %08X ??\n" , baseaddr , insword );
103+ }
104+
105+ baseaddr += 4 ;
106+ instindex ++ ;
79107 }
108+
109+ cleanup :
110+ return 0 ;
80111}
0 commit comments