Next:Lesson 2
Up: Overview
Previous: Syllabus
Lesson 1
Lesson 1: Arithmetic
The very first program for the course has the following requirement:
Print the number 7 on the screen
The absolute simplest way to do this is as follows
MOV AX,7
OUTPUT
Now would be a good time to explain the concept of a register. Do so by defining a register as a place inside the Rex86 machine that holds one, and only one, integer value. Emphasize this, that the only possible value that can be in a register is an integer. The value is held there for as long as needed, until something else changes it, or overwrites it completely. An example of such a thing is this first line of code MOV AX,7 which puts the value 7 into the register named AX. AX is a register provided by the Rex86 machine, always exists, and is always available.
Clearly, this second line OUTPUT outputs the value currently in AX onto the screen. In this case, 7.
The observant programmer may notice that OUTPUT is hardwired to always use AX. This is by design. By reducing the number of choices that the student has in creating this simple program, there are less concepts introduced, and less confusion as to what the correct answer may be. Simplifing the tools in this case creates a single straightforward, unambiguous answer.
Having OUTPUT hardwired to AX will also be useful in later lessons, when we will introduce the need to preserve register values, as well as conserve registers that are available.
The second program should be shown immediately after, by giving the requirement:
Have the computer calculate 10 + 6, and then print the answer to the screen
Here the concept of automation should be introduced. Do so by realizing that, of course, we could calculate this addition ourselves (if we had pencil and paper available), but the entire point of using machines in the first place is to reduce the amount of work we have to do. Telling the computer to perform the calculation itself lifts us of the burden, while also making more efficient use of processing time. Perhaps some student will object to writing a whole program to do something this simple. You may respond that learning how to do this simple example using a program teaches us about the tools to do similar calculations in the future, which may be much more complicated, and that we definately would not want to perform ourselves with pencil and paper.
The absolute simplest solution is
MOV AX,10
ADD AX,6
OUTPUT
This introduces a new instruction ADD. By comparing the english requirement to the Rex86 code, it's fairly obvious that the instruction takes the value in the register AX, and increases it by 6. Here is one of those cases of modifying a register based upon it's previous value as opposed to giving it an entirely new value.
This is a nice point to learn about the concept of state. At each point in time, the machine has a singlular state, including the register values, and what the next instruction to process is. The state is completely maintained, never changing, until an instruction is executed, and this will cause some form of calculation to occur, changing the machine state in a well-defined manner. This is no other cause of state change aside from executed instructions; no intrusion and no tampering. Hence the machine has a discrete progression of states, one after another, based only upon whatever processing the instructions perform, and nothing else.
Next present the following problem
Have the computer calculate 16 - 5, and then print the answer to the screen
By now this request should seem almost redundant. An ambitious student could easily guess to use "SUB", and this is indeed the correct answer.
MOV AX,16
SUB AX,5
OUTPUT
If (and only if) some smart alec points out that you could also use ADD AX,-5 then praise him for being very creative, but remind him that he shouldn't be too ambitious in performing work that the computer should be performing instead.
Here, the students should somewhat be getting the hang of how registers and simple instructions work. So, let's introduce a new concept to throw them off.
Wait for the user to input a number, then print the number that is 10 more than that on the screen.
Interactivity is very important for computers to have. A machine which operates all by itself, without ever listening to it's user, or changing it's manner of operation, gets boring very quickly. So here we are introducing a means by which the user can use their keyboard to get a number directly into the machine directly.
The instruction INPUT is the converse of OUTPUT. Instead of outputing the current value in "AX", it waits for input and then puts the inputted value into AX. This makes the solution:
INPUT
ADD AX,10
OUTPUT
Again, the entered value automatically goes into AX without it being specified. Invalid user inputs will result in AX becoming 0. Later on we'll learn of a way to test for this situation using code. (lesson 7, overflow)
Since AX is always used for input, the next problem is somewhat trickier.
Wait for the user to input two numbers, then print their sum.
Clearly this is impossible given the current repritoire of instructions. Inputting the second number will destroy the first, making us unable to add both values together. Luckily for us, we have more registers we can use. BX also exists in the Rex86 machine, independant of the AX register. Each has it's own value stored inside, and the machine state garuntees that they are each preserved between instructions unless something explicitly modifies them.
Since INPUT only puts values into AX, we will require BX to store the result of the first INPUT, so that we can INPUT the second time without danger of losing information. This results in the following solution:
INPUT
MOV BX,AX
INPUT
ADD AX,BX
OUTPUT
Though the following is also acceptable
INPUT
MOV BX,AX
INPUT
ADD BX,AX
MOV AX,BX
OUTPUT
It might be useful to show that these two code segments are equivilant. Use the concept of state to do so.
Notice here that we are using ADD in a new way. Instead of adding an integer value to a register, we are adding another register. This works by using the value currently in the register as an integer.
All of the arithmatic instructions so far have the form of <instruction> <register>,<value>, where <value> can be an integer value or any register. The order of the operands always goes <destination>,<source>, so that the register on the left is being modified.
Now that we have a firm grasp on simple integer arithmetic, let's try something different.
Print the text 'hello' on the screen.
This task cannot be done with integers alone. It's time to introduce a new data type of Rex86.
POINT AX,"hello"
OUTPUT TEXT
Recall that only integer values can be stored in AX. Clearly "hello" is not an integer value, it's a piece of text. So the POINT instruction takes this piece of text, associates a 'text ID' with it, and stores this 'text ID', which is a simple integer, into the AX register. There no garuntee as to what exactly this integer value may be, except that it directly corresponds to this text.
If only OUTPUT alone were used as the second line of code, it would output the integer value in AX, which has no actual meaning outside of the Rex86 machine. By specifying OUTPUT TEXT, we tell the machine to treat the value in AX as a 'text ID', and output the text it corresponds to instead of the value itself. This produces the desired result, "hello".
For those who may be curious, POINT will produce a different value for the 'text ID' each run of the program. This value may be random, but there is no garuntee. The technical details are somewhat complicated, and may be looked at in the appendix.Homework
"Given the following value in AX, and line(s) of code, write what the value of AX will be after the code is executed."
AX=713
ADD AX,82
;795
AX=1642
SUB AX,507
;1135
AX=994
MOV AX,485
;509
AX=206
ADD AX,AX
;412
AX=4961
MOV AX,AX
;4961
AX=3
POINT AX,"emptiness"
SUB AX,AX
;0
AX=844
SUB AX,11
;833
AX=1008
INPUT
MOV AX,19
;19
"The 'syntax' of code is the property of whether or not the code is correctly written, including the placement of spaces and puncuation. For each line of code, tell whether the syntax is correct or not. If it is not, identify what's wrong with it."
ADD AX,834
; correct
ADD 3,17
; incorrect, needs a register instead of 3
POINT AX,"347 apples, whoa!"
; correct
POINT AX,"$#&(@ apples!"
; correct
MOV AX,"9"
; incorrect, quotes are illegal
ADD AX 5
; incorrect, needs comma
MOV AX,6091
; correct
SUB AX,AX
; correct
POINT AX,58
; incorrect, need quoted text
ADDAX,7
; incorrect, needs space
POINT AX,"more text
; incorrect, text needs quote on right
SUB AX,42
; corect
OUTPUT 9
; incorrect, cannot use number
"You've seen how OUTPUT TEXT is used to output pieces of text. Similarily, OUTPUT INTEGER outputs integers. The default action, when only OUTPUT is specified with no type following it, is to output an integer, as all of our examples have shown. With this in mind, for each of the lines of code, find whether the OUTPUT instruction is being used correctly, or incorrectly, with regards to type."
MOV AX,759
OUTPUT INTEGER
; correct
POINT AX,"animals"
OUTPUT INTEGER
; incorrect
MOV AX,6
OUTPUT TEXT
; incorrect
INPUT INTGER
OUTPUT
; correct
INPUT
OUTPUT TEXT
; incorrect
Next:Lesson 2
Up: Overview
Previous: Syllabus
by dlong@progmatism.com. Plz don't copy kthx.