DEV Community

Thomas Leathers
Thomas Leathers

Posted on

3 1

Balanced Ternary Fibonacci, in a new language.

Overview:

SBTCVM is a balanced ternary (a variant of ternary that uses a negative digit instead of 2) virtual machine written in python.

Recently, I and the rest of the SBTCVM developers have been hammering away at a new, 6.5Khz, modular, codebase called 'SBTCVM Gen 2'. Its still in alpha, but its definitely making progress, and can already run moderately complex math programs, and print their output to a virtual TTY.

For those of you who saw my previous post on Fibonacci in balanced ternary, you probably already know, SBTCVM's old codebase only had an assembler. Well...

SBTCVM's new SSTNPL (SBTCVM Simplified Ternary Numeric Programming Language)

#Fibonacci sequence in SSTNPL
var endnum=10x4181
#both of these == 1 at first.
var num0=10x1
var num1=+
#----mainloop----
label loop
#result printing:
dumpd num0
space
dumpt num0
newline
#end condition
if num0,endnum goto end
#math
add num0,num1
set num0
swap num0,num1
#loop back around
goto loop
#end routine
label end
print written in SSTNPL
stop
view raw fibsstnpl.stnp hosted with ❤ by GitHub

SO, Whats going on here? well, this is the new, abstracted, primarily-numeric, language, built for SBTCVM gen2, thats not a week old.

basics:

SSTNPL is a language built primarily for mathematics. its driven by a seamless, named-integer-variable system. Its main goal is to abstract the complexities of the Virtual machine enough to provide a nice abstract language for learning Balanced Ternary mathematics, and doing calculations.

var

var = is how we create variables for use & change later. (you must call var for every variable you use, regardless of whether it needs a starting value. Also most operations (other than a val followed by a 'set' statement), only take variable names as arguments.

value can have 2 forms:

prefix the value with '10x' if you wish to use decimal. i.e.
10x-59

otherwise you can use ternary in both +0- and p0n notations.

Result printing:

here we have two strange commands:
dumpd and dumpt

dumpd dumps the specified integer in decimal form
dumpt does the same, but in ternary form.

newline and space are just TTY macros intended for formatting.

end condition:

the conditional goto. you might notice that it takes 3 arguments. 2 operators that are comma-separated, and a label to actually jump to.

there are actually 3 types: if(equals), ifmore, and ifless. (this is no coincidence, as SBTCVM's new CPU also has these 3)

math

add does just what you'd expect. It adds the two specified arguments. But how do we get our result?

thats the job of 'set' in this example, we 'set' num0 to the result of the previous operation.

swap, just switches the values of the two arguments.

And finally we just do 'goto loop' to return to the top of our loop at 'label loop', and then write a small exit routine to jump to in our conditional, and we have a program.

When we build this SSTNPL program into a memory image (.trom) and run it, via the stnpcom.py compiler and SBTCVM gen 2's curses VM frontend, the logged output looks like so:

frontend: Curses
Begin UIO tty log:
ready.
1 00000000+
1 00000000+
2 0000000+-
3 0000000+0
5 000000+--
8 000000+0-
13 000000+++
21 00000+-+0
34 00000++-+
55 0000+-00+
89 0000+0+0-
144 000+--+00
233 000+00-0-
377 00+---00-
610 00+0----+
987 00++0+--0
1597 0+-+--0++
2584 0++--0-0+
4181 +-0-+-0--
written in SSTNPL

SBTCVM's new codebase can be found here, and works in both python2 and python3:

GitHub logo SBTCVM / SBTCVM-Gen2-9

SBTCVM is a virtual machine implementation of a balanced ternary (base 3) computer. Features several compiled languages for ternary software development.

SBTCVM Gen2-9

Simple Balanced Ternary Computer Virtual Machine

v2.1.0.alpha

Need Help? See our Getting started guide

SBTCVM Project blog

What is SBTCVM?

Ever wonder what computers other than the boring-old binary would be like? Well, look no further SBTCVM, a python-written VM, simulates the little-known base number called Balanced Ternary!

What is balanced ternary? well, it has "0" and "+1", just like binary, but added into the mix is a "-1"! Yes, this means EVERY number is signed.

Intrigued? Well, as SBTCVM is Free & Open Source Software, and comes with a ready-to-use set of compilers and development tools, Getting started with balanced ternary with SBTCVM, should prove a fun challenge!

Screenshots:

'tritmap_heavy' TROM: High resolution Compressed image demo 'fttarget' TROM: Falling Ternary Targets Game 'clicalc' TROM's memory monitor 'ontrain' TROM: Oncoming Trains game SBTCVM's GUI desktop 'bench' TROM/disk, showing its info screen. Bench desktop

Features:

  • bundled ternary software. including demos, games and utilities.
  • 4 channel sound chip
  • 6.5Khz, 9-trit CPU
  • 3.25Khz 9-trit Coprocessor
  • 39.388 KiloNonets of system RAM
  • pygame frontend features color graphics.
  • multiple specialized programming languages with integrated compilers. (SSTNPL, SBTCVM…

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs