Lesson 3, Part 4 More Math
Now that you have mastered the easy stuff, lets try something more difficult. In this problem you have been told that you need to write a program where you have 3 Gallons, 6 Quarts and 7 pints. They want your program to tell them how many total pints there are in that group. There are 2 pints in a quart and 4 quarts in Gallon. Now rather than randomly trying to code this lets break it down into what we will be doing: Inputting numbers, making calculations, storing the result (output). In addition, we need to make some assumptions in order for this all to work. Here's how we break it down:
Assumptions:
1. 4 quarts in a Gallon
2. 2 pints in quart
3. We will be given no other units than pints, quarts, and gallons
4. We will only be converting one group of fluids at a time.
Input:
1. Num Gallons (3)
2. Num Quarts (6)
3. Num Pints (7)
4. quarts per gallon (4)
5. pints per quart (2)
Calculations:
1. Gallons to quarts = Num Gallons * 4
2. Gallons to pints = Gallons to quarts * 2
3. Quarts to pints = quarts * 2
4. Total = Gallons to pints + quarts to pints + pints
Output:
1. Total
Now we know exactly what needs to be done. It puts everything in a nice package for us. It is also the order in which we will be doing our op codes and Operands so out job is very easy. Here it is:
Keep looking down:
Inp 3 M1
Inp 6 M2
Inp 7 M3
Inp 4 M4
inp 2 M5
MMR M1 R1
MMR M2 R2
MMR M3 R3
MMR M4 R4
MMR M5 R5
Mul R1 R4 <---3 * 4
MAR R6 <---move the answer (12) to register 6
MUL R6 R5 <--- 12 * 2
MAR R7 <---moves the answer (24) to register 7
MUL R2 R 5 <---6 * 2
MAR R8 <---Moves answer (12) to R8
Add R7 R8 <---- 24 + 12
MAR R9 <---Moves answer (36) to R9
Add R9 R3 <--- 36 + 7
MAM M6 <---moves answer (43) to Memory location 6
HLT
Notice that my op codes and operands are in the same order as how I outlined them in Input, Calculations, Output. Once you have broken down the problem into easy sections the rest is all typing. In the next lesson and all other lessons we will see that the more effort you put into creating a correct and detailed problem statement the easier the problem is to solve. It is the most important skill you will take from this class so be sure to learn it well. It will seem hard and confusing at first but if you put effort into mastering it you will be using that skill for the rest of your life.
VB Script: Okay, now that we did it using assembly language let's code it using VB script using HTML Kit:
The parts below that are in italics are my comments in the code so you can see what is happening. I don't expect them to be in your code.
Keep going:
<Script language=vbscript>
Option Explicit
DIM gallons, quarts,
pints,numberQuartsInGallon,numberPintsInQuart,totalQuartsInGallon,totalPintsInGallons,totalpintsinquarts,
gallonstoquarts, gallonstopints, quartstopints, total
'Instead of using M1, M2, etc I'm using a combination of words for my places
in memory to make my code easier to understand
gallons = 3
quarts = 6
pints = 7
numberQuartsInGallon = 4
numberPintsInQuart = 2
totalQuartsInGallon = gallons * numberQuartsInGallon
'<--- 3 * 4 = 12
totalPintsInGallons = numberPintsInQuart * totalQuartsInGallon
'<-- 2 * 12 = 24
totalPintsInQuarts =
'<---- 6 * 2 = 12
total = totalPintsInGallons + totalPintsInQuarts + pints
'<---24 + 12 + 7 = 43
document.write total
'<---Output of 43
</Script>
I could have done my calculations like this:
total = gallons * numberQuartsInGallon + numberPintsInQuart * totalQuartsInGallon
+ quarts * numberPintsInQuart + pints
That calculation would give you the correct answer but also points off for using a complex mathematical operation. The math above requires the computer to do the same calculations as my first code but you have made it impossible to debug the calculation if the answer comes out wrong. You also made it very difficult for someone to come after you and modify the code. You should NEVER do more than one type of mathematical operation in an equation.
M4 = M1 + M2 +M3.....Good
M4 = M1 + M2 + M3/3... Bad (both addition and division in the same equation).
Always use the KISS principle (Keep It Simple Stupid) :-)