Skip to content
Permalink
Browse files
Tidied up to be used as little example
  • Loading branch information
James Shuttleworth committed Sep 15, 2021
1 parent 7afb70b commit e8d15ac829929c8162094329d08451d4b60be894
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 673 deletions.
@@ -0,0 +1 @@
*~
@@ -1,2 +1,42 @@
# subleq
SUBLEQ SIC/OISC
# SUBLEQ SIC/OISC

This is a simple interpreter for a single-insutruction computer.

The single instruction is SUBLEQ (subtract and branch if less-than-or-equal-to 0)

Example: subleq a b c
- Subtract value in memory address a from value in memory address b
- store result in memory address b
- if the value is less than or equal to 0, jump to c
- Programs are made of groups of 3 values (a, b, c)
- Code and data share the same memory
- A jump to -1 terminates
- A jump to -2 just moves forward to the next group of 3 (essentially means "no jump", just continue)
- Writing to address -1 outputs an ascii value

## Example:

If we have a piece of subleq code: `9 10 3 11 9 0 12 12 0 14 100 -1 0`

It would be executed like this:

```
9 10 3, 11 9 0, 12 12 0, 14 100 -1, 0
execute at 0
9 10 3, 11 9 0, 12 12 0, 14 86 -1, 0
execute at 3 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 15 86 -1, 0
execute at 6 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 15 86 -1, 0
execute at 0 (jumped!)
9 10 3, 11 9 0, 12 12 0, 15 71 -1, 0
execute at 3 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 16 71 -1, 0
execute at 6 (not jumping, just moving on)
9 10 3, 11 9 0, 12 12 0, 16 71 -1, 0
execute at 0 (jumped!)
9 10 3, 11 9 0, 12 12 0, 16 55 -1, 0
...and so on
```

Note that any instruction that doesn't jump is automatically followed by a jump forward by 3 to get to the next set of three values.

This file was deleted.

@@ -0,0 +1,25 @@
data="This is a test of the emergency broadcast system\n"
l=len(data)
ascii_vals=[str(ord(i)) for i in data]
ascii_string=""
c=0
for i in ascii_vals:
c=(c+1)%3
ascii_string+=i+", "
if c==0:
ascii_string+="\n"


ascii_string+="0"

code="""21, -1, -2,
19, 20, -1,
18, 0, -2,
17, 17, 0,
0, 0, 0,
0, 0, 0,
-1, 1, %i,
%s
"""%(l,ascii_string)

print(code)

This file was deleted.

@@ -1,23 +1,35 @@
prog = [ 21, -1, -2, #0
19, 20, -1, #3
18, 0, -2, #6
17, 17, 0, #9
#Example program
prog = [ 21, -1, -2, #0 #Print loc 21
19, 20, -1, #3 #Subtract 1 from pos 20, quit when 0
18, 0, -2, #6 #Add one to pos 0
17, 17, 0, #9 #Jump to pos 0
0, 0, 0, #12
0, 0, 0, #15
-1, 1, 13, #18
72, 101, 108, #21
108, 111, 32, #24
87, 111, 114, #27
108, 100, 33, #30
10] #33
-1, 1, 13, #18 Pos 20 is string length
72, 101, 108, #21 #Data begins here
108, 111, 32,
87, 111, 114,
108, 100, 33,
10]

pc=0 #Program counter. The pointer to where we are

out="" #Somewhere to collect the output to display at the end
pc=0
c=0
out=""
while True:
#Load the next 3 values into a, b, c
c+=1
a,b,c=prog[pc:pc+3]

#now what?

print out
if c==-2:
c=pc+3
pc+=3
if b>=0:
prog[b]-=prog[a]
result=prog[b]
else:
result=-1
if b==-1:
out+= chr(prog[a])
if result<=0:
pc=c
if pc<0:
break
print(out)

0 comments on commit e8d15ac

Please sign in to comment.