# Examples - damn_simple_architecture

## Keyboard shortcuts

Press `←` or `→` to navigate between chapters

Press `S` or `/` to search in the book

Press `?` to show this help

Press `Esc` to hide this help

# Examples

## Library Examples

### Multiplication Library (multiply.dsa)

    // multiply.dsa
    // usage:
    //
    // include multiply "<relative path>"
    //
    // usage for multiply:
    // push (arg1)
    // push (arg0)
    // call multiply::multiply
    // pop (arg0)
    // pop (arg1)

    multiply:
        push bpr
        mov spr, bpr

        ldw bpr, rg0, 8  // load op 1
        ldw bpr, rg1, 12 // load op 2

        lli 0, acc      // initialize accumulator

    start:
        add acc, rg0, acc
        dec rg1

        cmp rg1, zero
        jgt start

    end:
        stw acc, bpr, 8  // store result for caller
        mov bpr, spr
        pop bpr
        return

### Print Library (print.dsa)

    // print.dsa
    // usage:
    //
    // include print "<relative path>"
    //
    // usage for print:
    //      push (register containing address of string)
    //      call print::print
    //      pop zero
    //
    // usage for reset:
    //      call print::reset

    dw display: 0x20000
    dw current: 0x20000

    // prints the given text to the screen.
    print:
        push bpr
        mov spr, bpr

        ldw bpr, rg0, 8    // get string address argument
        ldw current, rg1    // get current display position

    print_loop:
        ldb rg0, acc
        stb acc, rg1

        iadd rg0, 1
        iadd rg1, 1

        cmp acc, zero
        jne print_loop
        jmp end

    // return
    end:
        stw rg1, current

        mov bpr, spr
        pop bpr
        return

    // resets the cursor position on the screen
    reset:
        push bpr
        mov spr, bpr
        ldw display, rg1
        stw rg1, current
        mov bpr, spr
        pop bpr
        return

### Example Program (main.dsa)

    include print "./print.dsa"

    dw stack: 0x10000
    db string: "'To confuse your enemy, you must first confuse yourself' - Probably Sun Tzu."

    init:
        // set up a stack.
        ldw stack, bpr
        mov bpr, spr

    start:
        lwi string, rg1

        // push string address argument
        push rg1
        // call print function
        call print::print
        // clean up stack
        pop rg1

        hlt