Search This Blog

Disclosure

Everything Tech Review is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. Amazon and the Amazon logo are trademarks of Amazon.com, Inc, or its affiliates.

Wednesday, June 15, 2022

My dive into Computer Architecture (Quick look at MIPS and the MARS Simulator)

MIPS CPU

Intro and Overview:

 While taking 2 courses on Computer Architecture (lab and lecture) with MIPS, I decided to compile the best resources that I could find and share some fun things I learned and built! 

I also built several interesting programs including a calculator, implemented bubble sort, and much more... I will share all the code at the end of the semester! 

MIPS is a RISC processer (reduced instruction set), the opposite would be CISC (complex), with processers such as the Intel Core and AMD Ryzen CPUS. 

The practical course primarily dealt with integers, which makes life way easier. We coded in the MIPS Mars Simulator (which has some cool tricks to it). Towards the end of the theoretical course we did cover Floating Point Conversions and Booth's Algorithm for multiplication. 

Prerequisites: 

Courses: 

Firstly, Digital Systems is a requirement for this course: 

  • You need to understand all the basic logic gates, registers, ALUs, conversion between bases, sign magnitude ,2s complement, Boolean addition, subtraction, and multiplication. 

Next, I advise you to take an Intro to Computer Science course before starting a Computer Architecture course.

  • This will allow you to understand the bigger picture in addition to assisting you with programming in the MIPS language. 

Additional subject matter:

Math Required:

  • Thankfully these courses don't require a high level of math, with that being said... you should know your powers of 2...
    • including 2^0 and the negative exponents.
    • this is because we are constantly using binary digits and need to convert easily 
  • Basic addition, long division, multiplication. 
    • Mainly for converting between different bases.

The Fun Part:

I came with prior coding experience but never dealt with assembly or anything remotely close to it before. I enjoyed learning about the instruction set and converting the logic I had previously into a program with assembly. For example, a for loop looks quite different than it would in C++ or Java.
        
        li $t0, 10 #run loop 10 times 
        li $t1, 0 #init counter 
loop: #loop through all items
beq $t1, $t0, result #reached end
#do action
addi $t1, $t1, 1 #increment counter
                j loop #jump back to loop
        result:
                #perform action after ending the loop

Thankfully, MIPS is a reduced instruction set processer (referred to as RISC, even though MIPS might not technically be a RISC compatible processer), so there are a handful of commands that you will often use vs. thousands on a CISC (complex instruction set computer). While the code might look foreign now, there isn't much of a learning curve.

In addition, it was interesting to learn how the data-path and a computer works on the most basic level, even to just add two integers together. In addition, they had a lot of optimizations. Such as if you wanted to check that one number is equal to another... on the basic level, you just subtract one number from the other... and if the result is a 0, then they are equivalent.  

Another note - The lecture portion: 

There are two segments to the computer architecture class... a lab and lecture. The lab involves programming in assembly and submitting code as assignments. The lecture is on the architecture, how the data-path works, etc. 

For example, what happens when a command gets executed on the hardware level. This is more abstract and not-hands on. Nonetheless, it is quite important to learn how these micro-processers work. 

Cool trick below:

Using the MARS Simulator, I was able to visualize how the internals of the MIPS chip would work (it is an oversimplification of the data-path but still helpful). If you don't have the tool installed, I linked to it further down in the article (under resources). 

ALU Control (for math operations)
ALU Control (for math operations) - also for BEQ/BNE
Registers (RF)
Registers (RF)

 If you want to try it yourself:

Click Tools -> MIPS X-Ray -> Connect to MIPS (button) -> Assemble -> Run. 

This compiles your program, and executes the first line... then you can witness what happens on a basic level for a single cycle processer. By clicking Run again, it will move on to the next command for you to witness. 

Now you can click on certain components such as Registers, Control, and ALU Control to see what happens for each command.


Datapath of single cycle
Datapath of single cycle

A cool tidbit I found was that when you use branch conditionals (essentially an if statement), it compares the two registers via subtracting one from the other. If the result is zero, they are equal! 

bne $t1, $t2, skip #branch not equal

If whatever is in $t1 = $t2, then branch to the skip label... Otherwise, it falls through to the next command (which is the else condition). 

Otherwise, they aren't equivalent and the BEQ won't run (or the BNE would). 
Think about checking if 4 = 4, but because it compares values in registers ... it would have to do 4-4=0 to determine equivalence.  

In code, it would look like:

addi $t1, $zero, 4
addi $t2, $zero, 4

beq $t1, $t2, exit 

Essentially the MIPS processer doesn't have space to compare two immediate values in a branch command. It needs enough space to store two registers (each 5 bits) and a 16 bit immediate value which is the label we want to jump to. Lastly, it still needs 5 bits for the operation code (to determine what command it is). 

That makes up 32 bits which is the maximum size for the command. There is a pseudo-command to compare 2 immediate values (such as 2 integers), but it boils down to adding each number to a register and then does the standard branch command. 

You can see how it would look below: 

Branch command in MIPS
Branch command in MIPS



ALU in MIPS
ALU, RF, and Control in MIPS

This is just a simple example of how the engineers had to think in a backwards-unconventional way to convert something that humans can do in a matter of seconds to something a computer would understand and run efficiently. In the lecture and via the books, you will learn and see what other challenges they faced and how they solved them! 

Resources: 


The MIPS Green Sheet is amazing, some courses let you even take it to the test with you (but not a calculator). Essentially it is a 2 page paper with all the major commands (most of them are not pseudocode), the register numbers, possible instruction types (I,J, R) and their formats. 

My other blog post on the subject!


For practical lab-work and programming this is a good guide for starting out: https://learnxinyminutes.com/docs/mips/ 




Compilers/Simulators:

You can use this cool tool to quickly convert a C or C++ program into MIPS Assembly Code (on the web)! 
- I'm using it with mips gcc 11.2.0, there are other versions of the mips gcc compiler in addition to some RISC ones! 

Another popular tool is the MARS Simulator which runs on Java and works for Windows and MacOS. There are other tools but they aren't as easy to use... MARS also has some cool tricks up it's sleeves (mainly for Single-Cycle)... 

Books: 

In addition, I would like to recommend this book "Computer Organization and Design MIPS Edition" , which aided me a lot in computer architecture. Note that in some editions, they have a chapter on Pipelining and others remove that chapter and replace it with Multi-Cycle... My course required us to know both, so it was a bit annoying to have to get another edition of the book. 

The newer versions usually skip multi-cycle and go directly to pipelining but check the table of contents before purchasing or downloading.

Please ask your professor if they are going to cover Pipelining and/or/neither Multi-Cycle and then you can make your decision based off of that. 

- the book authors also have presentation slides which your school might utilize.

See MIPS Run is also a popular addition.

Note, that Digital Systems was a prerequisite for this course, so you should also read this (before the MIPS stuff!): 

Introduction to Logic Circuits & Logic Design with VHDL (VHDL is a net bonus and the course I took didn't require it, I study CS and not Engineering which is why). You can skip those parts in the chapter if you wish!

You can purchase the books online, borrow from a library, or find some of them online. 

Slides:

While researching for the course, I found many college slide decks on each topic that helped immensely... I'd like to highlight certain institutions which did a stellar job of this: 

FSU MIPS Slides - clean design and very helpful


Videos:

Here is a playlist of the best videos I found covering coding in assembly as well as the theocraticals:


Code:


and I plan on making some more of the code from the course public and open source in the near future, in the meantime please follow my profile on twitter and github to be updated! 



No comments:

Post a Comment

Thank you for posting a comment, it will be reviewed and then posted shortly.