OVERVIEW
THE S16 ARCHITECTURE
The S16 processor is a fictional 16-bit Harvard stack machine for teaching purposes, which can be accessed through a browser-based simulator. It operates on 16-bit words (assumed to be unsigned for arithmetic purposes) and has the following components:
• A 16-bit program counter (only the low 12 bits are used).
• An operation stack that holds up to 256 words.
• A return stack that holds up to 256 words.
• An ALU that can perform many of the standard arithmetic, logic and comparison
operations that you would find in a higher-level programming language.
• 4K words of code memory (this means 4096).
• 4K words of data memory.
Each machine instruction is one machine word long, except for ones that take an additional 16-bit argument such as PUSH.
The execution cycle is as follows:
1. Fetch the instruction in code memory at the location indicated by the program counter.
2. Increment the program counter.
3. Decode the instruction. If it requires an operand, fetch that from code memory and in-
crement the program counter again.
4. Execute the instruction. This may change the program counter.
The all-zero word is the HALT instruction, which decrements the program counter again during execution so the processor will not proceed beyond an all-zero word, even in single-step mode in the simulator.
INSTRUCTION SET
In the following tables,
• Op is the opcode in hexadecimal.
• Mnemonic is the mnemonic to use in the assembler.
• Arg indicates if this instruction takes an argument (operand).
• Pre indicates the minimum number of words that must be on the stack before executing
this instruction. If the stack underflows, the processor halts with a fault.
• Post indicates the change in stack size after executing this instruction. If it is negative, the words to remove will always be covered by the precondition, however if it is positive then this implies an additional condition that the stack must not overflow – doing so causes a
processor fault.
• Description is a brief description of the instruction.
In assembly, anything from a semicolon ( ; ) to the end of a line is a comment. Labels are indicated by a trailing colon ( : ). Mnemonics must be capitalised.
Where an operation takes an operand, it may either be a constant in hexadecimal prefixed by a # sign, or a label. Thus “PUSH #2” pushes the constant 2, whereas “PUSH start” pushes the value of the label “start”. The assembler is a standard two-pass assembler.
HALT OPERATION 0000 HALT
STACK OPERATIONS
0001 PUSH Yes
0002 POP
0003 DUP
0004 SWAP
Pre Post Description
0 Sets the program counter back to the current inst-
ruction, then halts the processor.
Pre Post Description
+1 Pushes a constant onto the stack.
1 –1 Removes the top element from the stack.
1 +1 Duplicates the top stack element.
2 0 Swaps the top two words on the stack.
Op Mnemonic
Arg
Op Mnemonic
Arg
ARITHMETIC OPERATIONS
Op Mnemonic
Arg
Pre Post Description
0101 ADD
0103 SUB
0104 MUL
0105 MULC
0106 DIV
0107 MOD
ADDC stands for “add with carry”.
2 –1 3
2 –1
2 –1 2 0
2 –1
Pops two words off the stack, adds them and pushes the result back on the stack.
Pops two words off the stack, subtracts them and pushes the result back on the stack. The operation is X–Y where Y was the word on top of the stack.
Same as above, but multiplies the words.
Pops two words, multiplies them, then pushes the result as a pair of words, with the most significant 16 bits on the stack top.
Pops two words off the stack, performs integer division and pushes the result back on the stack. The divisor is the word on top of the stack.
Same as above, but computes the modulus.
0102
ADDC
–1
Pops three words from the stack, adds the two that were not the top word and then adds 1 if the top word was nonzero (the top word represents the carry in). Then pushes first the addition result, then an add- itional word that was 1 if there was a carry out, else 0.
2 –1
BINARY OPERATIONS
0201 AND
0202 OR
0203 XOR
0204 NAND
0205 NOT
0206 SHR
0207 SSR
0208 SHL
0209 SWE
Pre Post Description
Op Mnemonic
Arg
2 –1
2 –1 2 –1 2 –1 1 0
2 –1
2 –1
2 –1 1 0
Removes the top two words from the stack, performs the binary AND operation on them and places the result back on the stack.
Like above, but performs the binary OR.
Like above, but performs the binary XOR.
Like above, but performs the binary NAND. Removes the top word from the stack, performs binary NOT and puts the result back on the stack. Shifts the second-from-top element right by N bits, where N is the stack top element. Shifted in bits are all zeroes.
Signed shift right: same as above but shifts in the bit that used to be the most significant bit.
Same as SHR, but shifts left.
Swap endianness: swaps the low and high bytes of the stack top word.
SHL/R stands for shift left/right; SSR is
“shift signed right”.
COMPARISON AND TEST OPERATIONS
Op Mnemonic Arg Pre Post
Description
Remove two words from the stack and push 1 if they are equal, otherwise 0.
SameasaboveforX>Y. Same as above for X >= Y. SameasaboveforX