# 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 an machine.

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.

Table 1. Example of the difference in the MSB of a received and decoded word
MSBTypeTranslation

000010

operand

000010 = $2_{10}$

010010

instruction

010010 = DEL

110010

character

110010 = 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.

Possible instructions and what to do

## Character

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

Possible characters

## 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.