SPOT Programmer's Guide

Back to SPOT Home Page

Contents

Purpose

Writing SPOT Programs

Input Files

Strings

The stack

Wordsize

 

 

Purpose:

It is difficult to give students a sense of how computers really work without the experience of working with machine or assembly language, but this is missing from modern introductory Computer Science programming curricula. 

Typical real assemblers do not have easy interfaces and therefore cannot be taught at the high school level without a lot of unnecessary work to overcome the obscurities of working with them.  They were developed for professionals and not students.  The learning curve for real instruction sets is high because there is no runtime error checking (such as using unassigned register values) which leads to great frustration in debugging -- a very bad misfeature when one is trying to build enthusiasm for programming.

SPOT was developed specifically for teaching.  It has a stripped-down instruction set (under 40 instructions) and a number of technical details of real machine programming have been removed.  On the other hand, its runtime checks make common mistakes easy to fix, it can handle strings and elementary file reading, so more interesting problems can be handled and the program output can be made descriptive.

It is also portable (written in Python) and tied to a web page interface, so it overcomes the typical installation problems by being server-based, making use of only a typical browser interface.  There are no special requirements for the client browser's features, and therefore it can be used by anyone (we think).

It is also reasonably close to actual assembly languages.  For comparison, see the actual instruction set for the Intel 8086 processor (the grand-daddy of Windows-based PCs today).

Writing SPOT Programs

The full Instruction Set is described here. Programming is done by typing the instructions into the emulator's text area or copying the program into the text area from another source using the operating system's clipboard.  The syntax can be checked immediately, and the program run, with a limitation on the total number of instructions executed to prevent infinite loops.

Cards: Data, in the form of integers and strings, is stored by the programmer on "Cards".  One datum (number or string) per Card -- storing a datum on a Card erases that Card's previous contents.  Cards are named C0, C1, etc. (there is no highest number).  Cards are SPOT's version of CPU registers although there is no word-size limitation on the size of numbers that can be stored on a single Card, and Cards can store strings.

Details:

  • One instruction per line
  • Everything (except for strings) is case-independent
  • blank lines are allowed (and encouraged for readability)
  • comments start with the # character and extend to the end of the line
    # this is a comment
    write 2,c3  # writes the value 2 to C3
  • Labels are on lines by themselves and end with the character ":", e.g.
    A_Label_Here:
  • Instructions have 0 to 3 arguments.  For instance, ADD C1,C2,C3 adds the values in C1 and C2 and places the result in C3.
  • Programs start at the first executable instruction and proceed until:

    a) A STOP instruction is executed
    b) The maximum allowed number of instructions is executed (this number can be set)
    c) An execution error occurs (for instance, dividing by zero, or using the value of a Card that has not been set)

Input Files:

SPOT can read specially formatted input text files: the so-called "CSV"-type files (Comma-separated-values).  Each line of such a file contains a sequence of values separated by commas.  For instance:

"Isaac","Newton",1642
"Galileo","Galilei",1564
Albert,Einstein,1879

In a particular file, all lines must have the same number of comma-separated values (3 in the case above).  For the SPOT Emulator to recognize a file as a valid input file, its filename must have the suffix: ".spotdata".  The file must be placed into the same directory on the server that contains the SPOT emulator and all of the web page files (including this one).

SPOT has a special set of commands to deal with reading these input files:

READ-PROP 1,C2  (This will write the first value ("Isaac") onto Card 2)

READ-PROP 3,C5  (This will write the third value (1642) onto Card 5)

NEXT  (This will move to the next line in the input file -- the one with "Galileo" above)

REWIND  (This will move to the first line in the input file)

JUMP-IF-EOF some_label  (This will skip to the instruction following the label "some_label" in the program if we moved beyond the last line of the input file with a NEXT instruction)

Strings

SPOT has minimal support for strings -- the only purpose of strings is to allow more descriptive output than just numbers.  All you can do with strings is place them in Cards and print them out with Say.  Example:

write "Peter",C1
say "Hello, there "
say C1

On the other hand, normal assembly languages have no support for strings, and so this minor variation should not bother purists much, but definitely help the student produce understandable output.

The stack

Several commands use the processor's internal stack, and this requires explanation.  The commands are:

push
pop
gosub
return

The stack is an area in memory used for storing temporary information.  It cannot be addressed directly, but data can be stored there using the PUSH and GOSUB commands, and that data can be retrieved using the POP and RETURN commands.  

The stack is a first-in-last-out list.  It has a top and a bottom.  The top is the value most recently PUSH'ed onto the stack.  The bottom is the value pushed onto the stack longest ago.  Think of a stack of dishes at a restaurant on a spring-loaded carrier.  You put plates onto the stack, and then remove them in reverse order (you remove the most recently stored plate first).  

Assume that the stack is empty -- contains no stored data.  We can push a value onto the stack using the PUSH command.  Suppose the value in Card 2 is 12.  Then PUSH C2 will push the value 12 onto the stack.  Suppose the value in Card 3 is 5.  Then PUSH C3 will push the value 5 onto the stack.  The stack now has the values 5 and 12.  POPing the stack removes the value from the top of the stack and places it into a card.  Therefore POP C6 will place the number 5 into Card 6.  And then POP C7 will place the number 12 into Card 7.  Then the stack will be empty again.

Therefore, an easy way to exchange the values in Cards 1 and 2 would be:

push c1
push c2
pop c1
pop c2

GOSUB PUSHes the location of the next instruction onto a different stack -- the program stack.  Return POPs that location off that stack and causes the program to start executing at that location.  This is a handy way to allow subroutine entry and exit, where a subroutine may call another, deeper subroutine, which may call a deeper subroutine, and then the RETURNs must be executed in reverse order (a property of stack POPing).

Wordsize

Setting the wordsize to one of the options provided in the dropdown list on the Spot Emulator page restricts all calculated values to the number of bits specified (unless the "no limit" option is chosen).  This allows SPOT to emulate the arithmetic performed by real processors, including older, smaller ones with wordsizes as small as 4 bits.  The following is a list of some of the Intel microprocessors and their (register) wordsizes:

Intel microprocessor Wordsize
4004,4040 4
8008,8080 8
8088,8086 16
80286,80386,Pentium 32

Values can easily overflow after arithmetic operations.  For instance, with a wordsize of 4 bits, the valid values range from -8 to +7.  In fact, adding 1 to 7 results in -8, and subtracting 1 from -8 results in 7.
Wordsize Minimum value Maximum value
4 -8 = (-2**3) 7 = (2**3-1)
8 -128 = (-2**7) 127 = (2**7-1)
16 -32,768 = (-2**15) 32,767 = (2**15-1)
32 -2,147,483,648 
= (-2**31)
2,147,483,647 
= (2**31-1)
64 -9,223,372,036,854,775,808
= (-2**63)
9,223,372,036,854,775,808
= (2**63-1)

 

Creative Commons License
This work is licensed under a Creative Commons License.
All material related to the SPOT processor may be copied freely, however the author must be attributed.