18 KiB
b4 virtual machine
Source: https://github.com/tangentstorm/b4
GitHub - tangentstorm/b4: b4 : a tiny forth-like virtual machine
Navigation Menu
Toggle navigation
Appearance settings
-
Platform
- AI CODE CREATION
- DEVELOPER WORKFLOWS
- APPLICATION SECURITY
- EXPLORE
-
Solutions
- BY COMPANY SIZE
- BY USE CASE
- BY INDUSTRY
-
Resources
- EXPLORE BY TOPIC
- EXPLORE BY TYPE
- SUPPORT & SERVICES
-
Open Source
- COMMUNITY
- PROGRAMS
- REPOSITORIES
-
Enterprise
Search or jump to...
Search code, repositories, users, issues, pull requests...
Search
Clear
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
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
- Notifications You must be signed in to change notification settings
- Fork 4
- Star 39
b4 : a tiny forth-like virtual machine
39 stars 4 forks Branches Tags Activity
Notifications You must be signed in to change notification settings
Additional navigation options
tangentstorm/b4
main
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
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
b4aassembly 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/, implemented in J.
- ./pas/, implemented in free pascal.
- ./lil/, implemented in lil.
- ./js/, implemented in javascript.
- b4-gd, implemented in GDScript for Godot 4.
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
- b4 github repo
- twitter: @tangentstorm / #b4lang
- reddit: /r/b4lang
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
Resources
Uh oh!
There was an error while loading. Please reload this page.
Stars
Watchers
Forks
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
Footer
© 2026 GitHub, Inc.
Footer navigation
- Terms
- Privacy
- Security
- Status
- Community
- Docs
- Contact
- Manage cookies
- Do not share my personal information
You can’t perform that action at this time.
