SPOT Programming Examples

Back to SPOT Home Page

# The standard first program
say "Hello World!"
 
# Counting from 1 to 10
# Will output: 1 2 3 4 5 6 7 8 9 10
# Card 1 (C1) will be the counter
write 1, C1

TheLoop:
# Jump to the Finish label if C1 is greater than 10
jump-if-gt Finish, C1, 10

# Print out the counter
say C1
say " "

# add 1 to it and do it again
inc C1
jump TheLoop

Finish:
STOP
# Calculating factorials (this sample: 10!)
# C1 is the limit (10)
# C2 is the multiplier (1 through C1)
# C3 is the accumulator

write 10, C1
write 1, C2
write 1, C3

FactorialLoop:
jump-if-gt PrintAndFinish, C2, C1
mul C3,C2,C3  # multiply C3 * C2, put result into C3
inc C2
jump FactorialLoop

PrintAndFinish:
say C1
say "! = "
say C3
stop
 
# Using Gosub and Return for debugging output

# Suppose you want to print out the values
#   of the cards C1, C2 and C3 (they are your
#   working cards in the program), along
#   with a phrase that indicates where in the 
#   program you were when printing them out.

# Create a subroutine with a label and GOSUB
#   to that routine from whereever you wish to 
#   trigger the printout:

# here's the first part of your program:
write 12,c2
write 3,c1
mul c1,c2,c3
# put your location in C100
write "The first part:",c100
gosub Print123

# here's the second part of your program
#   with some other values of C1,C2,C3 that
#   you are curious about:
sub c2,c1,c3
inc c2
write "The second part:",c100
gosub Print123

stop

# Here's the printout subroutine
Print123:
say c100
say "\nC1="
say c1
say "\nC2="
say C2
say "\nC3="
say C3
say "\n\n"
return
 
 
# Reading and printing input data files
# Choose the input data file "FIRST", print out
#   the name and graduation date
# The "FIRST" file is of the form:
#    firstname,lastname,date-of-graduation

AnotherCard:
# read the first and second fields 
#   into Cards 1 and 2:
read-prop 1,c1
read-prop 2,c2
read-prop 3,c3
say c1
say " "
say c2
say " graduated in "
say c3
say "\n"

next
jump-if-eof TheEnd
jump AnotherCard

TheEnd:
stop
 
# Reading and calculating with Input Data files
# The inputfile "second" has format:
#    name,age,grade

# Find the average grade (make sure to skip 
#   the first line, which are the column headers)

# C1 will contain the count of lines
# C2 will contain the running total of grades

# BE SURE to choose the input file "second" in the
#   Input Data File dropdown

write 0,c1
write 0,c2

NextCard:
next
jump-if-eof Finish
inc c1
read-prop 3,c5
add c5,c2,c2
jump NextCard

Finish:
div c2,c1,c3
say "The average grade is "
say c3

# Exercise indexed read/write

# Read the first value on each line in a datafile 
#   into a sequence of cards
#   then print them out in reverse order
# Illustrates the use of 
#   indexed-read (Write-from-ind) and 
#   indexed-write (Write-to-ind)

# Choose any one of the datafiles which
# has a name as the first value on the line

# First: read the names from the datafile into cards 
# starting at C100 (then C101, C102, etc.)
write 0,c1

read-loop:
read-prop 1,c2
write-to-ind c2,100,c1  # write c2 to 100+c1
inc c1
next
jump-if-eof write-loop
jump read-loop

# Second: write out the names from the array of cards
# but working backwards (c1 already contains the 
# the count of names read in)
write-loop:
dec c1
jump-if-lt finish,c1,0
write-from-ind 100,c1,c2 # read from 100+c1 to c2
say c2
say "\n"
jump write-loop

finish:
stop
# Exercise PUSH/POP  (same as indexed read/write
#   exercise, but using PUSH/POP instead)

# Read the first value on each line in a datafile 
#   into a sequence of cards
#   then print them out in reverse order
# Illustrates the use of PUSH and POP

# Choose any one of the datafiles which
# has a name as the first value on the line

# First: read the names into cards starting at 100
write 0,c1 

read-loop:
next
jump-if-eof write-loop
read-prop 1,c2
push c2  # push c2 onto the stack
inc c1
jump read-loop

# get ready for writing
write-loop:
dec c1
jump-if-lt finish,c1,0
pop c2  # pop the next value off the stack into c2
say c2
say "\n"
jump write-loop

finish:
stop
# Effects of wordsize on numerical values
# Note: set the wordsize to 4 bits
# Let's count up in decimal and binary (notice
# the overflow after 7)

write 0,c1
write -8,c2

loop:
say c1
say " "
saybin c1
say "\n"
inc c1
inc c2
jump-if-lt loop,c2,7
 

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.