def read(register):
    """
    Get a byte from the keyboard and put it in the given register.

    This blocks.
    """

    ucode = assemble(SET, register, [0x9010])
    ucode = until(ucode, (IFE, register, 0x0))
    ucode += assemble(SET, [0x9010], 0x0)
    ucode += assemble(SET, PC, POP)
    return ucode
def memcpy():
    """
    Copy A bytes from B to C. Clobbers A.

    The copy is made back to front. No overlapping check is done.
    """

    preamble = assemble(ADD, B, A)
    preamble += assemble(ADD, C, A)
    # Top of the loop.
    ucode = assemble(SUB, A, 0x1)
    ucode += assemble(SUB, B, 0x1)
    ucode += assemble(SUB, C, 0x1)
    ucode += assemble(SET, [C], [B])
    ucode = until(ucode, (IFN, A, 0x0))
    # And return.
    ucode += assemble(SET, PC, POP)
    return preamble + ucode
def memcmp():
    """
    Put a length in A, two addresses in B and C, and fill A with whether
    they match (non-zero) or don't match (zero).
    """

    # Save X.
    preamble = assemble(SET, PUSH, X)
    preamble += assemble(SET, X, 0x0)
    preamble += assemble(ADD, B, A)
    preamble += assemble(ADD, C, A)
    # Top of the loop.
    ucode = assemble(SUB, B, 0x1)
    ucode += assemble(SUB, C, 0x1)
    ucode += assemble(IFE, [B], [C])
    ucode += assemble(BOR, X, 0xffff)
    ucode = until(ucode, (IFN, A, 0x0))
    ucode += assemble(SET, A, X)
    ucode += assemble(XOR, A, 0xffff)
    # Restore X.
    ucode += assemble(SET, X, POP)
    ucode += assemble(SET, PC, POP)
    return preamble + ucode
Beispiel #4
0
# Deep primitives.

ma.prim("read", read(A))
ma.prim("write", write(A))

# Top of the line: Go back to the beginning of the string.
ucode = assemble(SET, B, 0x0)
ucode += assemble(SET, C, ma.workspace)
# Read a character into A.
ucode += call(ma.asmwords["read"])
ucode += assemble(SET, [C], A)
ucode += assemble(ADD, B, 0x1)
ucode += assemble(ADD, C, 0x1)
# If it's a space, then we're done. Otherwise, go back to reading things from
# the keyboard.
ucode = until(ucode, (IFN, 0x20, [C]))
ucode += assemble(SET, C, ma.workspace)
ma.prim("word", ucode)

preamble = assemble(SET, C, 0x0)
ucode = assemble(MUL, C, 10)
ucode += assemble(SET, X, [A])
ucode += assemble(SUB, X, ord("0"))
ucode += assemble(ADD, C, X)
ucode += assemble(ADD, A, 0x1)
ucode += assemble(SUB, B, 0x1)
ucode = until(ucode, (IFE, B, 0x0))
ma.prim("snumber", preamble + ucode)

# Compiling words.