Assembler Example
The Assembler process enables a user to avoid calculating jump
addresses, remembering Op Codes, and deciding where to put data. In all
cases, all the user need do is type the code with symbolic names for
jump points and data and the Assembler will turn the result into a good
machine language program.
The process begins
with the user clicking on the Assembler menu and
choosing the Show Editor option. The user will then see the editor
window shown to the right here.
For our example assembler program we will write a program that will
read in a value from the user and will add up the numbers from 1 to
that value
and print out that sum. If the number is 0 or less, it will always
print out 0.
This will require us to use two pseudo-ops for instructions- READ
(which gets translated into LOAD 11110) and WRITE (which gets
translated into STOR 11111) as well as data items for containing the
current value (which will decrease by 1 down to 0 as we compute the
sum), a place for the sum, and a place for the value 1. We also use
comments which are indicated by a #. Everything after the # on a given
line is simply a comment and is disregarded by the Assembler.
Note Data items are of the form:
One: 1
and labeled instructions are of the form:
Loop: Instruction
Here is the
program. Note that there are lots of comments which pretty much
completely describe what the program does. Also, we have tabbed lines
over so that they look "pretty" and so it is easy to see the labeled
statement and the labeled storage locations.
Note that One is given the value 1 to start with and Value and Sum are
given the value 0. For Value, the 0 is meaningless since pretty much
right away our program puts a new value there. For Sum, the value is
also meaningless since we set the value to 0 by the LOAD Sum/SUB Sum
instructions that start the program. The reason for doing this is
because after you run the program once, Sum will no longer be 0 and if
you run it again you need to make sure it starts out at 0 in order to
properly add up the numbers.
Notice that One, however, stays 1 no matter how many times you run the
program. Nothing in the program will affect that memory location.
The loop shown here (a countdown loop) is a very common way to do
repetetive operations in Assembler/Machine Code programs.
Once you have typed the program in, you click on Assemble and another
window pops up to show you if you have any errors.
We see that there
are some problems. The window tells us that there is no such
instruction as STR at line 4 (and, of course, there isn't). We meant to
type STOR. It also tells us there is no label or number Ones in line 8
(and, of course, there isn't). Instead, we wished to type One.
We should now go ahead and change the editor window so that line 4 and
line 8 are correct. If we do so we will now find that when we click the
Assemble button the Error window
says (No errors). This means two things. First of all, there were no
errors so it assembled correctly and, second, the corresponding machine
code has been entered into the memory in the SimHYMN simulator.
Note that the program runs from memory locations 00000 to 01011 and the
three data items Value, Sum, and One are in memory locations 01100,
01101, and 01110 respectively in the SimHYMN simulator shown below.
Assembled Program.
Finally, here is the assembled program run with value 6 typed in by the
user.
Assembled/Executed
program