Specs - Stack Machine

As already explained in the task description, the main goal of this course is to build a robot that is able to read bar code cards containing instructions for a computing machine that operates on a stack. This so-called stack machine is capable of performing basic arithmetic and executing text-to-speech.
The following section is about the requirements and specifications for such a machine.

Info

As stack machines are Turing-complete per definition, but we don’t want to proof this for the given instruction set, let’s simply assume that stack machine here is an alias for a pushdown automaton.

The stack machine accepts 6-bit machine words as input. Depending on the most significant bit (MSB) $m$ of such word, the following five bits either represent an operand, an instruction or a character. After initialisation, the stack machine starts with an empty stack and an overflow flag set to False.

Example of the difference in the MSB of a received and decoded word

MSBTypeTranslation
000010operand000010 = $2_{10}$
010010instruction010010 = DEL
110010character110010 = O

  • $i$ - First bit (from left)
  • $j$ - Second bit (from left)

Operands

In case $i$ is not set check the second bit $j$ for $1$ or $0$. If $j$ is also not set simply take the remaining bits as the binary representation of an operand.
A single operand can have a value in the range $[0\ \dots\ 15]$.

Note that the value read shall be stored in an 8-bit integer internally. Pushing an operand resets the overflow flag to False.

Instructions

If $i$ is not set and $j$ is set you have to look up the corresponding command from the instruction set and proceed with further processing. This may include fetching the correct number of operands from memory, performing the action or calculation and in case a result is generated store it back into memory.

Note

In the following we will refer to operands on the stack by $o_{k}$. Where $k$ always stands for the current position of the element on top, i.e. the size of the stack. Any operation removes the used operand from the stack and pushes the result to it.

Bit sequenceAbbreviationShort descriptionOverflow flagOperands (popped/pushed)
010000STPEnd of execution. Does not change the stack at all.no change0/0
010001DUPDuplicates the last operand.no change1/2
010010DELRemoves the last operand.no change1/0
010011SWPSwaps the last two operands.no change2/2
010100ADDAdds $o_{k}$ to $o_{k-1}$.True if addition resulted in a carry out of the most-significant bit, False otherwise2/1
010101SUBSubtracts $o_{k}$ from $o_{k-1}$.True if subtraction resulted in a borrow out of the most-significant bit, False otherwise2/1
010110MULMultiplies $o_{k}$ with $o_{k-1}$.True if product doesn’t fit into data type, False otherwise2/1
010111DIVDivides $o_{k-1}$ by $o_{k}$.False2/1
011000EXPCalculates $o_{k-1}$^$o_{k}$^.True if product doesn’t fit into data type, False otherwise2/1
011001MODCalculates $o_{k-1}$ modulo $o_{k}$.False2/1
011010SHLShifts $o_{k-1}$ by $o_{k}$ places to the left.True if a 1 is shifted out of the operand, False otherwise2/1
011011SHRShifts $o_{k-1}$ by $o_{k}$ places to the right.False2/1
011100HEXConverts a sequence of hexadecimal into integer ($o_{k}$ to $o_{k-1}$).False2/1
011101FACCalculates factorial of $o_{k}$.True if factorial doesn’t fit into data type, False otherwise1/1
011110NOTForms the ones’ complement of $o_{k}$.False1/1
011111XORPerforms the logical XOR operation on $o_{k}$ and $o_{k-1}$ bit by bit.False2/1

Character

If $i$ is set the following 5 bits either are a character, SPEAK or a NOP command.

Bit sequenceDescription
100000NOP
100001SPEAK (Execute Text-to-Speech on decoded character list)
- Pop first item from stack (number, $l$)
- Now, pop $l$ items from the stack and combine them for TTS processing
- Execute TTS
100010WHITESPACE
100011NOP
100100A
100101B
100110C
100111D
101000E
101001F
101010G
101011H
101100I
101101J
101110K
101111L
110000M
110001N
110010O
110011P
110100Q
110101R
110110S
110111T
111000U
111001V
111010W
111011X
111100Y
111101Z
111110NOP
111111NOP

Exceptions

During the execution of code an exception may break the normal flow of the program. An exception is defined as every condition that leads to undefined behaviour.
For example, the programmer may ask the machine to perform an ADD instruction although there are no operands on the stack. Obviously, the instruction cannot be executed and the exception operand mismatch occurs. Running the program any further would not make sense in most cases, as following calculations can be expected to generate errors as well. As a consequence, the machine simply is being halted when encountering an exception.

Info

Please distinguish between undefined and other defined conditions carefully. An integer overflow for instance is accurately specified and therefore no exception.