Next:Lesson 3
Up: Overview
Previous: Lesson 1
Lesson 2
Lesson 2: Composition
In the previous lesson, we learned about machine state and how to use registers, such as AX and BX. We learned how to fill them with data, using MOV for integers, and POINT for text. We also learned instructions for performing simple arithmetic, such as ADD and SUB. Finally, we also saw how to perform INPUT and OUTPUT, including using OUTPUT TEXT when dealing with 'text id's.
Now we would like to present few more instructions. At the start of the lesson, show off these new tools by name only, MULT, HALF, SQRT, RAND, APPEND. After these are introduced, we'll go over a few examples rather quickly, since they are not any more complicated that what we've already seen.
Have the computer calculate 17 * 29 and then output the result.
Now we see the advantage of using a computer to perform these simple operations, as they can do it quicker than most of us can.
MOV AX,17
MULT AX,29
OUTPUT
Which outputs 493.
Have the computer find what half of 31791 is, rounded down.
MOV AX,31791
HALF AX
OUTPUT
Which outputs 15895.
Point out that HALF only needs one register, which acts as both the value to take the root of and the storage location in which to put the result. This is an example of the syntax changing depending upon what instruction is being used. The next instruction also behaves the same way.
Have the computer calculate the square root of 19742, rounded down.
MOV AX,19742
SQRT AX
OUTPUT
Which outputs 140.
As you can see, these instructions which produce fractional results always round down to the nearest whole number, since Rex86 registers can only hold integer values, and nothing else.
Pick a random number between 1 and 1000 and output it. Do this a second time, to verify that they're random.
RAND AX,1000
OUTPUT
RAND AX,1000
OUTPUT
So MULT, HALF, SQRT, and RAND join our arithmetic instructions, along with ADD and SUB giving us a fairly varied toolset. Individually, the instructions we've seen can't accomplish too much. Each has their own respective use, and will perform some simple operation, but none are any more powerful than a common hand-held calculator. Learning how to combine instructions to do more complex tasks is essential to making useful programs.
Given a value in AX, calculate three times four more than that value.
Such an operation involves both addition and multiplication, so we'll have to use ADD and MULT. By rewriting the requirement as an algebraic expression, letting "a" represent the initial value of AX, we see that we want
answer=3*(4+a)
This means we need to add first, then multiply. The order of the instructions is very important then. There is a garuntee that the machine state is preserved between each instruction, and since the instructiosn are always executed sequentially, we can see the correctness of our ordering by thinking about the machine state.
ADD AX,4
MULT AX,3
And then the state dissection looks like the following:
--------------------------------
State AX Next instruction
0 a ADD AX,4
1 a+4 MULT AX,3
2 (a+4)*3 -
--------------------------------
Here state was only good for verifying our work. At other times it's crucial to getting an answer.
Given a value in AX, calculate 1+2+...+that value, using the triangular number formula.
If you're not familiar with it, the triangular number formula states that n*(n+1)/2 gives the sum of all numbers from 1 to n. So let's just use this formula, like we did for the previous problem.
ADD AX,1
MULT AX,AX ; wrong
HALF AX
Unfortunately, this doesn't work. Let's look a state table to see why.
----------------------------------
State AX Next instruction
0 n ADD AX,1
1 n+1 MULT AX,AX
1 (n+1)^2 HALF AX
2 (n+1)^2/2 -
----------------------------------
As you can see, this gives us (n+1)^2/2. The problem is that after the ADD instruction, in state 2, AX holds n+1, and we've lost the original value of n. Whenever this situation occurs, we have to preserve the original value at the earliest possible moment, using a MOV into another register.
MOV BX,AX
ADD AX,1
MULT AX,BX
HALF AX
This gives us the answer we were looking for.
Composing instructions is useful for more than just calculating formula's. It is also necessary to coordinate input and output, in order to create a dialogue that's accomidating for the user.
Have the user input a number, prompting them before hand.
To prompt the user, we simply output text telling them what we need. After we're done with that, we wait for input.
POINT AX,"Please enter a number:"
OUTPUT TEXT
INPUT
Again the order is very important. If it was rearranged so that INPUT was first, the machine would wait until it recieved some input before going on to display the prompt.
A new instruction, APPEND allows us to combine two pieces of text together, like so.
POINT AX,"abc"
APPEND AX,"def"
It combines well with our current library of text instructions to allow to create more complex operations.
Given a text id in AX, prepend the text 'don't forget to ' to it.
This requirement is much like the triangular number problem, since we'll need to shuffle some data between registers in order to get a solution.
APPEND AX,"don't forget to " ; wrong
Doesn't work, since that appends, not prepends.
POINT AX,"don't forget to " ; wrong
APPEND AX,AX
Won't work, since we lose the original text of the text.
One of the following two
POINT BX,"don't forget to "
APPEND BX,AX
MOV AX,BX
or
MOV BX,AX
POINT AX,"don't forget to "
APPEND AX,BX
Will work.
This ability to combine and manipulate text comes in handy when we want to communicate with the user. Having a program that behaves according to things it's told makes it much friendlier to use.
Prompt the user to input their name, then reply with 'hello, ' followed by their name
This is a simple matter of combining our "prompt for a number" code with the text manipulation we just learned.
POINT AX,"What is your name?"
OUTPUT TEXT
INPUT TEXT
MOV BX,AX
POINT AX,"Hello, "
APPEND AX,BX
OUTPUT TEXT
Demonstrate the principle of composition by drawing lines on this code to demonstrate the seperate pieces we combined together.
Also, notice that text can be inputted the same way it is outputted. Using INPUT TEXT waits for the user to input any piece of text, then stores its text id into AX.Homework
Using only the following already learned instructions
MOV reg,value set reg to value
ADD reg,value add value to reg
SUB reg,value subtract value from reg
MULT reg,value multiply reg by value
SQRT reg take the square root of reg
HALF reg take half of reg
RAND reg,value set reg to a random number between 1 and value
OUTPUT output the value in AX
OUTPUT TEXT output the text identified by AX
Write code that fulfills the given requirement.
"Randomly pick either 50, 60, 70, 80, or 90, and output it."
"A pinochle deck only contains 9, 10, J, Q, K, A. Letting 11 through 14 represent J through A, write code that randomly picks a pinochle card and output its number."
"Find the greatest square number that is less than or equal to the value in AX, and output it. For example, 7 makes 4, 28 makes 25, 11 makes 9"
"Given values in AX and BX, representing the lengths of two sides of a right triangle, calculate the length of the hypotenuse and output it"
"Just as how the OUTPUT instruction behaves like OUTPUT INTEGER, using INPUT without a type is the same as using INPUT INTEGER. With this in mind, for each of the lines of code, find whether the INPUT and OUTPUT instructions are being used correctly, or incorrectly, with regards to type. If something is being used incorrectly, identify which line could be changed in order to fix the code."
INPUT INTEGER
MOV AX,912
OUTPUT
INPUT
MULT AX,AX
OUTPUT INTEGER
POINT AX,"xyz"
ADD AX,7
OUTPUT
INPUT TEXT
APPEND AX,"!"
OUTPUT
INPUT
POINT AX,"ok"
OUTPUT TEXT
INPUT INTEGER
APPEND AX,"es"
OUTPUT TEXT
Next:Lesson 3
Up: Overview
Previous: Lesson 1
by dlong@progmatism.com. Plz don't copy kthx.