Skip to content
toby1984 edited this page May 12, 2012 · 23 revisions

There's also an IDE now ...

I started integrating jASM16 into an IDE , check out the screenshots.

Invoking the compiler from the command-line

The Maven2 project is configured to generate a self-executable JAR that will launch the compiler.

>$ java -jar target/jasm16.jar --help
jASM_16 V0.5.1
(c) 2012 by [email protected]

Usage: [options] [-o <output file>] source1 source2 ...

-o              => output file to write generated assembly code to, otherwise code will be written to source.dcpu16
-d or --debug   => print debug output
--print         => print formatted source code along with hex dump of generated assembly
--print-symbols => print symbol table
--dump          => instead of writing generated object code to a file, write a hexdump to std out
--relaxed       => relaxed parsing (instructions are parsed case-insensitive)
-v or --verbose => print slightly more verbose output during compilation

Inspected generating bytecode

Not that I mistrust my bytecode generation, but I added a command-line switch to annotate the compiled source-code with the generated object code. Note that the assembler inlines literal values, labels and constants generated from expressions (.equ) , thus the output looks slightly different than Notch's example.

[21:37:54][tobi@schlepptop:~/schulungs_workspace/jASM_16(master)]$ java -jar target/jasm16.jar samples/specsample.dasm16 --print

; Try some basic stuff
                                        SET A,0x30          ; 7c01 0030 
                                        SET [0x1000],0x20   ; 7de1 1000 0020 
                                        SUB A,[0x1000]      ; 7803 1000 
                                        IFN A,0x10          ; c00d 
                                        SET PC,crash2       ; d9c1 

; Do a loopy thing
                                        SET I,10            ; a861 
                                        SET A,0x2000        ; 7c01 2000 
:loop (0x000c)                          SET [0x2000+I],[A]  ; 2161 2000 
                                        SUB I,1             ; 8463 
                                        IFN I,0             ; 806d 
                                        SET PC,loop         ; b1c1 

; Call a subroutine
                                        SET X,0x4           ; 9031 
                                        JSR testsub         ; d010 
                                        SET PC,crash2       ; d9c1 

:testsub (0x0014)                       SHL X,4             ; 9037 
                                        SET PC,POP          ; 61c1 

; Hang forever. X should now be 0x40 if everything went right.
:crash2 (0x0016)                        SET PC,crash2       ; d9c1 

; [*]: Note that these can be one word shorter and one cycle faster by using the short form (0x00-0x1f) of literals,
;      but my assembler doesn't support short form labels yet.
:text (0x0017)                          .dat "Hello world"; 0048 0065 006c 006c 006f 0020 0077 006f 0072 006c 0064

General syntax

Everything except labels and instructions is parsed case-insensitive (parsing opcodes can be made case-insensitive by setting ICompilerOption.RELAXED_PARSING or using the "--relaxed" command-line switch).

Expressions currently support the following operators: () + - * / << >> % < <= > >= == != (using C-style operator precedence). Expressions are generally allowed wherever a literal value is accepted (read: also .byte / .dat and .equ). Boolean expressions evaluate to either 1 (true) or 0 (false).

Recognized keywords

Keyword Example Description
.include "somefile" .include "specsample.dasm16" Include source file
include "somefile" include "specsample.dasm16" Include source file (alternative syntax)
.incbin "somefile" .incbin "pic.jpg" Include binary file
incbin "somefile" incbin "pic.jpg" Include binary file (alternative syntax)
.org ADDRESS .org 1024 Sets the starting address for subsequent instructions
.origin ADDRESS .origin 0x2000 Sets the starting address for subsequent instructions (alternative syntax)
.equ IDENTIFIER EXPRESSION .equ THE_ANSWER 42
.equ WEIRD_STUFF ((10*50)-30) << (2 % 5)
Defines a constant
:label :foo Defines a label
.label .fooBar Defines a label (alternative syntax)
label: fooBar: Defines a label (alternative syntax)
.byte .byte 1,0x02,b101101 Initializes memory with 8-bit values
dat dat 0x2000,1234,b1011101,THE_ANSWER Initializes memory with 16-bit values
.dat .dat 0x2000,1234,b1011101,THE_ANSWER , "some string" Initializes memory with 16-bit values (alternative syntax)
.word .word 0x2000,1234,b1011101,THE_ANSWER Initializes memory with 16-bit values (alternative syntax)
.bss .bss 1024 Creates zero-filled memory
reserve reserve 0x100 Creates zero-filled memory (alternative syntax)

Using the compiler as part of another Java application

The assembler was build with the intention of embedding it inside an IDE so it provides a lot of hooks and customization options for easy integration.

Check out how to embed the assembler