Last active
July 21, 2025 17:52
-
-
Save wallabra/40f91874bc239873d474f4ca3cb35299 to your computer and use it in GitHub Desktop.
hello world in 6502 assembly with memory mapped linux syscalls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// = hello world in 6502 assembly with memory mapped linux syscalls | |
// :author: wallabra | |
// :email: [email protected] | |
// | |
// literate 6502 assembly with asciidoc comments | |
// | |
// * uses Linux syscalls | |
// | |
// addresses exposed by 6502 VM:: | |
// + | |
// -- | |
// `$10-$1F`:: syscall arguments (4 bytes each) | |
// `$20-$2F`:: interrupt buffer (4 byte 'interrupt message type' + other data) | |
// -- | |
.data: | |
// == write | |
// `.string` returns the address | |
HelloWorld: .string "Hello World" | |
// assume `LenOf` is the assembler's length function. | |
// | |
// assume little endian. | |
HelloWorldLen: .word !LenOf(HelloWorld) | |
.text: | |
_begin: | |
// select `write` syscall (Linux: 1) | |
LDA #1 | |
STA $10 | |
// select stdout | |
LDA #1 | |
STA $14 | |
// data address pointer | |
// | |
// (assume it is auto-mapped to the 6502 address space | |
// in the syscall wrapper) | |
LDA HelloWorld | |
STA $18 | |
LDX #1 | |
LDA HelloWorld,X | |
STA $1 | |
// pass the length of string | |
LDA HelloWorldLen | |
STA $1C // LSB; assume little endian | |
LDX #1 | |
LDA HelloWorldLen,X | |
STA $1D // MSB | |
// state that this interrupt reason is a syscall (6502 VM | |
// interrupt handler) | |
LDA #$10 | |
STA $20 | |
// trigger interrupt | |
BRK | |
// end of program | |
// | |
// your 6502 VM may not require an explicit call to `exit` at the end, | |
// but let's say that it does for the sake of completeness | |
// select `exit` syscall (Linux: 0x80) | |
LDA #$80 | |
STA $10 | |
// exit code | |
LDA #0 | |
STA $14 | |
// state that this interrupt reason is a syscall | |
LDA #$10 | |
STA $20 | |
BRK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment