CCasm is an interpreted assembly-like language for computercraft
Github (https://github.com/NihilisticPuffin/CCasm)
Check the GitHub Readme for the most up to date info on CCasm
Installer
wget run https://raw.githubusercontent.com/NihilisticPuffin/CCasm/main/updater.lua
Instruction SetName | Instruction | Use |
Set | set [reg] [val] | Sets the value of a register |
Copy | cpy [reg] [reg] | Copies the value of the first register into the second register |
Move | mov [reg] [reg] | Moves the value of the first register into the second register* |
Push | psh [reg:val] | Pushes value onto stack |
Pop | pop | Pops top value off stack |
Duplicate | dup | Duplicates the top value on the stack |
Swap | swp | Swaps the top two values on the stack |
Store | str [reg] | Stores the value of a register onto the stack* |
Load | ldr [reg] | Pops top value off stack and loads into register |
Add | add | Pops top two values off stack adds them and pushes result onto stack |
Subtract | sub | Pops top two values off stack subtracts them and pushes result onto stack |
Multiply | mul | Pops top two values off stack multiplies them and pushes result onto stack |
Divide | div | Pops top two values off stack divides them and pushes result onto stack |
Modulo | mod | Pops top two values off stack preforms a modulo operation and pushes result onto stack |
Compare | cmp [reg] [reg:val] | Compares the value of a register with another value, pushes 1 onto stack if equal otherwise pushes 0 |
Jump | jmp [reg:label:val] | Jumps to line number of value or to label |
Jump If Equal | jeq [reg:label:val] | Pops top value off stack and jumps to line number of value or to label if value is not 0 |
Jump Not Equal | jne [reg:label:val] | Pops top value off stack and jumps to line number of value or to label if value is 0 |
UTC | utc [reg] | Sets value of register to milliseconds since UNIX Epoch |
Sleep | slp [val] | Sleeps for given number of seconds |
Dump | dmp | Dumps stack to console for debugging |
Character | chr [reg:val] | Outputs value as ASCII character |
Output | out [reg:val] | Outputs value |
Halt | hlt | Halts the program |
*Register set to null after instruction is executed
CommentsComments are started with a semicolon (;) and run until the end of the line
LabelsLabels are defined by any alphabetic character and underscores [a-zA-Z_] followed by a colon (:) Labels can be jumped to using jump instructions
set a 1
cmp a 1
jeq skip
out a
skip:
The `out a` instruction is skipped by the `jeq skip`
NumbersNumbers can be represented as any real number (Ex: 5, 3.14, -15) or as a hexideciaml number using a dollar sign (Ex: $FA, $B08A)
MacrosMacros can be used as simple function calls or to create things such as for and while loops (See Github: libs/loops.asm (https://github.com/NihilisticPuffin/CCasm/blob/main/libs/loops.asm))
Macros are defined with the `%macro` keyword followed by an identifier and closed with the `%end` key word
Macros are called with the % followed by the identifier
%macro hello
chr 72
chr 101
chr 108
chr 108
chr 111
chr 44
chr 32
chr 87
chr 111
chr 114
chr 108
chr 100
chr 33
chr 10
%end
%hello ; Prints 'Hello, World!' followed by a newline
Macros also support arguments by following the identifier with the number of arguments the macro takes
Macro arguments can be accessed within a macro using a % followed by the argument number
%macro say
out a
chr 10
%end
%macro for 3
for_loop:
%3 ; %3 gets replaced with %say
cmp %1 %2 ; %1 and %2 get replaced with a and 5 respectively
jeq for_exit
str %1
psh 1
add
ldr %1
jmp for_loop
for_exit:
%end
set a 1
%for a 5 %say ; Loops from value of a to 5 and outputs a
ExamplesWARNING: CCasm is currently under-development, examples may not use the latest features or may be broken by future updates
Timer.asm
utc d
%macro elapsed
; Calculate time elapsed in milliseconds
utc e
str e
str d
sub
ldr e
out e
chr 109 ;m
chr 115 ;s
chr 10 ;\n
%end
Fibonacci
#import "Timer.asm"
set a 90 ; Iterations
set b 0 ; Previous Number
set c 1 ; Current Number
fib:
cmp a 1
jeq _exit
str a
psh 1
sub
ldr a
psh b
psh c
add
mov c b
ldr c
jmp fib
_exit:
out c ; Print output of Fibonacci
chr 10 ; \n
%elapsed
FizzBuzz
#import "Timer.asm"
set a 1
set c 17
%macro fizz
chr 70
chr 105
chr 122
chr 122
%end
%macro buzz
chr 66
chr 117
chr 122
chr 122
%end
loop:
cpy a b
str b
dup
dup
psh 15
mod
ldr b
cmp b 0
jeq fizz_buzz
psh 3
mod
ldr b
cmp b 0
jeq fizz
psh 5
mod
ldr b
cmp b 0
jeq buzz
out a
chr 10
step:
cmp a c
jeq exit
str a
psh 1
add
ldr a
jmp loop
fizz:
%fizz
chr 10
jmp step
buzz:
%buzz
chr 10
jmp step
fizz_buzz:
%fizz
%buzz
chr 10
jmp step
exit:
%elapsed
I love the fact that you took the awesome amount of time to make this impressive interpeter for assembly language features in ComputerCraft! Keep it up, and this library may be released for the new version for COSC.