# b4 virtual machine

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

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



[Skip to content](#start-of-content)













## Navigation Menu

Toggle navigation

[Sign in](/login?return_to=https%3A%2F%2Fgithub.com%2Ftangentstorm%2Fb4)

Appearance settings

* Platform

  + AI CODE CREATION
    - [GitHub CopilotWrite better code with AI](https://github.com/features/copilot)
    - [GitHub SparkBuild and deploy intelligent apps](https://github.com/features/spark)
    - [GitHub ModelsManage and compare prompts](https://github.com/features/models)
    - [MCP RegistryNewIntegrate external tools](https://github.com/mcp)
  + DEVELOPER WORKFLOWS
    - [ActionsAutomate any workflow](https://github.com/features/actions)
    - [CodespacesInstant dev environments](https://github.com/features/codespaces)
    - [IssuesPlan and track work](https://github.com/features/issues)
    - [Code ReviewManage code changes](https://github.com/features/code-review)
  + APPLICATION SECURITY
    - [GitHub Advanced SecurityFind and fix vulnerabilities](https://github.com/security/advanced-security)
    - [Code securitySecure your code as you build](https://github.com/security/advanced-security/code-security)
    - [Secret protectionStop leaks before they start](https://github.com/security/advanced-security/secret-protection)
  + EXPLORE
    - [Why GitHub](https://github.com/why-github)
    - [Documentation](https://docs.github.com)
    - [Blog](https://github.blog)
    - [Changelog](https://github.blog/changelog)
    - [Marketplace](https://github.com/marketplace)

  [View all features](https://github.com/features)
* Solutions

  + BY COMPANY SIZE
    - [Enterprises](https://github.com/enterprise)
    - [Small and medium teams](https://github.com/team)
    - [Startups](https://github.com/enterprise/startups)
    - [Nonprofits](https://github.com/solutions/industry/nonprofits)
  + BY USE CASE
    - [App Modernization](https://github.com/solutions/use-case/app-modernization)
    - [DevSecOps](https://github.com/solutions/use-case/devsecops)
    - [DevOps](https://github.com/solutions/use-case/devops)
    - [CI/CD](https://github.com/solutions/use-case/ci-cd)
    - [View all use cases](https://github.com/solutions/use-case)
  + BY INDUSTRY
    - [Healthcare](https://github.com/solutions/industry/healthcare)
    - [Financial services](https://github.com/solutions/industry/financial-services)
    - [Manufacturing](https://github.com/solutions/industry/manufacturing)
    - [Government](https://github.com/solutions/industry/government)
    - [View all industries](https://github.com/solutions/industry)

  [View all solutions](https://github.com/solutions)
* Resources

  + EXPLORE BY TOPIC
    - [AI](https://github.com/resources/articles?topic=ai)
    - [Software Development](https://github.com/resources/articles?topic=software-development)
    - [DevOps](https://github.com/resources/articles?topic=devops)
    - [Security](https://github.com/resources/articles?topic=security)
    - [View all topics](https://github.com/resources/articles)
  + EXPLORE BY TYPE
    - [Customer stories](https://github.com/customer-stories)
    - [Events & webinars](https://github.com/resources/events)
    - [Ebooks & reports](https://github.com/resources/whitepapers)
    - [Business insights](https://github.com/solutions/executive-insights)
    - [GitHub Skills](https://skills.github.com)
  + SUPPORT & SERVICES
    - [Documentation](https://docs.github.com)
    - [Customer support](https://support.github.com)
    - [Community forum](https://github.com/orgs/community/discussions)
    - [Trust center](https://github.com/trust-center)
    - [Partners](https://github.com/partners)
* Open Source

  + COMMUNITY
    - [GitHub SponsorsFund open source developers](https://github.com/sponsors)
  + PROGRAMS
    - [Security Lab](https://securitylab.github.com)
    - [Maintainer Community](https://maintainers.github.com)
    - [Accelerator](https://github.com/accelerator)
    - [Archive Program](https://archiveprogram.github.com)
  + REPOSITORIES
    - [Topics](https://github.com/topics)
    - [Trending](https://github.com/trending)
    - [Collections](https://github.com/collections)
* Enterprise

  + ENTERPRISE SOLUTIONS
    - [Enterprise platformAI-powered developer platform](https://github.com/enterprise)
  + AVAILABLE ADD-ONS
    - [GitHub Advanced SecurityEnterprise-grade security features](https://github.com/security/advanced-security)
    - [Copilot for BusinessEnterprise-grade AI features](https://github.com/features/copilot/copilot-business)
    - [Premium SupportEnterprise-grade 24/7 support](https://github.com/premium-support)
* [Pricing](https://github.com/pricing)

Search or jump to...


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

Search

Clear

[Search syntax tips](https://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax)

# 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](https://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax).

Cancel
 Create saved search

[Sign in](/login?return_to=https%3A%2F%2Fgithub.com%2Ftangentstorm%2Fb4)

[Sign up](/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E&source=header-repo&source_repo=tangentstorm%2Fb4)

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](/tangentstorm) 
/
**[b4](/tangentstorm/b4)**
Public

* [Notifications](/login?return_to=%2Ftangentstorm%2Fb4) You must be signed in to change notification settings
* [Fork
  4](/login?return_to=%2Ftangentstorm%2Fb4)
* [Star
   39](/login?return_to=%2Ftangentstorm%2Fb4)

b4 : a tiny forth-like virtual machine

[www.reddit.com/r/b4lang/](http://www.reddit.com/r/b4lang/ "http://www.reddit.com/r/b4lang/")

[39
stars](/tangentstorm/b4/stargazers) [4
forks](/tangentstorm/b4/forks) [Branches](/tangentstorm/b4/branches) [Tags](/tangentstorm/b4/tags) [Activity](/tangentstorm/b4/activity)

[Star](/login?return_to=%2Ftangentstorm%2Fb4)

[Notifications](/login?return_to=%2Ftangentstorm%2Fb4) You must be signed in to change notification settings

* [Code](/tangentstorm/b4)
* [Issues
  0](/tangentstorm/b4/issues)
* [Pull requests
  2](/tangentstorm/b4/pulls)
* [Actions](/tangentstorm/b4/actions)
* [Projects
  0](/tangentstorm/b4/projects)
* [Wiki](/tangentstorm/b4/wiki)
* [Security
  0](/tangentstorm/b4/security)
* [Insights](/tangentstorm/b4/pulse)

Additional navigation options


* [Code](/tangentstorm/b4)
* [Issues](/tangentstorm/b4/issues)
* [Pull requests](/tangentstorm/b4/pulls)
* [Actions](/tangentstorm/b4/actions)
* [Projects](/tangentstorm/b4/projects)
* [Wiki](/tangentstorm/b4/wiki)
* [Security](/tangentstorm/b4/security)
* [Insights](/tangentstorm/b4/pulse)

# tangentstorm/b4

main

[Branches](/tangentstorm/b4/branches)[Tags](/tangentstorm/b4/tags)

Go to file

Code

Open more actions menu

## Folders and files

| Name | | Name | Last commit message | Last commit date |
| --- | --- | --- | --- | --- |
| Latest commit   History[1,568 Commits](/tangentstorm/b4/commits/main/)   1,568 Commits | | |
| [b3a](/tangentstorm/b4/tree/main/b3a "b3a") | | [b3a](/tangentstorm/b4/tree/main/b3a "b3a") |  |  |
| [bios](/tangentstorm/b4/tree/main/bios "bios") | | [bios](/tangentstorm/b4/tree/main/bios "bios") |  |  |
| [doc](/tangentstorm/b4/tree/main/doc "doc") | | [doc](/tangentstorm/b4/tree/main/doc "doc") |  |  |
| [electron](/tangentstorm/b4/tree/main/electron "electron") | | [electron](/tangentstorm/b4/tree/main/electron "electron") |  |  |
| [etc](/tangentstorm/b4/tree/main/etc "etc") | | [etc](/tangentstorm/b4/tree/main/etc "etc") |  |  |
| [jlang](/tangentstorm/b4/tree/main/jlang "jlang") | | [jlang](/tangentstorm/b4/tree/main/jlang "jlang") |  |  |
| [js](/tangentstorm/b4/tree/main/js "js") | | [js](/tangentstorm/b4/tree/main/js "js") |  |  |
| [lil](/tangentstorm/b4/tree/main/lil "lil") | | [lil](/tangentstorm/b4/tree/main/lil "lil") |  |  |
| [mcp-b4i](/tangentstorm/b4/tree/main/mcp-b4i "mcp-b4i") | | [mcp-b4i](/tangentstorm/b4/tree/main/mcp-b4i "mcp-b4i") |  |  |
| [pas](/tangentstorm/b4/tree/main/pas "pas") | | [pas](/tangentstorm/b4/tree/main/pas "pas") |  |  |
| [ref](/tangentstorm/b4/tree/main/ref "ref") | | [ref](/tangentstorm/b4/tree/main/ref "ref") |  |  |
| [.gitignore](/tangentstorm/b4/blob/main/.gitignore ".gitignore") | | [.gitignore](/tangentstorm/b4/blob/main/.gitignore ".gitignore") |  |  |
| [ASSISTANTS.md](/tangentstorm/b4/blob/main/ASSISTANTS.md "ASSISTANTS.md") | | [ASSISTANTS.md](/tangentstorm/b4/blob/main/ASSISTANTS.md "ASSISTANTS.md") |  |  |
| [README.org](/tangentstorm/b4/blob/main/README.org "README.org") | | [README.org](/tangentstorm/b4/blob/main/README.org "README.org") |  |  |
| [b4-tests.org](/tangentstorm/b4/blob/main/b4-tests.org "b4-tests.org") | | [b4-tests.org](/tangentstorm/b4/blob/main/b4-tests.org "b4-tests.org") |  |  |
| [b4i](/tangentstorm/b4/blob/main/b4i "b4i") | | [b4i](/tangentstorm/b4/blob/main/b4i "b4i") |  |  |
| [b4i-tests.org](/tangentstorm/b4/blob/main/b4i-tests.org "b4i-tests.org") | | [b4i-tests.org](/tangentstorm/b4/blob/main/b4i-tests.org "b4i-tests.org") |  |  |
| [b4ix](/tangentstorm/b4/blob/main/b4ix "b4ix") | | [b4ix](/tangentstorm/b4/blob/main/b4ix "b4ix") |  |  |
| [b4s-tests.org](/tangentstorm/b4/blob/main/b4s-tests.org "b4s-tests.org") | | [b4s-tests.org](/tangentstorm/b4/blob/main/b4s-tests.org "b4s-tests.org") |  |  |
| [bios-tests.org](/tangentstorm/b4/blob/main/bios-tests.org "bios-tests.org") | | [bios-tests.org](/tangentstorm/b4/blob/main/bios-tests.org "bios-tests.org") |  |  |
| [goals.org](/tangentstorm/b4/blob/main/goals.org "goals.org") | | [goals.org](/tangentstorm/b4/blob/main/goals.org "goals.org") |  |  |
| [init.b4i](/tangentstorm/b4/blob/main/init.b4i "init.b4i") | | [init.b4i](/tangentstorm/b4/blob/main/init.b4i "init.b4i") |  |  |
| [plan.org](/tangentstorm/b4/blob/main/plan.org "plan.org") | | [plan.org](/tangentstorm/b4/blob/main/plan.org "plan.org") |  |  |
| [pre-tests.org](/tangentstorm/b4/blob/main/pre-tests.org "pre-tests.org") | | [pre-tests.org](/tangentstorm/b4/blob/main/pre-tests.org "pre-tests.org") |  |  |
| [test](/tangentstorm/b4/blob/main/test "test") | | [test](/tangentstorm/b4/blob/main/test "test") |  |  |
| [test-b4-b4a](/tangentstorm/b4/blob/main/test-b4-b4a "test-b4-b4a") | | [test-b4-b4a](/tangentstorm/b4/blob/main/test-b4-b4a "test-b4-b4a") |  |  |
| [test-b4-gd](/tangentstorm/b4/blob/main/test-b4-gd "test-b4-gd") | | [test-b4-gd](/tangentstorm/b4/blob/main/test-b4-gd "test-b4-gd") |  |  |
| [test-b4-j](/tangentstorm/b4/blob/main/test-b4-j "test-b4-j") | | [test-b4-j](/tangentstorm/b4/blob/main/test-b4-j "test-b4-j") |  |  |
| [test-b4-js](/tangentstorm/b4/blob/main/test-b4-js "test-b4-js") | | [test-b4-js](/tangentstorm/b4/blob/main/test-b4-js "test-b4-js") |  |  |
| [test-b4-lil](/tangentstorm/b4/blob/main/test-b4-lil "test-b4-lil") | | [test-b4-lil](/tangentstorm/b4/blob/main/test-b4-lil "test-b4-lil") |  |  |
| [test-b4-pas](/tangentstorm/b4/blob/main/test-b4-pas "test-b4-pas") | | [test-b4-pas](/tangentstorm/b4/blob/main/test-b4-pas "test-b4-pas") |  |  |
| [test-b4i-js](/tangentstorm/b4/blob/main/test-b4i-js "test-b4i-js") | | [test-b4i-js](/tangentstorm/b4/blob/main/test-b4i-js "test-b4i-js") |  |  |
| [test-b4i-lil](/tangentstorm/b4/blob/main/test-b4i-lil "test-b4i-lil") | | [test-b4i-lil](/tangentstorm/b4/blob/main/test-b4i-lil "test-b4i-lil") |  |  |
| [test-b4i-pas](/tangentstorm/b4/blob/main/test-b4i-pas "test-b4i-pas") | | [test-b4i-pas](/tangentstorm/b4/blob/main/test-b4i-pas "test-b4i-pas") |  |  |
| [test-b4s](/tangentstorm/b4/blob/main/test-b4s "test-b4s") | | [test-b4s](/tangentstorm/b4/blob/main/test-b4s "test-b4s") |  |  |
| [test-bios](/tangentstorm/b4/blob/main/test-bios "test-bios") | | [test-bios](/tangentstorm/b4/blob/main/test-bios "test-bios") |  |  |
| [test-pre](/tangentstorm/b4/blob/main/test-pre "test-pre") | | [test-pre](/tangentstorm/b4/blob/main/test-pre "test-pre") |  |  |
| View all files | | |

## Repository files navigation

* [README](#)

# The b4 virtual machine

# Demo

[![ref/b4ix-rosetta.png](/tangentstorm/b4/raw/main/ref/b4ix-rosetta.png)](/tangentstorm/b4/blob/main/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 machine’s 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 it’s 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` that’s 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:

* [./jlang/](/tangentstorm/b4/blob/main/jlang), implemented in [J](https://code.jsoftware.com/wiki/Main_Page).
* [./pas/](/tangentstorm/b4/blob/main/pas), implemented in [free pascal](https://www.freepascal.org/).
* [./lil/](/tangentstorm/b4/blob/main/lil), implemented in [lil](https://beyondloom.com/decker/lil.html).
* [./js/](/tangentstorm/b4/blob/main/js), implemented in javascript.
* [b4-gd](https://github.com/tangentstorm/b4-gd), implemented in GDScript for [Godot 4](https://docs.godotengine.org/en/stable/).

The implementations are kept in sync through the tests in [./b4-tests.org](/tangentstorm/b4/blob/main/b4-tests.org)

See [./bios/bios.b4a](/tangentstorm/b4/blob/main/bios/bios.b4a) for an example of b4 assembly language.

# Links

* [b4 github repo](https://github.com/tangentstorm/b4)
* twitter: [@tangentstorm](https://twitter.com/tangentstorm) / [#b4lang](https://twitter.com/#!/search/realtime/%23b4lang)
* reddit: [/r/b4lang](http://reddit.com/r/b4lang)

# Background

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

* [Programming a Problem-Oriented Language](http://www.colorforth.com/POL.htm)
* [Starting Forth](http://www.forth.com/starting-forth/)

B4 was strongly influenced by the [ngaro virtual machine](http://retroforth.org/docs/The_Ngaro_Virtual_Machine.html) from retroforth 11,
as well as the [forth chips from greenarrays](https://www.greenarraychips.com/home/documents/index.php#architecture).

## About

b4 : a tiny forth-like virtual machine

[www.reddit.com/r/b4lang/](http://www.reddit.com/r/b4lang/ "http://www.reddit.com/r/b4lang/")

### Resources

[Readme](#readme-ov-file)

### Uh oh!

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

[Activity](/tangentstorm/b4/activity)

### Stars

[**39**
stars](/tangentstorm/b4/stargazers)

### Watchers

[**6**
watching](/tangentstorm/b4/watchers)

### Forks

[**4**
forks](/tangentstorm/b4/forks)

[Report repository](/contact/report-content?content_url=https%3A%2F%2Fgithub.com%2Ftangentstorm%2Fb4&report=tangentstorm+%28user%29)

## [Releases](/tangentstorm/b4/releases)

No releases published

## [Packages 0](/users/tangentstorm/packages?repo_name=b4)

No packages published

### Uh oh!

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

## [Contributors 3](/tangentstorm/b4/graphs/contributors)

### Uh oh!

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

## Languages

* [Pascal
  34.8%](/tangentstorm/b4/search?l=pascal)
* [Python
  22.4%](/tangentstorm/b4/search?l=python)
* [JavaScript
  10.7%](/tangentstorm/b4/search?l=javascript)
* [TypeScript
  8.5%](/tangentstorm/b4/search?l=typescript)
* [Forth
  7.8%](/tangentstorm/b4/search?l=forth)
* [J
  5.9%](/tangentstorm/b4/search?l=j)
* Other
  9.9%

## Footer

© 2026 GitHub, Inc.

### Footer navigation

* [Terms](https://docs.github.com/site-policy/github-terms/github-terms-of-service)
* [Privacy](https://docs.github.com/site-policy/privacy-policies/github-privacy-statement)
* [Security](https://github.com/security)
* [Status](https://www.githubstatus.com/)
* [Community](https://github.community/)
* [Docs](https://docs.github.com/)
* [Contact](https://support.github.com?tags=dotcom-footer)
* Manage cookies
* Do not share my personal information



You can’t perform that action at this time.