Lesson 4, Introduction to Problem Solving
Part I Decisions

We have seen how we can send data to the CPU and have the ALU area do some math for us.  Well, the ALU can do one more thing for us.  It can also compare two numbers or two groups of text (we call them text strings) and tell us whether they are the same or not.  How it tells us this is by placing a 1 in the accumulator if they are the same or a 0 in the accumulator if they are not the same.  For example, if we send the numbers 8 and 10 to the ALU to see if they are the same it will return a 0 to the accumulator because they are not the same.  If we input 8 and 8 and send them to the ALU then it will return 1 because they are the same.  The program below in the CPU simulator is comparing the numbers 8 and 10.  Just like in the other programs we ran in the simulator the first thing that must be done is store the input numbers in memory then send them to registers in the CPU. Then we'll have the ALU compare the numbers and places the answer in the Accumulator.  The last step is to move the answer to memory where we can use it.  Walk through the lines of instruction below and see how they move the data around and come up with an answer.

P1 and P2 sends the two numbers down to memory blocks M1 and M2.
P3 and P4 sends the two numbers up to CPU registers R1 and R2
P5 sends the numbers in Registers 1 and 2 to the ALU to compare (answer is stored in the accumulator)
P6 move the accumulator value to memory block M3

When we use an upper level language, like Visual Basic, to represent the above assembly program we will have to do more than just output a 1 or a 0 (humans won't know what that means).  Remember, when we do our output for the humans (document.write...) the output part is not represented in the CPU simulator program.  It really can't show us anything more than a mathematical result.  When we write a program we need to add an output statement so we can see the result of our program.  When we program this problem in VB Script we are going to create a decision structure to "decide" what output will be shown.  This will let us know whether a 1 or a zero is returned from the ALU.  The decision structure we will be using is the If...then...Else structure.   What we will be saying is If it is true (a 1 is returned, meaning the two numbers are the same) Then output the word "Same", Else output the words "Not the Same".  As you will see the syntax is pretty simple.  Let's code!

We also want to add a little functionality to this program.  Instead of the numbers being in our code (hard coding them) lets use a function that will allow our program to ask the user for two numbers.

To do that we will be using a new function called Inputbox().  A function is one of the two procedures we can use in programming (the other is called a subroutine).  A function takes in one or more values (arguments) and returns a value. When you think of a function think of the "Sum" function you use in MS Excel.  You select a bunch of cells and tell Excel to use the Sum function to add up the values in those cells.  What is really happening is that each of those numbers are sent to the CPU then ALU to have them added together then the result is placed in another cell (where we store that formula).  That's a function! 

Here is the syntax for the Inputbox function:

InputBox(prompt[,title][,default]) There is one required argument (Prompt)
and two optional arguments (title and default).  You only have to provide
the required argument. For this class I only expect you to use the "prompt" 
argument.
 
Parameter Description
prompt Required. What question do you want to ask the user?
title Optional. The title of the dialog box. Default is the application name
default Optional. A default text in the text box (what answer do you want in the box when it is first shown.

Open Notepad and type the following (don't type the '<---italic part of the lines):

<Script Language = "VBScript">
DIM M1, M2       '<-- tells the operating system to create two places in memory called M1, M2
M1 = Inputbox("What's the 1st number you want to compare?")      '<-- Input box asks for a number
M2 = Inputbox("What's the 2nd number you want to compare?")'      then the number is stored in M1 or M2
If M1 = M2 then    '<-- sends the values to the ALU then test to see if it's a 1 (true) or 0 (false)
    document.write "Same"  '<--If it's true then this line is interpreted and "Same" is written on the page.
Else
    document.write "Not the Same" '<--If the numbers are not the same then this message is displayed.

End If  '<-- We tell the interpreter that our decision structure is over. We must end the "If" whenever we start one.
</Script>  '<-- Ends our program

We don't really have to use M1 and M2 to indicate places in memory.  Another way to indicate places in memory is use words that describe what is stored there.  Since we want to store a "first number" and a "second number" we will replace M1 with "firstnumber" and M2 with "secondnumber" (you can not have any spaces in the names we use for memory).   The text we want to display should also be in memory so we'll make another place called "TheAnswer".  Then after we have finished making our decision and storing the proper response in "TheAnswer" then we output the answer at the end of the program.  This way seems like more work but it actually organizes the program a little better by displaying the answer at the end of the program where we would expect it to occur rather than hidden within our decision.  It's better to organize your program consistently so that if a year later, someone told you to go back and change the way the answer is displayed you wouldn't have to go digging around the code trying to find all the places where the display might pop out and just go to the end of the program and changing it just once in the output statement.  I'm also adding something new "joining" values from memory and text.  We do this to make our output more understandable.  Instead of just outputting "Same" or "not the same", let's show them what values they typed in initially too.  We use the symbol ampersand (&) to join things.  Our output code will now be:

 document.write "I compared " & firstnumber & " and " & secondnumber & " and they are " & TheAnswer 

We are joining the text "I compared " with the value that's stored in the variable (place in memory) firstnumber then join spaces and text " and " to them.  Then join what is stored in the variable secondnumber, then join the text and spaces " and they are " to them and finally join what is stored in the variable TheAnswer with everything we have joined so far.  This might sound complicated but once you run the code below it will make a lot of sense to you.

<Script Language = "VBScript">
DIM firstnumber, secondnumber, TheAnswer     '<-- tells the operating system to make three places in memory called firstnumber, secondnumber, and TheAnswer
firstnumber = Inputbox("What's the 1st number you want to compare?")             '<-- Same as before with just a new
secondnumber = Inputbox("What's the 2nd number you want to compare?")            ' way to get data
If firstnumber = secondnumber then              '<-- sends the values to the ALU then test to see if it's a 1 (true) or 0 (false)
    TheAnswer = "Same"   '<--If it's true then this line is interpreted and "Same" is placed in memory block called TheAnswer
Else
    TheAnswer = "not the same"  '<--If the numbers are not the same then this message is placed in memory block called   TheAnswer
End If        ' <-- We tell the interpreter that our decision is over
Document.write answer & "<p>"  '<-- joins what is stored in TheAnswer with a paragraph tag (we use the "&" to join things)
document.write "I compared " & firstnumber & " and " & secondnumber & " and they are " & TheAnswer  ' <-- more joining
</Script>

Notice that we use quotes " " for text, the names of places in memory without quotes, and ampersands "&" to tell the VB to join whatever is on each side of it together.

This is the last time you will be using Notepad for your code.  From now on you will be using html kit for coding.  The next lesson will provide that software to you.

Home