Category:Computer/zero Assembly: Difference between revisions

no edit summary
(Created page with "{{stub}}{{language|Computer/zero Assembly}} Computer/zero is a minimal instruction set architecture designed to accompany [http://edmundgriffiths.com/degreezero.html Programmi...")
 
No edit summary
 
(One intermediate revision by one other user not shown)
Line 1:
{{stub}}{{language|Computer/zero Assembly}}
Computer/zero is a minimalsimple instruction set architecture designed to accompany [http://edmundgriffiths.com/degreezero.html Programming degree zero], a short tutorial on computers and machine-level programming for the absolute beginner. It is deliberately minimal, offering only eight instructions, one general-purpose register, and 32 bytes of memory. As a result, it is not only easy to learn but also very easy to implement: the core interpreter for Computer/zero machine code comes to about 15 lines in a high-level language, making it quite feasible for beginners to implement and extend it for themselves. A partly graphical browser-based implementation (written in JavaScript) is available [http://edmundgriffiths.com/czero.html here].
 
==Instruction Set==
There are only 8 instructions to use. The top 3 bits represent the instruction, the bottom 5 are the operand. Operands represent absolute memory addresses only. Therefore, the only way to add or subtract a constant is to embed it in your program code (and doing so will consume an entire extra byte.) <code>NOP</code> and <code>STP</code> don't use their operands, so you can embed arbitrary data inside these instructions to save a little space.
 
* <b>NOP</b>: <code>0b000xxxxx</code>. This instruction does nothing.
* <b>LDA x</b>: <code>0b001xxxxx</code>. Loads the 8-bit value at memory address x into the accumulator.
* <b>STA x</b>: <code>0b010xxxxx</code>. Stores the 8-bit value in the accumulator into memory address x.
* <b>ADD x</b>: <code>0b011xxxxx</code>. Adds the value at memory address x to the value in the accumulator, and the result is stored in the accumulator.
* <b>SUB x</b>: <code>0b100xxxxx</code>. Subtracts the value at memory address x from the value in the accumulator. The result is stored in the accumulator.
* <b>BRZ x</b>: <code>0b101xxxxx</code>. Branch If Zero. If the accumulator contains the value 0, execution will jump to memory address x. Otherwise, this is the same as a NOP.
* <b>JMP x</b>: <code>0b110xxxxx</code>. Execution jumps to memory address x.
* <b>STP</b>: <code>0b111xxxxx</code>. Execution of code ends.
 
==Memory==
There are only 32 bytes of memory, and this includes your program code, leaving very little memory to work with. As such, there are no function calls or recursion, since both require a call stack and much more RAM than we have here.
 
==Self-Modifying Code==
With such little RAM available, the biggest tool we have at our disposal for programming is self-modifying code. The CPU makes no distinction between data and instructions, and as such we can store values into memory before that memory is executed, in order to change what the computer will do. Here's an example:
 
<lang 6502asm>LDA 4
STA 2
ADD 3
STP
SUB 3</lang>
 
The above snippet of code never actually <code>ADDs</code> anything, since that instruction gets overwritten by <code>SUB 3</code>. And what is <code>SUB 3</code> subtracting? It's subtracting the value of <code>STP</code>, which can be encoded as any number between 0xE0 and 0xFF. Specifically, the encoding of <code>SUB 3</code> (i.e. 0b10000011) is decreased by some value between 0xE0 and 0xFF, and since all arithmetic on this CPU is modulo 256 (as is standard for nearly all 8-bit CPUs), there will be roll-over across the FF-00 boundary. Without a hardware carry flag, however, it's difficult to track this overflow.
1,489

edits