xASM is a lightweight assembly-like language designed for learning and experimentation. It features a custom instruction set, register-based architecture, and a JavaScript interpreter that can execute xASM programs.
- Assembly style syntax: Familiar assembly-like instruction format with labels and jumps
- Register based architecture: Uses registers A, B, C, D, E for operations and data storage
- Basic arithmetic: Support for ADD, SUB, MUL, DIV, MOD, and POW operations
- Control flow: Jump instructions (JMP, JEQ) with label support for loops and conditionals
- String perations (USELESS RIGHT NOW!): CONCAT for string concatenation and LEN for length operations
- System Calls: CALL instruction for output and system operations
- Tiny and lightweight: Pure JavaScript implementation with no external dependencies
# Using git
git clone https://github.com/xbcq1490/xASM.git
cd xASM
npm install -g .
# Using npm
npm install -g xasm
# Run the included example
npm test
# Run with the cli tool
xasm ./path/to/file.xa
# Or run manually
node cli.js ./path/to/file.xa
xASM has infinite registers.
- Used for storing values, performing calculations, and passing parameters
LOAD A, 42 ; Load immediate value 42 into register A
ADD A, 5 ; Add 5 to register A
SUB A, 3 ; Subtract 3 from register A
MUL A, 2 ; Multiply register A by 2
DIV A, 4 ; Divide register A by 4
MOD A, 3 ; A = A modulo 3
POW A, 2 ; A = A raised to power of 2
UNM A, B ; Unary minus operation
CONCAT A, B, C ; Concatenate all values, then store in A register
LEN A, B ; Get length of value in B, store in A
LABEL loop ; Define a label named 'loop'
JMP loop ; Jump to label 'loop'
JEQ A, 0, end ; Jump to 'end' if A equals 0
JEQ A, 5, true, false ; Jump to 'true' if A equals 5, otherwise 'false'
CALL C, A ; System call, 0 = print, 1 = error
HALT ; Stop program execution
; This is a comment - lines starting with semicolon are ignored
; Count from 1 to 10
LOAD A, 1
LOAD C, 0
LABEL loop
CALL C, A ; Print current number
ADD A, 1 ; Increment counter
JEQ A, 11, end, loop ; Continue if A < 11
LABEL end
HALT
The xASM interpreter consists of several components:
- Parser (
src/parser/
): Parses xASM source code into a tree. - Interpreter (
src/interpreter/
): Executes the parsed instructions using a virtual machine - Opcodes (
src/opcodes/
): Defines the instruction set and argument requirements - Registers (
src/registers/
): Register template
- Node.js: Requires Node.js 12.0 or higher
- File Extension: Uses
.xa
extension for xASM source files - No Memory Model: Operations are register-based only
- Limited I/O: Only supports basic print output via CALL instruction
The interpreter provides basic error handling for:
- Invalid instruction syntax
- Missing opcodes
- Register access errors
try {
const vm = new interpreter(parsed.tree);
vm.executeTree();
} catch (error) {
console.error("Execution failed:", error, message);
}
Contributions are welcome! This project was made as an experiment. Feel free to:
- Add new opcodes
- Improve the parser
- Enhance error handling
- Create more example programs
- Optimize the interpreter
Please feel free to submit a Pull Request.
This project is licensed under the GPL-3.0-or-later License - see the LICENSE file for details.
If you encounter any issues or have feature requests, please file them on the GitHub issues page.
Made with ❤️ for "assemblers"