I'm trying to convert a disassembled Hex Dump to a SPIM friendly assembly file. This requires that I take lines like:

004000f8 <_init>:
  4000f8:	27bdffe8 	addiu	sp,sp,-24
  4000fc:	afbf0010 	sw	ra,16(sp)
  400100:	0c100064 	jal	400190 <call_gmon_start>
  400104:	00000000 	nop
  400108:	0c100094 	jal	400250 <frame_dummy>
...

and make them:

.text 004000f8 
<_init>:
       addiu	$sp,$sp,-24
       sw	$ra,16($sp)
       jal	400190 <call_gmon_start>
       nop
       jal	400250 <frame_dummy>
...

How I do this is I define starting features and terminating features. I then just search for them using a big if statement within a loop that traverses each word in my document. I insert into 3 vectors the starting position, the length of the "sentence", and the type of sentence. I then reiterate through the vector full of words and copy the sentences as specified by the vectors into a new file. I'm having a lot of trouble getting this to work and I hate using so many if statements. 3 for every terminating feature (there are 4 features) + another feature for a second type of "sentence". How do I do this in a more precise way? I know that there will always be 13 different kinds of lines to be added to my new file.

Edited 6 Years Ago by dansnyderECE: n/a

Do you mean your input is a file? If so, why not read each line in and parse it instead of reading the whole file content and then parse? Nevertheless, you still have to use switch or if-else statement to parse each of operator that SPIM has, or you may have a text parser (may use flex) to parse it for you before hand, and then you convert the parsed result to SPIM friendly format.

EDIT: Just look at the source again, how about you remove certain portion of text you do not want in each line? The portion you do not want seems to be those 2 sets of hex values in front of each operator. You just need to deal with the special case on the first line. The rest would be the same.

Edited 6 Years Ago by Taywin: n/a

That's very true. That may be a better way to go about doing this. How do I go about storing a line as a string? I can just use a string operator to toss out the first however many characters.

OK, just searched on Google and I found this link showing you how to read a file in line by line as string. Not sure if it is the same C++ compiler you are using... Once you get each line, you should be able to manipulate with it easily. If you still have trouble, please post your problem again.

This article has been dead for over six months. Start a new discussion instead.