 This time we're going to be building a program that can do some arithmetic for us. We've already got some basic information set up for us. We've got some integers in T0 through T3 and I've got a new line string up here that I can use to separate some of my results. In each of these cases I'm just going to do one arithmetic operation and then display the results to the console. So I'll start with an add instruction and because I just want to print out the results I'm not going to do anything further with my arithmetic. I'm just going to store the result in A0 immediately which will allow me to print that result out pretty easily. For this first example I'm just going to add the contents of T0 to the contents of T3. So this statement is telling me that register A0 gets the value of register T0 plus T3 which should be 2 plus 8. Now since I just want to print this integer out I will put 1 into V0 and make a system call from that. So it should print out 2 plus 8 which is 10. Now I'll print out the end line and then I can try another arithmetic operation. This time I'll try an add immediate instruction. Again I'll store my results in register A0 and I'll start with T2 and I'm going to add on the constant 17. So this should take contents of T2, add 17 to them and store the results into A0. So I'll have 5 plus 17 which is 22. Then I can print out that result as well. We'll come back in a minute and add some more operations here but for the moment I'll go ahead and save this and test our code. So it printed out 10 because we had 2 plus 8 and then it printed out 22 because we had 5 plus 17. A subtract instruction will work pretty much the same way. I'll store my results into A0 and this time I'll calculate contents of T0 minus the contents of T1. So A0 will equal T0 minus T1 which is 2 minus 7. If I go ahead and run that then I get negative 5 as we should expect. Next we'll try looking at a multiply and a divide instruction. So our multiply instruction takes two registers. It will multiply their contents together and then store the results into the high and low registers. So for this example I'll multiply T1 by T2 and since that can give me a 64 bit result those values will go into two registers instead of just one. But since I know I really just have small numbers I'm only going to worry about the lower 32 bits of my result and I'll copy those into A0. So really I know that A0 is T1 times T2 then I can print out that result. Now if I go run this code it prints out 35 which is 7 times 5. I could ask it to print out the upper 32 bits as well. I can use the move from high operation to copy those upper 32 bits into A0 but as we'd expect the upper 32 bits are zeros. A divide instruction is pretty similar. I can use the div instruction to divide 7 by 2. So if I just wanted to print out the quotient I'd use the move from low operation again. If I'd like to see the remainder as well I can use the move from high operation. So this time I did 7 divided by 5 and I got a result of 1 remainder 2. If I change that and I do say 7 divided by 2 then I get 3 remainder 1. Well the MIPS architecture actually provides a molten div instruction as we've used them here. There are a couple of popular pseudo-instructions that allow you to simplify some of this writing. So instead of writing two instructions to do a multiplication or a division operation I can't occasionally get by with one. So I could have used the mole pseudo-instruction instead. In this case this will do exactly the same thing as our previous two instructions. And in fact the assembler will actually expand my mole pseudo-instruction into these two instructions. So this will work fine in any case where I have a small result. Something that only needs 32 bits. So again I got 35 out. If I want to do division I can actually use a div pseudo-instruction but provide it with three arguments instead of just two. And to get the remainder I can use the REM pseudo-instruction. And this is equivalent to doing a modulus operation in other high-level languages. So I'd already seen that my mole pseudo-instruction produced 35 just the same as my original two multiply instructions. Now I've got the three and the one that I expected from doing integer division and looking for the remainder as well. In this case my instructions are a little bit redundant because the machine will end up doing the integer division twice. Once for the div pseudo-instruction and then once for the remainder. Where before all I really needed to do to get the remainder was to use the move from high operation. The results of the integer division were still in the high and low registers so I was able to just grab that out. But down here with the remainder pseudo-instruction the assembler doesn't know that those results are still available to it. But the mole div and REM pseudo-instructions can make your life a bit easier because you don't have to remember to write two instructions every time when you really just want to do a simple operation. Instead the pseudo-instructions give you the same format that you have with some of the simpler arithmetic instructions like addition and subtraction.