Skip to content

Commit aebc5e3

Browse files
🎓 Topic assembly programming --- Counting to 10 example (#228)
1 parent f9d5635 commit aebc5e3

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
LDAD 0
2+
SAVA 0xF
3+
LDAR 0xF
4+
LDBD 1
5+
ADAB
6+
SAVA 0xF
7+
OUTU 0xF
8+
LDBD 10
9+
SUAB
10+
JMPS 0x2
11+
HALT
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
v2.0 raw
2+
0x20
3+
0x5f
4+
0x1f
5+
0x41
6+
0x70
7+
0x5f
8+
0xdf
9+
0x4a
10+
0x80
11+
0xb2
12+
0xf0

site/topics/programming/programming-assembly.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,69 @@ Count to 10
155155

156156

157157

158+
Count to 10
159+
===========
160+
161+
* With the use of the assembler, the programs are easier to write and understand
162+
* This is important as solving complex problems is challenging enough as is
163+
164+
* The tedium of machine code only makes solving complex problems that much more difficult
165+
166+
167+
* Consider the more complex problem of counting to 10
168+
169+
* Output the numbers ``1`` to ``10``
170+
* This may sound simple, but it is challenging when programming at such a low level
171+
172+
173+
* This problem combines two of the previous problems
174+
175+
* Counting forever
176+
177+
* requires looping
178+
179+
180+
* Checking if a value is less than 10
181+
182+
* requires branching
183+
184+
185+
* Below is the assembly and corresponding machine code for a solution to this problem
186+
187+
.. list-table:: Count to 10
188+
:header-rows: 1
189+
:align: center
190+
191+
* - Assembly
192+
- Machine Code
193+
194+
* - .. literalinclude:: counting_10.esap
195+
:language: text
196+
:lineno-match:
197+
198+
- .. literalinclude:: counting_10.hex
199+
:language: text
200+
:lineno-match:
201+
202+
203+
* Notice how the count value must be preserved before the subtraction can happen
204+
* Here, the ``JMPS`` instruction is used like a kind of while loop
205+
206+
* While the count is less than 10, jump
207+
208+
209+
* Note that, when running the program, it will appear to count ``0`` -- ``10``
210+
211+
* This is due to the simulator and how the output register starts with a ``0``
212+
213+
214+
* Additionally, the starting instructions of ``LDAD 0`` and ``SAVA 0xF`` could have been excluded
215+
216+
* The simulator initializes RAM with ``0``\s
217+
* However, having clear and intentional code is preferred
218+
219+
220+
158221
For Next Time
159222
=============
160223

0 commit comments

Comments
 (0)