This time, we will introduce a more comprehensive assembly language which is also done completely by hand.
The architecture is known as the "Little Man Computer", it too is in decimal (base-10).
The little man computer is a model designed to assist in the understanding of the fetch/execute cycle, through the visualization of elements of the computer architecture that would normally remain hidden.
The story is the suppose we have a "little man" trapped in a room with 100 mailboxes.
We also have two mailboxes at the other end labelled
INBOX
and OUTBOX
which are used for receiving and outputting data to the outside world.In the center of the room, there is a work area containing a simple two function (addition and subtraction) calculator known as the Accumulator and a reset-able counter known as the Program Counter.
Architecture Specifications
So, we have 100 memory cells (the mailboxes) each can contain 3-digit numbers from -999 to +999.The basic algorithm for the instruction cycle is given by:
- check the Program Counter for the mailbox number that contains a program instruction
- fetch the instruction from the mailbox with that number
- Increment the Program Counter (so that it contains the mailbox number of the next instruction)
- decode the instruction (includes finding the mailbox number for the data it will work on)
- fetch the data from the mailbox with the number found in the previous step
- execute the instruction
- store the new data in the mailbox from which the old data was retrieved
- Repeat the cycle or halt
As with the CARDIAC system, an instruction looks like
OAA
where the first digit specifies the opcode (the operation to be carried out) and AA
contain the address which we need to work with.There are roughly 10 operations for the little man computer, similar to the CARDIAC system.
In general, any assembly language will have a similar grouping of instructions into several families:
- Arithmetic: arithmetic instructions may operate on all registers or on just a special register (e.g. accumulator).
- Control: stuff like "copy contents of register 1 into register 2", loading to and storing from the accumulator, "jumps" like "goto", etc.
- Register-addressing method: these are indirectly loading values into registers, very important for theoretical computer scientists...not so important a distinction for the working coder.
- Input-output: usually present in all models.
Now, let us list off the instructions for the little man computer (taken from wikipedia).
Numeric code | Mnemonic code | Instruction | Description |
---|---|---|---|
1xx | ADD | ADD | Add the value stored in mailbox xx to whatever value is currently on the accumulator (calculator).
|
2xx | SUB | SUBTRACT | Subtract the value stored in mailbox xx from whatever value is currently on the accumulator (calculator).
|
3xx | STA | STORE | Store the contents of the accumulator in mailbox xx (destructive).
|
5xx | LDA | LOAD | Load the value from mailbox xx (non-destructive) and enter it in the accumulator (destructive). |
6xx | BRA | BRANCH (unconditional) | Set the program counter to the given address (value xx). That is, value xx will be the next instruction executed. |
7xx | BRZ | BRANCH IF ZERO (conditional) | If the accumulator (calculator) contains the value 000, set the program counter to the value xx. Otherwise, do nothing.
|
8xx | BRP | BRANCH IF POSITIVE (conditional) | If the accumulator (calculator) is 0 or positive, set the program counter to the value xx. Otherwise, do nothing.
|
901 | INP | INPUT | Go to the INBOX, fetch the value from the user, and put it in the accumulator (calculator)
|
902 | OUT | OUTPUT | Copy the value from the accumulator (calculator) to the OUTBOX.
|
000 | HLT | HALT | Stop working. |
DAT | DATA | This is an assembler instruction which simply loads the value into the next available mailbox. DAT can also be used in conjunction with labels to declare variables. For example, DAT 984 will store the value 984 into a mailbox. |
Mnemonics
In the Little man computer, there are these exotic things called "mnemonics". It works sort of like macros in C/C++...but not really.For example, we could refer to data locations using mnenomics:
INP STA FIRST INP STA SECOND LDA FIRST SUB SECOND OUT HLT FIRST DAT SECOND DATThe
STA FIRST
uses FIRST
as-a mnenomic, and it is defined as-a DAT
.In short, a mnemonic is a short sequence of characters (e.g.
FIRST
or SECOND
) that are an alias for an opcode (e.g. DAT
).Labels
This is the last innovative feature which differentiates Little Man from CARDIAC. We use labels to indicate portions of our program...you could think of this sort of like defining procedures.Well, to be more precise, a label can be used to identify a particular instruction as a target for a
BRANCH
instruction ("where the procedure begins").We can do more! We can also have "variables", that is associate data to a sequence of characters. We have seen this with the
FIRST
and SECOND
example code.An example program:
INP LOOP SUB ONE // This address will be called LOOP, // Subtract the value stored at ONE OUT BRZ QUIT // If we are at 0, then jump to the location QUIT BRA LOOP // We are not at 0, go back to the start of LOOP QUIT HLT ONE DAT 1 // Put the value 1 in this mailbox, // and call it ONE (variable declaration)Now what are some exercises?
Exercises
- Write a program that finds the first 100 prime numbers.
- Modify this program to take a positive number from the user
n
, and find the firstn
prime numbers. - Implement a
mul
procedure to multiply integers together. - Write up code to implement a floating point arithmetic division procedure (see Donald Knuth's Art of Computer Programming vol. 2 for details).
- Write a procedure to write a given number in a given base (e.g. write 720 in base 3).
Future Directions
Maybe next time I will begin discussing the MIXAL, theMIX
assembly language designed by Donald Knuth.
No comments:
Post a Comment