NeoVM and over 50 years of computing on stack machines

Igor Machado
NeoResearch
Published in
3 min readJun 10, 2019

NeoVM is a virtual stack-based machine for processing Smart Contract applications (and also transaction verifications) on Neo Blockchain, inspired by many successful stack languages like Bitcoin Script, Microsoft CIL, Java Virtual Machine and, finally, the FORTH language.

A Stack is a very simple and nice data structure, that defines only two ways of accessing its data: you can push data on top of the stack, or pop data from the top. Although quite simple in principle, it allows creating very complex programs, specially if you have access to multiple stacks. Stack machines have been around for a while now, and as extensions of classic Pushdown Automata, they can be as powerful as any Turing Machine.

It is hard to find out who started doing computing on stack machines first, as many languages use the concept of virtual stacks on computing or parsing, like ALGOL 60, and modern languages like Java, Python, etc. It is hard to find a language so reliant on stack machines such as FORTH, created by Chuck Moore on 1970, and used in several aerospacial projects since then. Since FORTH is one of the purest languages regarding stack computation (nearly everything must be done using stacks) we will use it as our reference here.

One interesting project than soon emerged was to implement NeoVM directly on FORTH language, to verify that all these stack concepts suit perfectly on Neo blockchain virtual machine. The result can be found on NeoResearch nvm-forth project, that although not entirely complete for now, it’s already capable of doing quite a few interesting things.

nvm-forth project: github.com/NeoResearch/nvm-forth

The computation done by Smart Contracts on Neo blockchain is performed using these tiny operations called opcodes, and they can be used to code virtually everything! For example, if you want to put two numbers (1 and 2) on a stack, you can use opcodes PUSH1 PUSH2. How can you add them together (performing 1+2)? You simply need to use opcode ADD, then two top stack elements will be added together, and the result will be put back on the stack (in this case, value 3). This process can be done for basic arithmetic operations, such as ADD, SUB, MUL, DIV, and special ones such as MOD (modular arithmetic).

Numbers on NeoVM are also very special (and Big): all arithmetics is performed on 32-bytes (or 256-bits) integer numbers. This is much bigger than usual computing int and long types, which are usually 4 and 8 bytes (32 and 64 bits, respectively), this is why we can safely use NeoVM to calculate things such as a Satoshi Unit (0.00000001). Usually, floating point arithmetics do not allow such precision, although this can be safely implemented with 64-bit longs to guarantee the 8-digit precision, together with a reasonable number of digits.

Back to stack operations, we can use all these arithmetics together with function calls, jumps, in a very transparent way for the user, allowing for example, coding in higher level languages such as C#, Python, Go and Java. This still guarantees precision and determinism on the performed operations, which is fundamental for a verifiable and transparent blockchain technology.

So, question is: what can you do with NeoVM besides writing Smart Contracts on Neo blockchain? The answer is: everything. Since NeoVM is Turing-Complete by design, any existing operation can also be done in such virtual machine. To show you that, we looked for and found an interesting “Snake Game” implementation using FORTH stack language, written by Nick Morgan, and we “translated” it to our nvm-forth implementation. The result is amazing (see picture below).

“Snake Game” implemented using nvm-forth project and original EasyForth by Nick Morgan

If you want to learn more about Stack Machines, NeoVM and the FORTH language, feel free to visit NeoResearch NVM Learn Tutorial.

This and other nice open-source projects can be found on NeoResearch Community GitHub, see you there ;)

(*) This project will also be part of NEO Tutorial initiative by NGD, and is a gentle introduction to the upcoming NEO3 Specification.

--

--