CoderRightInTheProgram
CoderRightInTheProgram

Reputation: 127

Need to write a MIPS assembler in C or C++, looking for some design suggestions

I'm needing to write a MIPS assembler in C/C++. Before I start just writing some code, I think I should actually take some time and do some planning first. There is about 15 MIPS instructions I need to account for, include J, but not JR. The program needs to take in a file that has .text,.data, and .word sections along with labels, then output a file with the first line being in decimal with the number of instructions and the number of words of data. The rest is the machine code encoded in hex. The final set of lines consists of hexadecimal values representing the initial values of the words in the data segment. I know I'll need to do 2 passes to first parse the labels and JUMP instruction. Basically I'm just looking for advice on how to setup the data structures. Should I do an array of strings that hold the OPCODE, the RS, RT, RD, etc... then convert that to hex somehow? Or is there a better way to do this from someone that has any advice/experience? Thanks for your help/suggestions!

Upvotes: 4

Views: 4044

Answers (1)

Aniruddha
Aniruddha

Reputation: 358

I actually did this a long time ago for something related to a class project! You're right about having to do 2 passes. However, don't use an array of strings for the registers. In fact you don't need to use strings at all. You can put the OPCODE in an enum, and the registers in an enum. For 15 instructions, you can easily do most of the work by handcoding switch-case, if-else statements rather than designing a fully generalized solution. It might be tempting to use regular expressions, but for your problem it's not worth the effort (though you should definitely use any opportunity you get to learn regex if you have the time!). Then use hashmap-like structures to map between the registers and OPCODE and their HEX values, and use those. You can do any address calculations directly in code. This is just a suggestion, you should definitely experiment. My main point is that if you are reading a string, you shouldn't store it in the same form when you can process it first and store something (read: object) more meaningful.

Basically, you only need the first pass for the labels etc. You can do everything else in the second pass. If you look at the basic typical compiler/assembler flow chart in any O/S textbook, you can easily emulate each step - that's what I did.

Hope this helps!

Upvotes: 1

Related Questions