Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions site/topics/programming/counting_10.esap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
LDAD 0
SAVA 0xF
LDAR 0xF
LDBD 1
ADAB
SAVA 0xF
OUTU 0xF
LDBD 10
SUAB
JMPS 0x2
HALT
12 changes: 12 additions & 0 deletions site/topics/programming/counting_10.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
v2.0 raw
0x20
0x5f
0x1f
0x41
0x70
0x5f
0xdf
0x4a
0x80
0xb2
0xf0
63 changes: 63 additions & 0 deletions site/topics/programming/programming-assembly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============

Expand Down