Week 10

 This week I found an interesting problem that was exactly like a problem I did in my class this semester, csc 230, but instead of mips I did it in java. The problem is fizzbuzz where you take a number and if the number is a multiple of 3 print fizz and it its a multiple of 5 print buzz and if its a multiple of both print fizzbuzz. There are two method one(first one) is the one that was in a video where I discovered the problem and the second one is me doing it the way I thought of. The problem was pretty easy but the way it did intrigued me because It was different than mine his felt much more clever than mine. What he does is creates variable fizz and buzz so when fizz equals 3 and buzz equals 5 we reset and add to the arraylist but its cool that he does this by using variables to keep track of the fizz and buzz. So fizz for the first 10 will look like this 0,1,2,0,1,2,0,1,2,0,1 for buzz its 0,1,2,3,4,0,1,2,3,4,0 at all zeros after the first zero will add the respective word. 

For my method the thought process is much more simple I use the mod operator, but first I count from 1  to the input number and check mod of 15,3,5 and they do their respective tasks. Funny enough leetcode says my code is faster and uses less memory so thats cool.

But the main reason I wanted to share this problem was not only to show the new way he showed me but also to show the mips way of doing it, which is much harder since the language is not as convenient.

So the two pictures at the very bottom is the mips code that will execute fizzbuzz. I'm gonna try my best to explain the code at the bottom. The first pic is of the main where I will loop though my method file that is linked and will print fizz,buzz, or fizzbuzz. The first 3 lines of code will system call the message at the top and will print "Enter an integer", the next 5 lines will take in the integer, store that integer in register t1 and will store a 1 in register t5, which will be used to loop. The "loop:" is a label that will label a specific address (beginning of the loop) beq is branch if equal so if t1 and t5 are equal go to exit which is another label that will be outside the loop and terminate the program when it is done. If t1 and t5 arent equal it will jump to my linked file after it jumps back it will add 1 to t5 and j (jump) back to "loop" (address) until t5 and t1 are equal and we terminate. Looking at the linked file(second pic) we take the register that is being incremented till it is equal to t1( our goal) and divide it by 15 and in mips there is a "hi" and a "lo" hi is the remainder and lo is the answer. Since we want the remainder we take the hi and put in the register t6 from there we check if the reminder is zero( meaning it is a multiple) if it is we go to the first exit which prints fizzbuzz and jumps back to the main file. If it doesn't equal zero which means its not divisible by 15 we go to the next operation which is dividing by 3, we the proceed to check if its reminder is zero and send to the second exit which prints fizz and jumps back to main. We do the same thing with 5, but after 5 if it doesn't exit using the others we assume that it is divisible by none of the numbers and just print the number we are on and jump back to main. We jump back and forth till t1 and t5 are equal and the program ends. Hopefully, you understood my explanation of the mips code if you dont understand mips and can see the cool differences between the java code and the mips code.

URL: https://www.youtube.com/watch?v=_Gof448HzAI&list=PLU_sdQYzUj2keVENTP0a5rdykRSgg9Wp-&index=81





Comments

Popular posts from this blog

Week 9

Week 11