DEV Community

0xTomas
0xTomas

Posted on

Building a MIPS Decoder

Background

As the capstone for my CS104: Computer Architecture course in Codecademy's Computer Science path, I decided to attempt an implementation of an MIPS decoder. According to the official documentation, "The MIPS32 architecture is based on a fixed-length, regularly encoded instruction set" and supports the execution of high-level languages. The decoder would accept standard MIPS commands, such as 'sll $t4, $t0, 5' or 'sb $t1, 0x1001000c($t0)' and then return both the binary and HEX format of the command.

While the program itself has no application to the real world, I found it an incredibly useful exercise in developing my coding skills, namely defining functions and converting integers to binary, as well as furthering my understanding of the format of MIPS instructions. It also allowed me to investigate how values are stored in registers, learn how to pad a string with 0s to a fixed length, and the purpose of importing 'os' and 'sys' to call on the 'exit' and 'clear' functions.

Program Output

Image description

Python Code

The Python code was forked from an existing Git Hub repository in order to understand it in detail, as it was a challenging exercise to approach without any guidance.

There are three main elements:

  1. instruction_decode: Accepts the parameter 'instruction', which is the first element of the user input, and takes the form of the MIPS code such as sb or sll. It determines and returns the function type and opcode

  2. register_decode: Accepts the parameters 'function type', 'instruction' and 'argument', which returns an array in the form [rs, rt, rd, shamt] for r-type functions or [rs, rt, immediate] for i-type functions. Each value within the array references an index reference.

  3. mips_decoder_main: Receives input from the user in the form of a MIPS instruction, converts the code into an instruction to be passed into instruction_decode and pulls the register values from register_decode. It then converts each element of array mentioned above into binary and hex, and returns the MIPS instruction.

Git Hub

Here is a link to the repository on Git Hub: https://github.com/0xTomas/CS104-Computer-Architecture/tree/main/MIPS_Decoder

Conclusion

I honestly found this task a challenge, and certainly need support in the form of a model answer. It was, however, very useful to deconstruct code that I did not understand and attempt to draw out its various functions. I managed to understand each element, and while I would not have been able to write the code myself, I was able to add comments throughout the program that explain each step.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
0xapotheosis profile image
0xApotheosis

A journey of a thousand miles begins with a single step.

👋 Kindness is contagious

Found this article valuable? Consider leaving a ❤️ or sharing your thoughts!

Got it!