Pseudo-Pascal to ASM compiler

A compiler written in 8086 assembly in 2006, running in your browser

DOS screen

Click the screen and type

8086

Variables

Stack

The language

A small Pascal-like language with 16-bit signed integers. Keywords are case-insensitive, ; starts a comment, and every block has its own closing keyword.

program
    var a, b, i          ; integers (also used as characters)
    array v[10]          ; arrays of integers, indexed from 0
    $hello = "Hello!"    ; string constants, for writeStr
begin
    a = 7 * (b + 1) / 2  ; + - * /   and parentheses
    if a > 3 & !(b = 0)   ; = <> < > <= >=   & | ~(xor) !(not)
        writeStr($hello)
    else
        writeInt(a)
    endif
    while i < 10
        v[i] = i * i
        i = i + 1
    endwhile
    loop 3               ; repeats 3 times, using CX
        write('*')
    endloop
    read(a)              ; a key, without echo
    readInt(b)           ; a number, with echo
    write(a)             ; a character
    newline
    masm                 ; assembly, copied as is
        mov ax, a
        shl ax, 1
        mov b, ax
    endmasm
end.

It's a single-pass recursive descent compiler, after Jack Crenshaw's Let's Build a Compiler, with no syntax tree: the parser emits assembly as it goes. The code treats the 8086 as an accumulator machine with a stack: AX is the accumulator, and PUSH AX / POP BX hold the left operand of each binary operator. How it works.