Subleq

From Esolang

Jump to: navigation, search

Subleq (SUBtract and branch if Less-than or EQual to zero) is a dialect of OISC.

Contents

[edit] Basic

Subleq is a simple one instruction language. Each subleq instruction has 3 operands:

A B C

which are memory addresses. Execution of one instruction A B C subtracts the value of memory in A from the content of memory in B. If value after subtraction in B less or equal to zero, then execution jumps to the address C; otherwise to the next instruction.

For example, in

3 4 6
7 7 7
3 4 0

first instruction subtracts 7 (address 3) from 7 (address 4). The result in address 4 is 0, so goto 6. On address 6 is the instruction 3 4 0 which again substracts 7 from now 0 and jumps back to 0. Here is a sequence of execution (A and B are shown after subtraction)

0: 3 4 6 A=7 B=0
6: 3 4 0 A=7 B=-7
0: 3 4 6 A=7 B=-14
6: 3 4 0 A=7 B=-21
0: 3 4 6 A=7 B=-28
... 

[edit] Hello, world!

For simplicity, let us replace the real cell addresses with names(labels).

X Y 6
X:7 Y:7 7
X Y 0

On the first line X refer to a memory cell defined somewhere else. On the second line X is actually defined by X:7, which means that this memory cell has name X and its initial value 7.

Now let our language to make something useful - produce output. To achieve that we need a special memory address (because all the instruction can do is subtract). Lets call it OUT, and lets its value be defined at run time.

H OUT 3
i OUT 6
0 0 (-1)
H:72 i:105

This program prints "Hi". The program's code is

9 -1 3
10 -1 6
0 0 -1
72 105

Here is OUT defined as -1. 72 and 105 are ASCII codes for 'H' and 'i'. The third line instruct to subtract value of zero's cell from itself and to go to the address -1 (since the result will always be 0). Go to negative address means halt. This last instruction could well be

5 5 -8

any A and B registers but A=B and any negative C.

Since we are using already symbolic names for cell addresses we need a simple converter to produce a digit code for SUBLEQ executor. This simple converter can do slightly more:

# Hello world! (Hi)
Hi OUT
Hi+1 OUT
0 0 (-1) 
. Hi: "Hi"

Note the differences: comment line, goto address may be left out (meaning next instruction address), arithmetic expression (Hi+1), literal "Hi" equivalent to 'H' 'i', and dot meaning that the following is not an instruction.

Dot does not make difference in the execution in this case, but it make difference in the code:

72 105

without dot this line would be

72 105 12

12 is the address of the next (not present) instruction.

[edit] Hello, world! (more)

Placing more then 1 instruction on one line will require semicolon:

Hi OUT;  Hi+1 OUT; 0 0 (-1) 

Instructions which clear one memory cell are always in the form: Z Z. This can be shortened to just Z. And yet another convention is ?(question mark): it refers to the next to current address. For example,

3 4 6
7 7 7
3 4 0

can be rewritten as

3 4 ?+3
7 7 ?
3 4 0

Given these improvements we can write a proper "hello world" program which iterates over a string of characters .

# Hello, world!

# OUT=*p; 
a; p Z; Z a; Z
a:0 OUT

# p++
m1 p;

#check if p==E
 
a; E Z; Z a; Z;
b; p Z; Z b; Z;
E b ?+3
Z Z ?+3
p a ?+3

Z Z 0

0 0 (-1)  # halt

. p:H Z:0 m1:-1 b:0

. H: "Hello, world!\n" E:E

[edit] How to run subleq programs

[edit] Subleq assembler

Download windows executable (or C++ source) assembler. It takes stdin and produce stdout. The command

sqasm.exe < hw.sq > hw

will read hw.sq file and produce hw text file. If hw.sq is the first "hello world" example, the command also prints "UNRESOLVED: [OUT:4]" which tells that label OUT was not resolved - it is OK because OUT is the default name for the output register for the emulator.

[edit] Subleq emulator

Download windows executable emulator (or C++ source). Running with no arguments it prints a brief help.

sqrun.exe hw

[edit] External links

http://en.wikipedia.org/wiki/Subtract_and_branch_if_negative#Subtract_and_branch_if_negative

http://eigenratios.blogspot.com/2006/09/mark-ii-oisc-self-interpreter.html

http://mazonka.com/subleq/

http://99-bottles-of-beer.net/language-oisc-1395.html

http://geocities.com/r_e_s_01/subleq/ Subleq variants; interpreters in Subleq and Python

Personal tools