Assembly¶
Assembly code provides a way to specify instructions for the computer to perform in a more human readable from than machine code
Assembly operations and arguments to specify their behaviour can be saved in an assembly file and passed to an assembler to convert them to machine code that the computer can then execute.
These are the operations and other assembly constructs that can be used to create a valid assembly file.
Arithmetic Operations¶
ADD¶
The ADD operation adds the value held in the specified module (or a
constant) to the accumulator. The ALU flags generated by this operation are
stored.
The possible usages are:
ADD AADD BADD CADD CONST
ADDC¶
The ADDC operation adds the value held in the specified module (or a
constant) to the accumulator. If the carry flag is high, an additional 1 is
added. The ALU flags generated by this operation are stored.
The possible usages are:
ADDC AADDC BADDC CADDC CONST
SUB¶
The SUB operation subtracts the value held in the specified module (or a
constant) from the accumulator. The ALU flags generated by this operation are
stored.
The possible usages are:
SUB ASUB BSUB CSUB CONST
SUBB¶
The SUBB operation subtracts the value held in the specified module (or a
constant) from the accumulator. If the borrow flag is high an additional 1 is
subtracted. The ALU flags generated by this operation are
stored.
The possible usages are:
SUBB ASUBB BSUBB CSUBB CONST
LSHIFT¶
The LSHIFT operation moves all the bits in the specified module one place
to the left (toward the most significant bit) in place. A zero is added in the
rightmost (least significant bit) place. The ALU flags generated by this
operation are stored.
The possible usages are:
LSHIFT ACCLSHIFT ALSHIFT BLSHIFT C
LSHIFTC¶
The LSHIFTC operation moves all the bits in the specified module one place
to the left (toward the most significant bit) in place. If the carry flag is
high, a 1 is added in the rightmost (least significant bit) place. If the
flag is low, the rightmost place is left as 0. The ALU flags generated by this
operation are stored.
The possible usages are:
LSHIFT ACCLSHIFT ALSHIFT BLSHIFT C
INCR¶
The INCR operation adds one to the given argument (in place). The ALU flags
generated by this operation are stored.
The possible usages are:
INCR ACCINCR AINCR BINCR C
DECR¶
The DECR operation subracts one from the given argument (in place). The ALU
flags generated by this operation are stored.
The possible usages are:
DECR ACCDECR ADECR BDECR C
Data Operations¶
COPY¶
The COPY operation copies the value from a source module to a destination
module. This overwrites the current value of the destination register. It
requires a single machine code byte in program memory.
It is used by specifying the source module as the first argument and the destination module as the second.
The possible usages are:
COPY ACC ACOPY ACC BCOPY ACC CCOPY ACC SPCOPY A ACCCOPY A BCOPY A CCOPY A SPCOPY B ACCCOPY B ACOPY B CCOPY B SPCOPY C ACCCOPY C ACOPY C BCOPY C SPCOPY PC ACCCOPY PC ACOPY PC BCOPY PC CCOPY PC SPCOPY SP ACCCOPY SP ACOPY SP BCOPY SP C
LOAD¶
The LOAD operation loads a value from data memory into a module.
It is used by specifying the position in memory as the first argument and the destination module as the second. The position in memory can be a module or a constant and is encased in square parentheses.
The possible usages are:
LOAD [ACC] ACCLOAD [ACC] ALOAD [ACC] BLOAD [ACC] CLOAD [A] ACCLOAD [A] ALOAD [A] BLOAD [A] CLOAD [B] ACCLOAD [B] ALOAD [B] BLOAD [B] CLOAD [C] ACCLOAD [C] ALOAD [C] BLOAD [C] CLOAD [PC] ACCLOAD [PC] ALOAD [PC] BLOAD [PC] CLOAD [SP] ACCLOAD [SP] ALOAD [SP] BLOAD [SP] CLOAD [CONST] ACCLOAD [CONST] ALOAD [CONST] BLOAD [CONST] C
STORE¶
The STORE operation stores a value from a module in memory.
It is used by specifying the module as the first argument and the location in memory as the second. The location in memory can be a module or a constant and is encased in square parentheses.
The possible usages are:
STORE ACC [ACC]STORE ACC [A]STORE ACC [B]STORE ACC [C]STORE ACC [PC]STORE ACC [SP]STORE ACC [CONST]STORE A [ACC]STORE A [A]STORE A [B]STORE A [C]STORE A [PC]STORE A [SP]STORE A [CONST]STORE B [ACC]STORE B [A]STORE B [B]STORE B [C]STORE B [PC]STORE B [SP]STORE B [CONST]STORE C [ACC]STORE C [A]STORE C [B]STORE C [C]STORE C [PC]STORE C [SP]STORE C [CONST]STORE PC [ACC]STORE PC [A]STORE PC [B]STORE PC [C]STORE PC [PC]STORE PC [SP]STORE PC [CONST]
PROGLOAD¶
The PROGLOAD operation loads a value from program memory into the ACC module.
It is used by specifying the position in program memory as the single argument. The position in memory can be a module or a constant and is encased in square parentheses.
The possible usages are:
PROGLOAD [ACC]PROGLOAD [A]PROGLOAD [B]PROGLOAD [C]PROGLOAD [PC]PROGLOAD [SP]PROGLOAD [CONST]
PROGSTORE¶
The PROGSTORE operation stores the value in the ACC module into program memory.
It is used by specifying the position in program memory as the single argument. The position in memory can be a module or a constant and is encased in square parentheses.
The possible usages are:
PROGSTORE [ACC]PROGSTORE [A]PROGSTORE [B]PROGSTORE [C]PROGSTORE [PC]PROGSTORE [SP]PROGSTORE [CONST]
PUSH¶
The PUSH operation takes a value in a module and add it to the top of the stack.
The stack grows downwards in memory, starting at address 255. The SP module is used to keep track of the top of the stack. The value in SP points at the current top of the stack. SP should be initialised to 0 so that the first push will put the value at address 255. The push first decrements SP, then stores the value in memory at the new value of SP.
The flags generated while decrementing SP are not stored.
The push operation is used by specifying the module to push onto the stack as the first and only argument.
The possible usages are:
PUSH ACCPUSH APUSH BPUSH CPUSH PC
POP¶
The POP operation takes the value from the top of the stack and copies it to
a module.
The value currently at top of the stack is pointed to by SP. A pop will first copy the value at the top of the stack into a module, then increment SP (the stack grows downwards in memory from address 255).
The flags generated while incrementing SP are not stored.
The pop is used by specifying the module to pop the value off the stack into as the first and only argument.
The possible usages are:
POP ACCPOP APOP BPOP C
SET¶
The SET operation will set a module in the computer to a given
constant value.
It is used by specifying a module as the first argument, then the value to set it to as a constant.
It requires two machine code bytes in program memory. Consider the SET_ZERO operation if the constant is zero.
The possible usages are:
SET ACC CONSTSET A CONSTSET B CONSTSET C CONSTSET SP CONST
SET_ZERO¶
The SET_ZERO operation will set a module in the computer to zero.
The ALU flags generated by this operation are not stored.
It is used by specifying a module as the first and only argument.
The possible usages are:
SET_ZERO ACCSET_ZERO ASET_ZERO BSET_ZERO C
Program Control Operations¶
NOOP¶
The NOOP does nothing - no module transfers occur on the bus for one instruction cycle.
The possible usages are:
NOOP
JUMP¶
The JUMP operation will set the program counter to a value.
The possible usages are:
JUMP ACCJUMP AJUMP BJUMP CJUMP SPJUMP CONSTJUMP [ACC]JUMP [A]JUMP [B]JUMP [C]JUMP [SP]JUMP [PC]JUMP [CONST]
JUMP_IF_LT_ACC¶
The JUMP_IF_LT_ACC operation will set the program counter (jump)
to the value of a given constant (second argument) if the value of the
first argument (module or constant) is less than the accumulator.
The instruction generates and stores (clobbers) the ALU flags.
The possible usages are:
JUMP_IF_LT_ACC A CONSTJUMP_IF_LT_ACC B CONSTJUMP_IF_LT_ACC C CONSTJUMP_IF_LT_ACC PC CONSTJUMP_IF_LT_ACC SP CONSTJUMP_IF_LT_ACC CONST CONST
JUMP_IF_LTE_ACC¶
The JUMP_IF_LTE_ACC operation will set the program counter (jump)
to the value of a given constant (second argument) if the value of the
first argument (module or constant) is less than or equal to the
accumulator.
The instruction generates and stores (clobbers) the ALU flags.
The possible usages are:
JUMP_IF_LTE_ACC A CONSTJUMP_IF_LTE_ACC B CONSTJUMP_IF_LTE_ACC C CONSTJUMP_IF_LTE_ACC PC CONSTJUMP_IF_LTE_ACC SP CONSTJUMP_IF_LTE_ACC CONST CONST
JUMP_IF_EQ_ACC¶
The JUMP_IF_EQ_ACC operation will set the program counter (jump)
to the value of a given constant (second argument) if the value of the
first argument (module or constant) is equal to the accumulator.
The instruction generates and stores (clobbers) the ALU flags.
The possible usages are:
JUMP_IF_EQ_ACC A CONSTJUMP_IF_EQ_ACC B CONSTJUMP_IF_EQ_ACC C CONSTJUMP_IF_EQ_ACC PC CONSTJUMP_IF_EQ_ACC SP CONSTJUMP_IF_EQ_ACC CONST CONST
JUMP_IF_GTE_ACC¶
The JUMP_IF_GTE_ACC operation will set the program counter (jump)
to the value of a given constant (second argument) if the value of the
first argument (module or constant) is greater than or equal to the
accumulator.
The instruction generates and stores (clobbers) the ALU flags.
The possible usages are:
JUMP_IF_GTE_ACC A CONSTJUMP_IF_GTE_ACC B CONSTJUMP_IF_GTE_ACC C CONSTJUMP_IF_GTE_ACC PC CONSTJUMP_IF_GTE_ACC SP CONSTJUMP_IF_GTE_ACC CONST CONST
JUMP_IF_GT_ACC¶
The JUMP_IF_GT_ACC operation will set the program counter (jump)
to the value of a given constant (second argument) if the value of the
first argument (module or constant) is greater than the accumulator.
The instruction generates and stores (clobbers) the ALU flags.
The possible usages are:
JUMP_IF_GT_ACC A CONSTJUMP_IF_GT_ACC B CONSTJUMP_IF_GT_ACC C CONSTJUMP_IF_GT_ACC PC CONSTJUMP_IF_GT_ACC SP CONSTJUMP_IF_GT_ACC CONST CONST
JUMP_IF_EQ_ZERO¶
The JUMP_IF_EQ_ZERO operation will set the program counter (jump)
to the value of a given constant (second argument) if the value of the
first argument (a module) is equal to zero.
The instruction generates and stores (clobbers) the ALU flags.
The possible usages are:
JUMP_IF_EQ_ZERO ACC CONSTJUMP_IF_EQ_ZERO A CONSTJUMP_IF_EQ_ZERO B CONSTJUMP_IF_EQ_ZERO C CONSTJUMP_IF_EQ_ZERO PC CONSTJUMP_IF_EQ_ZERO SP CONST
JUMP_IF_POSITIVE_FLAG¶
The JUMP_IF_POSITIVE_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for resulted in a positive value (when read as 2’s compliment).
The possible usages are:
JUMP_IF_POSITIVE_FLAG CONST
JUMP_IF_NEGATIVE_FLAG¶
The JUMP_IF_NEGATIVE_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for resulted in a negative value (when read as 2’s compliment).
The possible usages are:
JUMP_IF_NEGATIVE_FLAG CONST
JUMP_IF_OVERFLOW_FLAG¶
The JUMP_IF_OVERFLOW_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for resulted in an overflow.
The possible usages are:
JUMP_IF_OVERFLOW_FLAG CONST
JUMP_IF_NOT_OVERFLOW_FLAG¶
The JUMP_IF_OVERFLOW_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for did not result in an overflow.
The possible usages are:
JUMP_IF_NOT_OVERFLOW_FLAG CONST
JUMP_IF_UNDERFLOW_FLAG¶
The JUMP_IF_UNDERFLOW_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for resulted in an underflow.
The possible usages are:
JUMP_IF_UNDERFLOW_FLAG CONST
JUMP_IF_NOT_UNDERFLOW_FLAG¶
The JUMP_IF_NOT_UNDERFLOW_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for did not result in an underflow.
The possible usages are:
JUMP_IF_NOT_UNDERFLOW_FLAG CONST
JUMP_IF_ZERO_FLAG¶
The JUMP_IF_ZERO_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for resulted in an answer of zero.
The possible usages are:
JUMP_IF_ZERO_FLAG CONST
JUMP_IF_NOT_ZERO_FLAG¶
The JUMP_IF_NOT_ZERO_FLAG operation will set the program counter to the
value of a given constant if the last operation that the ALU flags were
stored for resulted in a non zero answer.
The possible usages are:
JUMP_IF_NOT_ZERO_FLAG CONST
CALL¶
The CALL operation will push the current program counter (i.e. the next
instruction to be executed) onto the stack, then set the program counter (
i.e. jump) to the value in the given module or constant.
The possible usages are:
CALL ACCCALL ACALL BCALL CCALL CONST
RETURN¶
The RETURN operation will pop the top of the stack into the program counter.
It expects to be used after having arrived at a section of assembly with the
CALL instruction.
The possible usages are:
RETURN
HALT¶
The HALT operation halts execution of the computer by stopping the clock.
The possible usages are:
HALT
Logical Operations¶
NOT¶
The NOT operation inverts all the bits of the specified module in place.
The ALU flags generated by this operation are stored.
The possible usages are:
NOT ACCNOT ANOT BNOT C
AND¶
The AND operation performs a logical AND with the value held in the
specified module (or a constant) and the accumulator. The result is stored in
the accumulator. The ALU flags generated by this operation are stored.
The possible usages are:
AND AAND BAND CAND CONST
NAND¶
The NAND operation performs a logical NAND with the value held in the
specified module (or a constant) and the accumulator. The result is stored in
the accumulator. The ALU flags generated by this operation are stored.
The possible usages are:
NAND ANAND BNAND CNAND CONST
OR¶
The OR operation performs a logical OR with the value held in the
specified module (or a constant) and the accumulator. The result is stored in
the accumulator. The ALU flags generated by this operation are stored.
The possible usages are:
OR AOR BOR COR CONST
NOR¶
The NOR operation performs a logical NOR with the value held in the
specified module (or a constant) and the accumulator. The result is stored in
the accumulator. The ALU flags generated by this operation are stored.
The possible usages are:
NOR ANOR BNOR CNOR CONST
XOR¶
The XOR operation performs a logical XOR with the value held in the
specified module (or a constant) and the accumulator. The result is stored in
the accumulator. The ALU flags generated by this operation are stored.
The possible usages are:
XOR AXOR BXOR CXOR CONST
NXOR¶
The NXOR operation performs a logical NXOR (an XOR, then inverted) with
the value held in the specified module (or a constant) and the accumulator.
The result is stored in the accumulator. The ALU flags generated by this
operation are stored.
The possible usages are:
NXOR ANXOR BNXOR CNXOR CONST
Miscellaneous Operations¶
ROT_LEFT¶
The ROT_LEFT operation moves all the bits in the number one place to the
left (most significant side), adding a 0 on the least significant side. If
the most significant bit was a 1, then after the rotation this is set back
on the least significant side.
This operation will generate and store (clobber) ALU flags.
The possible usages are:
ROT_LEFT ACCROT_LEFT AROT_LEFT BROT_LEFT C
Constants¶
Constants are values that the assembler will convert to machine code bytes for operations that require data in the machine code. For example, a jump to an explicit index in program memory, or setting a register to an explicit value.
There are 3 kinds of constants: labels, variables and numbers.
Labels¶
A label binds to the line of assembly that follows it. Once assembly is complete the label’s value is the index in program memory of the instruction byte that followed the label definition. E.g. If an assembly file looked like this:
LOAD [#123] A
ADD A
@label
SET B #42
The value of @label would be 3. The instruction byte corresponding to
SET B #42 is at program memory index 3. LOAD [#123] A takes 2 bytes,
ADD A one, and SET B #42 is the byte after that.
Labels are typically used by jump operations.
A label is a token that starts with the @ character followed by any letter or
an underscore, then any alphanumeric or an underscore. E.g.:
@label@label_1@_other_label
Labels must be unique.
A label is defined by putting it on a line by itself.
Variables¶
Variables are named aliases for indexes into data memory. They can be predeclared by putting them by themselves on a line or declared as they are used by using them as an argument.
The index for a given variable is determined by the assembler. As it parses assembly lines from the start of the file to the end, addresses are assigned to variables as they are encountered in the file. E.g. for the following assembly:
$variable1
COPY A ACC
LOAD [$variable2] A
variable1 is predeclared, variable2 is declared as it’s used. Once assembled, variable1 is an alias for 0, variable2 is an alias for 1.
A variable is a token that starts with the $ character followed by any letter or
an underscore, then any alphanumeric or an underscore. E.g.:
$variable$variable1$_other_variable
Numbers¶
Numbers are integer values. In most cases they within the range -127 to 255 (inclusive). This range comes from the minimum and maximum values that 8 bits, or 8 bits with 2’s compliment encoding can hold.
A number is a token that starts with the # character and is followed by any
valid Python integer definition. E.g.
#123(decimal)#-5(decimal)#0b00010010(binary)#-0b0101(binary)#0xA2(hex)#0o107(octal)
Comments¶
Comments are parts of the assembly file ignored by the assembler.
A comment is anything after and including // on a line until the end of the
line.