B32 Assembler

Toplevel

<symbol>   
Top Level Form

Defines the symbol symbol as a label to the current position.

dw  &rest expressions
Top Level Form
ds  integer
Top Level Form

Allocates data space of size integer words.

defconstant  symbol expression
Top Level Form

Defines the symbol symbol as a constant with value value.

tagbody  &body body
Top Level Form
seg  name
Top Level Form
text   
Top Level Form
data   
Top Level Form
bss   
Top Level Form

The following assembled output goes to the segment named by name. Predefined segments are :text, :data, and :bss. For convienience the short hands (text), (data), and (bss) are provided.

defmacro  name lambda-list &body body
Top Level Form

Defines a macro named name. Like in Common Lisp lambda-list is a macro lambda list. The body is evaluated as Common Lisp. Macros are applied both in top level forms and in expressions.

To return more than one insturction consider using tagbody.

Example

(defmacro lodi (addr)
  "A <- [[addr]] -- indirect load"
  `(tagbody
     (lod ',addr)
     (ior '(lod 0))     ;build instruction
     (sto (+ @ 1))      ;put it ahead
     (lod 0)))          ;to be filled.

Expressions

quote  expression
Expression

Evaluates to the address of a memory word, where the value of expression is stored. The assembler takes some effort to avoid duplicate storage for the same constant to be allocated.

Constants are allocated to the end of the text segment.

@   
Expression

The current program address.

seg  name &optional (offset 0)
Expression

Evaluates to the address of the segment named by name (usually :text, :bss, or :data) plus the offset offset.

Arithmetric

All the following arithmetric expressions behave like their Common Lisp counterpart. The result is coerced to an integer an then the lower 32-bits are used.

+  &rest numbers
Expression
*  &rest numbers
Expression
-  first &rest numbers
Expression
/  first &rest numbers
Expression
floor  x y
Expression
ceiling  x y
Expression
truncate  x y
Expression
mod  x y
Expression
rem  x y
Expression
logior  &rest numbers
Expression
logxor  &rest numbers
Expression
logand  &rest numbers
Expression
lognot  number
Expression
ldb  byte integer
Expression
ash  integer count
Expression

Instructions as Expressions

Instructions are recognized as expressions, which might be handy in constructing instructions. Example:

;; A <- [A]
(ior '(lod 0))
(sto (+ @ 1))
(lod 0)

Example

The simulator has its virtual TTY at address #xF00000. A write to that address would output a character and a read would read one. The tty is assumed to use UTF-8 encoding.

Let us write a chargen program:

(defconstant +tty+ #xF00000)    ;Our virtual TTY

    (:text)
chargen
    (lod '#\space)
    (sto curchar)

loop
    (lod curchar)           ;get char
    (sto +tty+)             ;write it to TTY
    (add '1)                ;increment
    (sto curchar)           ;save
    ;; Test for end
    (sub '127)
    (jnz loop)

    (jmp chargen)           ;And do everything again

    ;; space for CURCHAR
    (:bss)
curchar
    (ds 1)                  ;Space for one word