diff --git a/site/topics/programming/counting_10.esap b/site/topics/programming/counting_10.esap new file mode 100644 index 00000000..3016b486 --- /dev/null +++ b/site/topics/programming/counting_10.esap @@ -0,0 +1,11 @@ +LDAD 0 +SAVA 0xF +LDAR 0xF +LDBD 1 +ADAB +SAVA 0xF +OUTU 0xF +LDBD 10 +SUAB +JMPS 0x2 +HALT \ No newline at end of file diff --git a/site/topics/programming/counting_10.hex b/site/topics/programming/counting_10.hex new file mode 100644 index 00000000..a592c8cc --- /dev/null +++ b/site/topics/programming/counting_10.hex @@ -0,0 +1,12 @@ +v2.0 raw +0x20 +0x5f +0x1f +0x41 +0x70 +0x5f +0xdf +0x4a +0x80 +0xb2 +0xf0 diff --git a/site/topics/programming/programming-assembly.rst b/site/topics/programming/programming-assembly.rst index 1194ae8d..3f1348b8 100644 --- a/site/topics/programming/programming-assembly.rst +++ b/site/topics/programming/programming-assembly.rst @@ -155,6 +155,69 @@ Count to 10 +Count to 10 +=========== + +* With the use of the assembler, the programs are easier to write and understand +* This is important as solving complex problems is challenging enough as is + + * The tedium of machine code only makes solving complex problems that much more difficult + + +* Consider the more complex problem of counting to 10 + + * Output the numbers ``1`` to ``10`` + * This may sound simple, but it is challenging when programming at such a low level + + +* This problem combines two of the previous problems + + * Counting forever + + * requires looping + + + * Checking if a value is less than 10 + + * requires branching + + +* Below is the assembly and corresponding machine code for a solution to this problem + + .. list-table:: Count to 10 + :header-rows: 1 + :align: center + + * - Assembly + - Machine Code + + * - .. literalinclude:: counting_10.esap + :language: text + :lineno-match: + + - .. literalinclude:: counting_10.hex + :language: text + :lineno-match: + + +* Notice how the count value must be preserved before the subtraction can happen +* Here, the ``JMPS`` instruction is used like a kind of while loop + + * While the count is less than 10, jump + + +* Note that, when running the program, it will appear to count ``0`` -- ``10`` + + * This is due to the simulator and how the output register starts with a ``0`` + + +* Additionally, the starting instructions of ``LDAD 0`` and ``SAVA 0xF`` could have been excluded + + * The simulator initializes RAM with ``0``\s + * However, having clear and intentional code is preferred + + + For Next Time =============