CCasm - An assembly language for computercraft

Started by NihilisticPuffin, Apr 17, 2023, 11:26 AM

Previous topic - Next topic
CCasm is an interpreted assembly-like language for computercraft

Github
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 Set

NameInstructionUse
Setset [reg] [val]Sets the value of a register
Copycpy [reg] [reg]Copies the value of the first register into the second register
Movemov [reg] [reg]Moves the value of the first register into the second register*
Pushpsh [reg:val]Pushes value onto stack
PoppopPops top value off stack
DuplicatedupDuplicates the top value on the stack
SwapswpSwaps the top two values on the stack
Storestr [reg]Stores the value of a register onto the stack*
Loadldr [reg]Pops top value off stack and loads into register
AddaddPops top two values off stack adds them and pushes result onto stack
SubtractsubPops top two values off stack subtracts them and pushes result onto stack
MultiplymulPops top two values off stack multiplies them and pushes result onto stack
DividedivPops top two values off stack divides them and pushes result onto stack
ModulomodPops top two values off stack preforms a modulo operation and pushes result onto stack
Comparecmp [reg] [reg:val]Compares the value of a register with another value, pushes 1 onto stack if equal otherwise pushes 0
Jumpjmp [reg:label:val]Jumps to line number of value or to label
Jump If Equaljeq [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 Equaljne [reg:label:val]Pops top value off stack and jumps to line number of value or to label if value is 0
UTCutc [reg]Sets value of register to milliseconds since UNIX Epoch
Sleepslp [val]Sleeps for given number of seconds
DumpdmpDumps stack to console for debugging
Characterchr [reg:val]Outputs value as ASCII character
Outputout [reg:val]Outputs value
HalthltHalts the program
*Register set to null after instruction is executed

Comments
Comments are started with a semicolon (;) and run until the end of the line

Labels
Labels 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`

Numbers
Numbers can be represented as any real number (Ex: 5, 3.14, -15) or as a hexideciaml number using a dollar sign (Ex: $FA, $B08A)

Macros
Macros can be used as simple function calls or to create things such as for and while loops (See Github: 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

Examples

WARNING: 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

QuickMuffin8782

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.
GOTTA GO FAST!!!! - Sunrise Studios now calling testers! Check the post!
Gitlab - Join my official discord! - SS Discord