Files
forth_bootslop/references/ForthNotes/b4_virtual_machine.md
2026-02-19 16:16:24 -05:00

18 KiB
Raw Permalink Blame History

b4 virtual machine

Source: https://github.com/tangentstorm/b4

GitHub - tangentstorm/b4: b4 : a tiny forth-like virtual machine

Skip to content

Navigation Menu

Toggle navigation

Sign in

Appearance settings

Search or jump to...

Search code, repositories, users, issues, pull requests...

Search

Clear

Search syntax tips

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel Submit feedback

Saved searches

Use saved searches to filter your results more quickly

Name

Query

To see all available qualifiers, see our documentation.

Cancel Create saved search

Sign in

Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

tangentstorm / b4 Public

b4 : a tiny forth-like virtual machine

www.reddit.com/r/b4lang/

39 stars 4 forks Branches Tags Activity

Star

Notifications You must be signed in to change notification settings

Additional navigation options

tangentstorm/b4

main

BranchesTags

Go to file

Code

Open more actions menu

Folders and files

Name Name Last commit message Last commit date
Latest commit History1,568 Commits 1,568 Commits
b3a b3a
bios bios
doc doc
electron electron
etc etc
jlang jlang
js js
lil lil
mcp-b4i mcp-b4i
pas pas
ref ref
.gitignore .gitignore
ASSISTANTS.md ASSISTANTS.md
README.org README.org
b4-tests.org b4-tests.org
b4i b4i
b4i-tests.org b4i-tests.org
b4ix b4ix
b4s-tests.org b4s-tests.org
bios-tests.org bios-tests.org
goals.org goals.org
init.b4i init.b4i
plan.org plan.org
pre-tests.org pre-tests.org
test test
test-b4-b4a test-b4-b4a
test-b4-gd test-b4-gd
test-b4-j test-b4-j
test-b4-js test-b4-js
test-b4-lil test-b4-lil
test-b4-pas test-b4-pas
test-b4i-js test-b4i-js
test-b4i-lil test-b4i-lil
test-b4i-pas test-b4i-pas
test-b4s test-b4s
test-bios test-bios
test-pre test-pre
View all files

Repository files navigation

The b4 virtual machine

Demo

ref/b4ix-rosetta.png

The above screenshot is the b4ix enhanced interactive shell. The sceen is broken into three sections:

  • on top, a view into the systems video buffer, which is pre-initialized with a logo from the bios.
  • in the middle, an interactive shell for the b4a assembly language.
  • at the bottom, a view of stacks and section of the machines ram

explanation of the demo code

# https://rosettacode.org/wiki/Loops/For
:E lb 'e io rt            # set up ^E to emit a character
:I cd cd du dc sw dc rt   # set up ^I to fetch outer loop counter
:loop du .f du ^I sb c1 ad .f lb '. ^E .n lb '| ^E .n zp rt
5 loop

The ^E syntax calls whatever function the E register is pointing at, and the assembler assigns it when it sees :E.

Here, ^E loads the byte 'e (quoted ascii char) and sends that as a command to the io op, which prints the next character on the stack.

The ^I definition fetches the outer loop counter in a .f.n (“for/next”) loop.

Since its a function call, we have to dig deep into the control stack (cd means copy from control stack to data stack), (du)plicate the counter, and then (sw)ap it with the return address so we can push that back onto the control stack (dc means data -> control), and then rt (return).

Then :loop dupes its argument and uses it as an argument to the .f=/.n= loop.

During these loops, the loop counter is on the control stack, so ^I grabs it as explained earlier, but there is also a copy of the original loop length (5 in this case) on the data stack.

so du ^I sb duplicates the 5, subtracts the outer loop counter, and leaves the result on the stack.

c1 is an op that adds the constant 1 to the stack, and ad adds the top two values, so c1 ad adds 1 to the previous expression to get the length of the inner loop.

.f lb '. ^E .n prints that many dots.

lb '| ^E .n prints the pipe character and ends the outer loop.

then zp rt removes that original 5 thats been sitting on the stack the whole time (passed in when we explicitly called 5 loop) and returns to the calling function (in this case, the interactive “b4ix” shell.)

About b4

b4 is a tiny virtual machine, with a forth-like flavor.

There are currently five implementations:

The implementations are kept in sync through the tests in ./b4-tests.org

See ./bios/bios.b4a for an example of b4 assembly language.

Links

Background

B4 is a forth-like virtual machine. Quick intros to forth (free and online):

B4 was strongly influenced by the ngaro virtual machine from retroforth 11, as well as the forth chips from greenarrays.

About

b4 : a tiny forth-like virtual machine

www.reddit.com/r/b4lang/

Resources

Readme

Uh oh!

There was an error while loading. Please reload this page.

Activity

Stars

39 stars

Watchers

6 watching

Forks

4 forks

Report repository

Releases

No releases published

Packages 0

No packages published

Uh oh!

There was an error while loading. Please reload this page.

Contributors 3

Uh oh!

There was an error while loading. Please reload this page.

Languages

© 2026 GitHub, Inc.

You cant perform that action at this time.