Wednesday, March 30, 2022

ESOLANG SUBLEQ

ESOLANG:  Esoteric (computer programming) Language

SUBLEQ:  Subtract and Branch on Less than or Equal (to zero)

I ran across Truttle1's videos on Esolangs last week, and have watched them with somewhat morbid fascination.  I remember messing around with one of these (either Subleq or DJN) on the Commodore 64 way back when.  Life has been stressful the last couple of weeks, and playing with these has been a good escape from reality.

My two favorites at the moment are Subleq and Starfish.  With Starfish (and its parent, Fish), I specifically like the stack of stacks.  That would be really clever and useful to implement with a Forth-like language.  Speaking of which, Factor appears to be the bastard mutant love child of Forth and Lisp.  What can I say, I'm a language junkie.

Anyway, I've been musing over improving/expanding Subleq, a computer language with only one command (B=B-A, branch to C if B≤0) in my sleepless nights.  Yes, I'm trying to optimize something that is deliberately obfuscated.  The language could be expanded by one more address as a target for the subtraction (C=B-A, branch to D if C≤0).  (In Subleq, every reference is a memory address, not directly a number.  So what you're really doing is taking the number at address A, subtracting it from the number found at address B, storing the result in address C, moving to the next instruction if the result was positive, or moving to the instruction at address D if the result was zero or negative.)  That would make moving memory around a bit easier.  

Having discovered Subleq+, which uses negative addresses for indirect addressing (the number in address B is the address of the number to be subtracted from), I wondered what more could be done with the negative address space.  Using address 0 as the instruction pointer seemed obvious.  I'm not certain how useful it will be, but it certainly can't hurt.  Oh, who am I kidding, of course it could seriously mess things up!  Where's the fun in avoiding that sort of confusion in an esolang?

Since -1 is used in Subleq for input (A)/output (B)/program halt (C), perhaps a few other low addresses could be used for different meta operations?  What is Subleq especially weak at?  Bitwise operations.  What does Subleq need?  A bitwise coprocessor, accessed through negative addressing!  For instructions -2 through -7, we access a coprocessor function by making it the target of the storage (C) word in the instruction.

Address   Contents
 0        Instruction Pointer (initially set to a self-referential 0)
-1        A: get character from stdin => C
          B: get character from stdin and negate it => C
          C: send result ABS(B-A) to stdout as character
          D: halt program

C:-2      NOT A => B
C:-3      A AND B => D
C:-4      A OR B => D
C:-5      A XOR B => D
C:-6      Left shift A by B bits => D
C:-7      Right shift A by B bits => D

We could continue to add functionality at the expense of address space, but why spoil the fun?  With 4 word instructions, 7 is a great place to stop.  

But if we wanted to add a coprocessor for floating point math, well, you need a whole separate space to hold the numbers.  Transferring integers to FP registers translates them.  Transferring floating point numbers back to integers rounds them down.

Address    Contents
-8           FP Constant E (2.718281828)
-9           FP Register F
-10          FP Register G
-11          FP Register H (results)
C:-12        B + A => H
C:-13        B - A => H
C:-14        B * A => H
C:-15        B / A => H
C:-16        Log base B of A (A:100 B:10 => H:2)
C:-17        B to the power of A (A:2 B:10 => H:100)
C:-18        A root of B (A:2 B:9 => H:3)
-19          FP Constant Pi (3.141592653)

But that's just crazy talk.  

No comments:

Post a Comment

I reserve the right to remove egregiously profane or abusive comments, spam, and anything else that really annoys me. Feel free to agree or disagree, but let's keep this reasonably civil.