<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Z80 Assembly &#8211; Espamática</title>
	<atom:link href="https://espamatica.com/category/z80-assembly/feed/" rel="self" type="application/rss+xml" />
	<link>https://espamatica.com</link>
	<description>Programación en 8 bits y algo más.</description>
	<lastBuildDate>Mon, 24 Jun 2024 05:18:57 +0000</lastBuildDate>
	<language>es</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>

<image>
	<url>https://i0.wp.com/espamatica.com/wp-content/uploads/2023/11/LogoWeb.png?fit=32%2C29&#038;ssl=1</url>
	<title>Z80 Assembly &#8211; Espamática</title>
	<link>https://espamatica.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">147753459</site>	<item>
		<title>ZX Spectrum Assembly &#8211; Utilities</title>
		<link>https://espamatica.com/zx-spectrum-assembly-utilities/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-utilities/#comments</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Wed, 27 Mar 2024 16:49:44 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5490</guid>

					<description><![CDATA[This is a compilation of different routines that can be useful to deal with certain aspects of the ZX Spectrum.]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">This is a compilation of different routines that can be useful to deal with certain aspects of the ZX Spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">The entry will be updated in the future.</p>



<p class="wp-block-paragraph" style="font-size:16px">Translated with <a href="https://www.deepl.com/es/translator/" target="_blank" rel="noreferrer noopener nofollow">DeepL</a>.</p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#zx-spectrum-model">ZX Spectrum model</a>
</li>
<li><a href="#pal-vs-ntsc">PAL vs NTSC</a>

<ul>
<li><a href="#what-is-the-difference">What is the difference?</a>
</li>
<li><a href="#how-does-it-affect-us">How does it affect us?</a>
</li>
<li><a href="#how-do-we-find-out-the-system">How do we find out the system?</a>
</li>
</ul>
</li>
<li><a href="#downloads">Downloads</a>
</li></ul></div>


<h2 id="zx-spectrum-model" class="wp-block-heading has-x-large-font-size">ZX Spectrum model</h2>



<p class="has-medium-font-size wp-block-paragraph">On more than one occasion we will need to know which model of ZX Spectrum our program is going to run on, 16K, 48K or 128K, and in the latter case if it is in 48K or 128K mode.</p>



<p class="has-medium-font-size wp-block-paragraph">The way to get the ZX Spectrum model I&#8217;m going to show is based on what <a href="https://twitter.com/sergio_thepope" target="_blank" rel="noreferrer noopener nofollow">Sergio thEpOpE</a> exposed in the <a href="https://t.me/CursoBasicZX" target="_blank" rel="noreferrer noopener nofollow">Basic ZX Course</a> group on Telegram.</p>



<p class="has-medium-font-size wp-block-paragraph">Depending on the value that we find in the memory position 0x4B (75), we can know if it is a 128K model or on the contrary if it is a 16/48K model. If when reading the value of this memory position we get 0x6E (110), the program is running in a 128K model, otherwise it is running in a 16/48K model.</p>



<p class="has-medium-font-size wp-block-paragraph">If you are running on a 16/48K ZX Spectrum model, it is interesting to know which one it is, information that can be found at locations 0x5CB4 (23,732) and 0x5CB5 (23,733). In these addresses we will find the total amount of memory: 0x7FFF (32,767) in 16K models or 0xFFFFFF (65,535) in 48K models. As we can see, the second byte (the least significant one) has the same value, changing only the most significant one, which is located at memory location 0x5CB5 (23,733). </p>



<p class="has-medium-font-size wp-block-paragraph">If we read the value of this position and get 0x7F (127) we are executing in a 16K model, if we get 0xFF (255) we are executing in a 48K model. For 128K models the value is the same as for 48K models.</p>



<p class="has-medium-font-size wp-block-paragraph">If it is running on a 128K ZX Spectrum model, it is possible that it is running in 48K or 128K mode, which we can also find out. First we get the value of the memory location 0x5C3B (23,611), we divide by 16 and the result is divided by 2. If the result of this operation is equal to the integer division of the value of the memory location by 16 and the result is divided by 2, it is running in 48K mode, otherwise in 128K mode.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">Pseudocode</span><span role="button" tabindex="0" data-code="IF INT(PEEK(23611)/16)/2 = INT(INT(PEEK(32611)/16/2)
    48K model
ELSE
    128K model
END IF" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">IF INT(PEEK(23611)/16)/2 = INT(INT(PEEK(32611)/16/2)</span></span>
<span class="line"><span style="color: #002339">    48K model</span></span>
<span class="line"><span style="color: #002339">ELSE</span></span>
<span class="line"><span style="color: #002339">    128K model</span></span>
<span class="line"><span style="color: #002339">END IF</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In other words, if the result of the integer division between the value of memory location 0x5C3B (23,611) and 16 is even, it is in 48K mode, otherwise it is in 128K mode.</p>



<p class="has-medium-font-size wp-block-paragraph">In ZX Spectrum <a href="https://espamatica.com/category/z80-assembly/">assembly</a> we are allowed, with right-hand rotations, to make divisions by powers of 2 (16 = 2^4), so we would have to make four rotations to make the division by 16 and then check if the result is even, bit 0 is zero.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $cc                   ; A = 1100 1100
; We use displacements to perform the division by 16
sra  a                        ; A = A/2 =  0110 0110
sra  a                        ; A = A/4 =  0011 0011
sra  a                        ; A = A/8 =  0001 1001
sra  a                        ; A = A/16 = 0000 1100
; Check if A is even
bit  $00, a
jr   z, isEven                ; If bit 0 is 0, it is even, it skips
isOdd:
; isOdd code
isEven:
; isEven code" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, $cc                   </span><span style="color: #357B42; font-style: italic">; A = 1100 1100</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; We use displacements to perform the division by 16</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/2 =  0110 0110</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/4 =  0011 0011</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/8 =  0001 1001</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/16 = 0000 1100</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Check if A is even</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$00</span><span style="color: #002339">, a</span></span>
<span class="line"><span style="color: #002339">jr   z, isEven                </span><span style="color: #357B42; font-style: italic">; If bit 0 is 0, it is even, it skips</span></span>
<span class="line"><span style="color: #7EB233">isOdd:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; isOdd code</span></span>
<span class="line"><span style="color: #7EB233">isEven:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; isEven code</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If you notice, we have shifted bits 4 to 7 to bits 0 to 3 and bits 4 to 7 have been set to zero, finally checking if bit 0, formerly bit 4, is 0 if it is even or 1 if it is odd. We can save displacements and check bit 4 directly.</p>



<p class="has-medium-font-size wp-block-paragraph">Based on what we have seen, the routine to find out on which ZX Spectrum model we are running a program could be the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ZXModel
;
; Gets the ZX model
;
; Entry:  nothing
;
; Output: BC = ZX model
;              0 = 16K
;              1 = 48K
;              2 = 128K
;              3 = 128K in 48K mode
; Alters the value of the BC and AF registers.
ZXModel:
ld   bc, $00			; BC is used to return the value to BASIC

ld   a, ($4b)           ; A = model address
cp   $6e
jr   z, is128           ; $6E, 128, jump

is16or48:
ld   a, ($5cb5)         ; A = amount of memory, upper part
rla                     ; Rotate to the right to check if it is $BF or $FF
ret  nc                 ; NC = 16K, jump ($BF)
inc  c                  ; C+=1
ret                     ; 48K, jump

is128:
ld   c, $02             ; C = 128K
ld   a, ($5c3b)         ; A = mode address
; IF INT(PEEK 23611/16)/2 = INT(INT(PEEK 23611/16)/2) = 48K.
; Dividing by 16 is like shifting 4 times to the right,
; bit 4 is set to bit 0
; Knowing if it is divisible by two is like knowing if it is even, bit 0 = 0
; It all boils down to evaluating bit 4, if it is 0 it is 48K mode, if it is 1 it is 128K mode
bit  $04, a             ; Bit 4?
ret  nz                 ; !0, 128K mode, exit
inc  c                  ; C+=1
ret                     ; 48K mode" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ZXModel</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Gets the ZX model</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entry:  nothing</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: BC = ZX model</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              0 = 16K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              1 = 48K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              2 = 128K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              3 = 128K in 48K mode</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the BC and AF registers.</span></span>
<span class="line"><span style="color: #7EB233">ZXModel:</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$00</span><span style="color: #002339">			</span><span style="color: #357B42; font-style: italic">; BC is used to return the value to BASIC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$4b</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = model address</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$6e</span></span>
<span class="line"><span style="color: #002339">jr   z, is128           </span><span style="color: #357B42; font-style: italic">; $6E, 128, jump</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">is16or48:</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$5cb5</span><span style="color: #002339">)         </span><span style="color: #357B42; font-style: italic">; A = amount of memory, upper part</span></span>
<span class="line"><span style="color: #002339">rla                     </span><span style="color: #357B42; font-style: italic">; Rotate to the right to check if it is $BF or $FF</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nc                 </span><span style="color: #357B42; font-style: italic">; NC = 16K, jump ($BF)</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                  </span><span style="color: #357B42; font-style: italic">; C+=1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; 48K, jump</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">is128:</span></span>
<span class="line"><span style="color: #002339">ld   c, </span><span style="color: #174781">$02</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; C = 128K</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$5c3b</span><span style="color: #002339">)         </span><span style="color: #357B42; font-style: italic">; A = mode address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; IF INT(PEEK 23611/16)/2 = INT(INT(PEEK 23611/16)/2) = 48K.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Dividing by 16 is like shifting 4 times to the right,</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 4 is set to bit 0</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Knowing if it is divisible by two is like knowing if it is even, bit 0 = 0</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; It all boils down to evaluating bit 4, if it is 0 it is 48K mode, if it is 1 it is 128K mode</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$04</span><span style="color: #002339">, a             </span><span style="color: #357B42; font-style: italic">; Bit 4?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nz                 </span><span style="color: #357B42; font-style: italic">; !0, 128K mode, exit</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                  </span><span style="color: #357B42; font-style: italic">; C+=1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; 48K mode</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To test this routine, you must compile it and load it, for example, at memory address 0x7D00 (32,000), so that it will run on any ZX Spectrum model or clone. Once the routine is loaded, if you type PRINT USR 32000, the value corresponding to the ZX Spectrum model on which it is running will be printed on the screen.</p>



<p class="has-medium-font-size wp-block-paragraph">The value returned by an assembly program to the ZX Spectrum BASIC is done through the BC register.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa243f53&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa243f53" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="706" height="688" data-attachment-id="5398" data-permalink="https://espamatica.com/ensamblador-zx-spectrum-utilidades/zxmodel/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?fit=706%2C688&amp;ssl=1" data-orig-size="706,688" data-comments-opened="1" data-image-title="ZX Model" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?fit=706%2C688&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?resize=706%2C688&#038;ssl=1" alt="ZX Spectrum model" class="wp-image-5398" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?w=706&amp;ssl=1 706w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?resize=300%2C292&amp;ssl=1 300w" sizes="(max-width: 706px) 100vw, 706px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum model</figcaption></figure>



<h2 id="pal-vs-ntsc" class="wp-block-heading has-x-large-font-size">PAL vs NTSC</h2>



<p class="has-medium-font-size wp-block-paragraph">Sometimes, we may be interested in knowing if the ZX Spectrum on which our program is running is PAL or NTSC system.</p>



<h3 id="what-is-the-difference" class="wp-block-heading has-large-font-size">What is the difference?</h3>



<p class="has-medium-font-size wp-block-paragraph">In PAL system there are 50 interruptions per second, while in NTSC system the number of interruptions per second is 60.</p>



<h3 id="how-does-it-affect-us" class="wp-block-heading has-large-font-size">How does it affect us?</h3>



<p class="has-medium-font-size wp-block-paragraph">It can affect us a lot. If our program relies on interrupts for timing, for example by executing the enemy movement routine every five interrupts, on a PAL system the enemy will move five times per second, while on NTSC it will move six times per second, resulting in faster enemy movement on NTSC than on PAL, which can affect gameplay.</p>



<h3 id="how-do-we-find-out-the-system" class="wp-block-heading has-large-font-size">How do we find out the system?</h3>



<p class="has-medium-font-size wp-block-paragraph">In this case I have relied on Habisoft&#8217;s Es.pectrum emulator, which you can download from <a href="https://habisoft.com/espectrum/" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<p class="has-medium-font-size wp-block-paragraph">This wonderful emulator emulates a wide variety of ZX Spectrum models and their clones, so it&#8217;s ideal for testing the program I&#8217;ve implemented to detect if it&#8217;s PAL or NTSC, and it&#8217;s also free.</p>



<p class="has-medium-font-size wp-block-paragraph">The program is simple, activate the ZX Spectrum interrupts in mode 2 and the first time the interrupt routine is executed it sets swCount to one to indicate that the counter variable in the main loop is being incremented. The second time the interrupt routine is executed it sets swCount to two to indicate that the program should exit. The value of counter is loaded into BC so that it can be obtained from Basic.</p>



<p class="has-medium-font-size wp-block-paragraph">In short, the program counts the number of times the main loop is executed between two interrupts. Based on the test results, we could say that NTSC yields values below 100, while PAL yields values above 100.</p>



<p class="has-medium-font-size wp-block-paragraph">All this has to be treated with great care, if you look at the results you can see values that are at the limit.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(3 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; Program to find out if it's NTSC or PAL
; Compile: pasmo --name PAL_NTSC --tapbas PAL_NTSC.asm PAL_NTSC.tap
; Once loaded, run PRINT USR 32000 to see the result
; Not compatible with 16K model

; Results of PRINT USR 32768 using emulator Es.pectrum
;
; https://habisoft.com/espectrum/
;
; Sinclair ZX Spectrum:
;       48/48+                  216
;       48 NTSC                  64
;       128                     230
;       48 ar                   ¿?
;       48 se                   215
; Timex:
;       TS 2068                  64
;       TC 2068                 216
;       TC 2048                 216
;       Komputer 2086           216
; Investrónica:
;       48+ es                  215
;       128+ es                 230
;       Inves+                  230
; Amstrad:
;       +2                      230
;       +2 es                   230
;       +2 fr                   230
;       +2 ar                   ¿?
;       +2A 4.0                 230
;       +2A 4.1                 230
;       +2A 4.0 es              230
;       +2A 4.1 es              230
;       +2A ar                  ¿?
;       +3 4.0                  230
;       +3 4.1                  230
;       +3 4.0 es               230
;       +3 4.1 es               230
; Pentagon:
;       Pentagon 128            241
;       Pentagon 512            241
;       Pentagon 1024 SL        227
; ZS Rechearch:
;       Leningrad               184
;       Scorpion ZS-256         151
;       Scorpion ZS-256 Turbo+  215
; Microdigital:
;       Tk90x pt-BR              73
;       Tk90x es-AR             233
;       Tk95  pt-BR              72
;       Tk95  es-AR             233
; ATM:
;       ATM-Turbo               215
;       ATM-Turbo 2+            176
; Otros:
;       Orel BK-08               ¿?
;       Dubna 48K                15
;       HC-91                   230
;       BK-001                  105
;       Foton-Ik03               ¿?
;       Alf TV Game (Elf)        ¿?
;       Vesta IK-30             184
; Mods:
;       +3e                     230
;       +3e es                  230
;       +2e                     230
;       +2e es                  230
;       Pentagon es             241
; Virtuales:
;       Spec256 48K             216
;       Spect256 128K           230
org  $8000

; Input: prepare for interruptions
main:
ld   hl, isr            ; HL = interrupt routine address
ld   ($feff), hl        ; Store in $feff the address
ld   a, $fe             ; of routine isr
ld   i, a               ; I = $fe
im   2                  ; Go to interrupt mode 2

; Loop, waits for swCount to be 1 to start counting
; When swCount is 2, exit loop and end of program
loop:
ld   a, (swCount)       ; A = indicator to know whether to count
or   a                  ; A = 0?
jr   z, loop            ; Z = yes, jump
cp   $02                ; A = 2?
jr   z, exit            ; Z = yes, jump

ld   hl, counter        ; HL = counter address
inc  (hl)               ; Counter+=1

jr   loop               ; Loop

; Exit from the programme
; Sets interrupts to mode 1
; Sets the counter to BC so that it can be retrieved
; from BASIC
exit:
im   1                  ; Go to interrupt mode 1
ld   b, $00
ld   a, (counter)
ld   c, a               ; BC = counter, to return the result
ret                     ; Back to BASIC

; Interruptions routine
; At each interrupt it increments swCount
isr:
push hl                 ; Preserves HL

ld   hl, swCount
inc  (hl)               ; Increases the indicator to know if counting

pop  hl                 ; Retrieves HL

ei                      ; Enables interruptions
ret                     ; Exit

; Vars
swCount:                ; Indicator to know whether to count
db $00
counter:                ; Counter
db $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; Program to find out if it&#39;s NTSC or PAL</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Compile: pasmo --name PAL_NTSC --tapbas PAL_NTSC.asm PAL_NTSC.tap</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Once loaded, run PRINT USR 32000 to see the result</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Not compatible with 16K model</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Results of PRINT USR 32768 using emulator Es.pectrum</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; https://habisoft.com/espectrum/</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sinclair ZX Spectrum:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48/48+                  216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 NTSC                  64</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       128                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 ar                   ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 se                   215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Timex:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TS 2068                  64</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TC 2068                 216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TC 2048                 216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Komputer 2086           216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Investrónica:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48+ es                  215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       128+ es                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Inves+                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Amstrad:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2                      230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 es                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 fr                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 ar                   ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.0                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.1                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.0 es              230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.1 es              230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A ar                  ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.0                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.1                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.0 es               230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.1 es               230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Pentagon:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 128            241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 512            241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 1024 SL        227</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ZS Rechearch:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Leningrad               184</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Scorpion ZS-256         151</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Scorpion ZS-256 Turbo+  215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Microdigital:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk90x pt-BR              73</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk90x es-AR             233</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk95  pt-BR              72</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk95  es-AR             233</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ATM:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       ATM-Turbo               215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       ATM-Turbo 2+            176</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Otros:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Orel BK-08               ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Dubna 48K                15</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       HC-91                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       BK-001                  105</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Foton-Ik03               ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Alf TV Game (Elf)        ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Vesta IK-30             184</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Mods:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3e                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3e es                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2e                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2e es                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon es             241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Virtuales:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Spec256 48K             216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Spect256 128K           230</span></span>
<span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: prepare for interruptions</span></span>
<span class="line"><span style="color: #7EB233">main:</span></span>
<span class="line"><span style="color: #002339">ld   hl, isr            </span><span style="color: #357B42; font-style: italic">; HL = interrupt routine address</span></span>
<span class="line"><span style="color: #002339">ld   ($feff), hl        </span><span style="color: #357B42; font-style: italic">; Store in $feff the address</span></span>
<span class="line"><span style="color: #002339">ld   a, $fe             </span><span style="color: #357B42; font-style: italic">; of routine isr</span></span>
<span class="line"><span style="color: #002339">ld   i, a               </span><span style="color: #357B42; font-style: italic">; I = $fe</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">2</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Go to interrupt mode 2</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Loop, waits for swCount to be 1 to start counting</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; When swCount is 2, exit loop and end of program</span></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   a, (swCount)       </span><span style="color: #357B42; font-style: italic">; A = indicator to know whether to count</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                  </span><span style="color: #357B42; font-style: italic">; A = 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, </span><span style="color: #7B30D0">loop</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Z = yes, jump</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2?</span></span>
<span class="line"><span style="color: #002339">jr   z, exit            </span><span style="color: #357B42; font-style: italic">; Z = yes, jump</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, counter        </span><span style="color: #357B42; font-style: italic">; HL = counter address</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)               </span><span style="color: #357B42; font-style: italic">; Counter+=1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">loop</span><span style="color: #002339">               </span><span style="color: #357B42; font-style: italic">; Loop</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Exit from the programme</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sets interrupts to mode 1</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sets the counter to BC so that it can be retrieved</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; from BASIC</span></span>
<span class="line"><span style="color: #7EB233">exit:</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">1</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Go to interrupt mode 1</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #002339">ld   a, (counter)</span></span>
<span class="line"><span style="color: #002339">ld   c, a               </span><span style="color: #357B42; font-style: italic">; BC = counter, to return the result</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Back to BASIC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Interruptions routine</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; At each interrupt it increments swCount</span></span>
<span class="line"><span style="color: #7EB233">isr:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl                 </span><span style="color: #357B42; font-style: italic">; Preserves HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, swCount</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)               </span><span style="color: #357B42; font-style: italic">; Increases the indicator to know if counting</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl                 </span><span style="color: #357B42; font-style: italic">; Retrieves HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ei                      </span><span style="color: #357B42; font-style: italic">; Enables interruptions</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Exit</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Vars</span></span>
<span class="line"><span style="color: #7EB233">swCount:</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; Indicator to know whether to count</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">counter:</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; Counter</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span></code></pre></div>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa244e0e&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa244e0e" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" decoding="async" width="642" height="556" data-attachment-id="5405" data-permalink="https://espamatica.com/ensamblador-zx-spectrum-utilidades/pal_ntsc/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?fit=642%2C556&amp;ssl=1" data-orig-size="642,556" data-comments-opened="1" data-image-title="PAL_NTSC" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum &amp;#8211; PAL vs NTSC&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?fit=642%2C556&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?resize=642%2C556&#038;ssl=1" alt="ZX Spectrum - PAL vs NTSC" class="wp-image-5405" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?w=642&amp;ssl=1 642w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?resize=300%2C260&amp;ssl=1 300w" sizes="(max-width: 642px) 100vw, 642px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum &#8211; PAL vs NTSC</figcaption></figure>



<h2 id="downloads" class="wp-block-heading has-x-large-font-size">Downloads</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="http://tinyurl.com/22srfwmz" rel="nofollow noopener" target="_blank">ZX Spectrum model</a></li>



<li class="has-medium-font-size"><a href="http://tinyurl.com/22qwfmwf" target="_blank" rel="noreferrer noopener nofollow">PAL vs NTSC</a></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-utilities/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5490</post-id>	</item>
		<item>
		<title>ZX Spectrum screen</title>
		<link>https://espamatica.com/zx-spectrum-screen/</link>
					<comments>https://espamatica.com/zx-spectrum-screen/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Wed, 13 Dec 2023 07:19:19 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5319</guid>

					<description><![CDATA[Screen layout In this chapter we are going to talk about the screen layout of the ZX Spectrum. ZX Spectrum]]></description>
										<content:encoded><![CDATA[
<h2 id="screen-layout" class="wp-block-heading has-large-font-size">Screen layout</h2>



<p class="has-medium-font-size wp-block-paragraph">In this chapter we are going to talk about the screen layout of the ZX Spectrum.</p>



<p class="wp-block-paragraph" style="font-size:16px">Translated with <a href="https://www.deepl.com/es/translator/" target="_blank" rel="noreferrer noopener nofollow">DeepL</a>.</p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#screen-layout">Screen layout</a>
</li>
<li><a href="#zx-spectrum-graphic-area">ZX Spectrum graphic area</a>
</li>
<li><a href="#zx-spectrum-attributes-area">ZX Spectrum attributes area</a>
</li>
<li><a href="#routines">Routines</a>
</li>
<li><a href="#memory-address-from-character-coordinates">Memory address from character coordinates</a>
</li>
<li><a href="#character-coordinate-from-memory-address">Character coordinate from memory address</a>
</li>
<li><a href="#next-row-memory-address">Next row memory address</a>
</li>
<li><a href="#previous-row-memory-address">Previous row memory address</a>
</li>
<li><a href="#next-scanline">Next scanline</a>
</li>
<li><a href="#previous-scanline">Previous Scanline</a>
</li>
<li><a href="#attribute-address-from-character-coordinates">Attribute address from character coordinates</a>
</li>
<li><a href="#character-coordinates-from-attribute-address">Character coordinates from attribute address</a>
</li>
<li><a href="#attribute-address-from-graphic-area-address">Attribute address from graphic area address</a>
</li>
<li><a href="#graphic-area-address-from-attribute-address">Graphic area address from attribute address</a>
</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul></div>


<h2 id="zx-spectrum-graphic-area" class="wp-block-heading has-large-font-size">ZX Spectrum graphic area</h2>



<p class="has-medium-font-size wp-block-paragraph">The screen of the ZX Spectrum has a resolution of 256*192 pixels, or what is the same 32 columns and 192 scanlines. With each byte we can configure 8 pixels, those at one will have the color of the ink (INK) and those at zero the color of the background (PAPER); 256 pixels / 8 pixels per byte = 32 columns. The graphic area occupies 6,144 bytes ($1800) and starts at memory address 16,384 ($4000).</p>



<p class="has-medium-font-size wp-block-paragraph">One of the peculiarities of the ZX Spectrum is the way the graphic area is distributed. Each character consists of 64 pixels, or 8&#215;8 pixels. Each line of the character is called a scanline. If we paint on the first scanline of a character and then add 32 to the memory position, the logic tells us that we are on the next scanline of the character, but it is not so because we are actually on the first scanline of the character just below the one we are on.</p>



<p class="has-medium-font-size wp-block-paragraph">If we paint position $4000, the first byte of the screen, and then we want to paint in the first scanline of row 8 of the screen, the logic says that we would only have to add 8 times 32 ($20) to position $4000, so the first scanline of the first character of row 8 would be in position $4100. But this is not the case since position $4100 corresponds to the second scanline of the first character on the screen.</p>



<p class="has-medium-font-size wp-block-paragraph">This behavior is due to the fact that the ZX Spectrum screen is divided into thirds. Each of the thirds has 8 rows and each of the rows has 8 scanlines. The first third goes from memory location $4000 to $47FF, the second third from $4800 to $4FFF and the third third third from $5000 to $57FF.</p>



<p class="has-medium-font-size wp-block-paragraph">Although this may seem strange at first, when you see the composition that the bytes take when making up a memory location, it all makes sense. The composition of the bytes is as follows:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph">010T TSSS RRRC CCCC</p>



<p class="has-medium-font-size wp-block-paragraph">Where 010 is always fixed, TT represents the third (from 0 to 2), SSS represents the scanline within the character (from 0 to 7), RRR represents the row within the third (from 0 to 7) and CCCCCC represents the column (from 0 to 31).</p>



<p class="has-medium-font-size wp-block-paragraph">If we look at the row, from 0 to 23, bits 0, 1 and 3 always indicate the row within the third, and bits 4 and 5 the third, so TTRRR is equivalent to the line in character coordinates (from 0 to 23).</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">Row </span><span style="color: #174781">0</span><span style="color: #002339">:  </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">0000</span><span style="color: #002339"> Row </span><span style="color: #174781">0</span><span style="color: #002339"> of third </span><span style="color: #174781">0</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">7</span><span style="color: #002339">:  </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">0111</span><span style="color: #002339"> Row </span><span style="color: #174781">7</span><span style="color: #002339"> of third </span><span style="color: #174781">0</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">8</span><span style="color: #002339">:  </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">1000</span><span style="color: #002339"> Row </span><span style="color: #174781">0</span><span style="color: #002339"> of third </span><span style="color: #174781">1</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">15</span><span style="color: #002339">: </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">1111</span><span style="color: #002339"> Row </span><span style="color: #174781">7</span><span style="color: #002339"> of third </span><span style="color: #174781">1</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">16</span><span style="color: #002339">: </span><span style="color: #174781">0001</span><span style="color: #002339"> </span><span style="color: #174781">0000</span><span style="color: #002339"> Row </span><span style="color: #174781">0</span><span style="color: #002339"> of third </span><span style="color: #174781">2</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">23</span><span style="color: #002339">: </span><span style="color: #174781">0001</span><span style="color: #002339"> </span><span style="color: #174781">0111</span><span style="color: #002339"> Row </span><span style="color: #174781">7</span><span style="color: #002339"> of third </span><span style="color: #174781">2</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To see all of the above, let&#8217;s perform a simple example that fills the bytes of the first column with a simple $AA pattern (1001 1001).</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org  $8000
ld   hl, $4000    ; HL = VideoRAM start address
ld   bc, $20      ; BC = value to be added to move to the next character
ld   a, $08       ; A = to draw the first scanline of the first third of the first third
loop:
ld   (hl), $aa    ; Load on screen the pattern 10011001
add  hl, bc       ; Advances HL to the next row
dec  a            ; Decrease A
jr   nz, loop     ; Loop until A reaches 0
ret               ; Back to Basic
end  $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$8000</span></span>
<span class="line"><span style="color: #002339">ld   hl, </span><span style="color: #174781">$4000</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; HL = VideoRAM start address</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$20</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; BC = value to be added to move to the next character</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$08</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; A = to draw the first scanline of the first third of the first third</span></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), $aa    </span><span style="color: #357B42; font-style: italic">; Load on screen the pattern 10011001</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  hl, bc       </span><span style="color: #357B42; font-style: italic">; Advances HL to the next row</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  a            </span><span style="color: #357B42; font-style: italic">; Decrease A</span></span>
<span class="line"><span style="color: #002339">jr   nz, </span><span style="color: #7B30D0">loop</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Loop until A reaches 0</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">               </span><span style="color: #357B42; font-style: italic">; Back to Basic</span></span>
<span class="line"><span style="color: #002339">end  </span><span style="color: #174781">$8000</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If we run the program, it draws the pattern $AA (10101010) in the first scanline of the first 8 rows.</p>



<p class="has-medium-font-size wp-block-paragraph">To see the ZX Spectrum screen layout, just change the LD A line, $08 and load multiples of $08; $08, $10, $18, $20, $28, $30, $38, $40, $40, $48….</p>



<p class="has-medium-font-size wp-block-paragraph">Until we reach $48, we will not see how the first scanline of the 8 rows of the second third is painted.</p>



<h2 id="zx-spectrum-attributes-area" class="wp-block-heading has-large-font-size">ZX Spectrum attributes area</h2>



<p class="has-medium-font-size wp-block-paragraph">Following the graphic area (pixels) is the attribute area, which runs from address 22,528 ($5800) to address 23,295 ($5AFF), with a length of 768 ($300) bytes.</p>



<p class="has-medium-font-size wp-block-paragraph">Each byte in the attribute area sets the attributes of a character (8&#215;8 pixels), which is the reason for the famous attribute clash.</p>



<p class="has-medium-font-size wp-block-paragraph">The format of each byte of the attribute area is as follows:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>FBPPPIII</strong></p>



<p class="has-medium-font-size wp-block-paragraph">Where F is FLASH, B = BRIGHT, P = PAPER and I = INK.</p>



<p class="has-medium-font-size wp-block-paragraph">In the case of the attributes area the distribution is linear, so if we are in the byte of the attributes of column 0, row 0 of the screen and add 32 we are in the attributes of column 0, row 1.</p>



<p class="has-medium-font-size wp-block-paragraph">The composition of the memory addresses of the attribute area is as follows:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>0101 10TT RRRC CCCC</strong></p>



<p class="has-medium-font-size wp-block-paragraph">Where 0101 10 is always fixed, TT represents the third (from 0 to 2), RRR represents the row within the third (from 0 to 7) and CCCCC represents the column (from 0 to 31). Again TTRRR is equivalent to the row in character coordinates (from 0 to 23).</p>



<h2 id="routines" class="wp-block-heading has-large-font-size">Routines</h2>



<p class="has-medium-font-size wp-block-paragraph">From here, we are going to see routines that can help you when working with the ZX Spectrum screen. Keep in mind that these routines do not control if the coordinates are outside the screen area.</p>



<h2 id="memory-address-from-character-coordinates" class="wp-block-heading has-large-font-size">Memory address from character coordinates</h2>



<p class="has-medium-font-size wp-block-paragraph">The CharCoord2PointerHR routine takes character coordinates and returns the corresponding memory address.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; CharCoord2PointerHR: Gets the memory address
; corresponding to the specified character coordinates. 
; 
; Entrada: B -&gt; Y-coordinate (0 a 23).
;          C -&gt; X-coordinate (0 a 31).
; 
; Salida: HL -&gt; Memory address corresponding to the
;               coordinates (010T TSSS RRRC CCCC). 
;
; Alters the value of AF and HL registers.
; ----------------------------------------------------------------- 
CharCoord2PointerHR:
ld	a, b      ; A = row. Bits 0, 1 and 2 row. Bits 3 and 4 third.
and $18       ; It stays with the third, 0001 1000. 
              ; Scanline always 0.
or 	$40       ; Add fixed part, 0100 0000.
ld 	h, a      ; H = A. High part of calculated direction.
ld 	a, b      ; A = row.
and $07       ; It stays with the row inside the third.
rrca
rrca
rrca          ; Rotate three times to pass value to bits 5, 6 and 7.Rotate three times to pass value to bits 5, 6 and 7.
or 	c         ; Add column.
ld 	l, a      ; L = A. Lower part of calculated direction.
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; CharCoord2PointerHR: Gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the specified character coordinates. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: B -&gt; Y-coordinate (0 a 23).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;          C -&gt; X-coordinate (0 a 31).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Memory address corresponding to the</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               coordinates (010T TSSS RRRC CCCC). </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">CharCoord2PointerHR:</span></span>
<span class="line"><span style="color: #002339">ld	a, b      </span><span style="color: #357B42; font-style: italic">; A = row. Bits 0, 1 and 2 row. Bits 3 and 4 third.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; It stays with the third, 0001 1000. </span></span>
<span class="line"><span style="color: #002339">              </span><span style="color: #357B42; font-style: italic">; Scanline always 0.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> 	</span><span style="color: #174781">$40</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; Add fixed part, 0100 0000.</span></span>
<span class="line"><span style="color: #002339">ld 	h, a      </span><span style="color: #357B42; font-style: italic">; H = A. High part of calculated direction.</span></span>
<span class="line"><span style="color: #002339">ld 	a, b      </span><span style="color: #357B42; font-style: italic">; A = row.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; It stays with the row inside the third.</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca          </span><span style="color: #357B42; font-style: italic">; Rotate three times to pass value to bits 5, 6 and 7.Rotate three times to pass value to bits 5, 6 and 7.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> 	c         </span><span style="color: #357B42; font-style: italic">; Add column.</span></span>
<span class="line"><span style="color: #002339">ld 	l, a      </span><span style="color: #357B42; font-style: italic">; L = A. Lower part of calculated direction.</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To test this routine we can pass it positions and then load a pattern at the returned address to see where it draws.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org $8000

ld b, $10                     ; Load in B the row.
ld c, $10                     ; Load in C the column.
call CharCoord2PointerHR      ; Calculates memory location.
ld (hl), $ff                  ; Activates all pixels in the
                              ; memory location.

inc b                         ; Increases row.
inc c                         ; Increases column.
call CharCoord2PointerHR      ; Calculates memory location.
ld (hl), $ff                  ; Activates all pixels in the
                              ; memory location.

inc b                         ; Increases row.
dec c                         ; Increases column.
call CharCoord2PointerHR      ; Calculates memory location.
ld (hl), $ff                  ; Activates all pixels in the
                              ; memory location.

ret

end	 $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld b, </span><span style="color: #174781">$10</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Load in B the row.</span></span>
<span class="line"><span style="color: #002339">ld c, </span><span style="color: #174781">$10</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Load in C the column.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CharCoord2PointerHR      </span><span style="color: #357B42; font-style: italic">; Calculates memory location.</span></span>
<span class="line"><span style="color: #002339">ld (hl), $ff                  </span><span style="color: #357B42; font-style: italic">; Activates all pixels in the</span></span>
<span class="line"><span style="color: #002339">                              </span><span style="color: #357B42; font-style: italic">; memory location.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> b                         </span><span style="color: #357B42; font-style: italic">; Increases row.</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> c                         </span><span style="color: #357B42; font-style: italic">; Increases column.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CharCoord2PointerHR      </span><span style="color: #357B42; font-style: italic">; Calculates memory location.</span></span>
<span class="line"><span style="color: #002339">ld (hl), $ff                  </span><span style="color: #357B42; font-style: italic">; Activates all pixels in the</span></span>
<span class="line"><span style="color: #002339">                              </span><span style="color: #357B42; font-style: italic">; memory location.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> b                         </span><span style="color: #357B42; font-style: italic">; Increases row.</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339"> c                         </span><span style="color: #357B42; font-style: italic">; Increases column.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CharCoord2PointerHR      </span><span style="color: #357B42; font-style: italic">; Calculates memory location.</span></span>
<span class="line"><span style="color: #002339">ld (hl), $ff                  </span><span style="color: #357B42; font-style: italic">; Activates all pixels in the</span></span>
<span class="line"><span style="color: #002339">                              </span><span style="color: #357B42; font-style: italic">; memory location.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end	 </span><span style="color: #174781">$8000</span></span></code></pre></div>



<h2 id="character-coordinate-from-memory-address" class="wp-block-heading has-large-font-size">Character coordinate from memory address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHR2CharCoord routine takes a memory address and returns the corresponding character coordinates.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ---------------------------------------------------------------- 
; PointerHR2CharCoord: Obtiene las coordenadas de carácter
; correspondientes a la dirección de memoria especificada.
;
; Entrada: HL -&gt; Memory pointer. 010T TSSS RRRC CCCC.
;
; Salida: B -&gt; Y-coordinate (0 a 23).
;         C -&gt; X-coordinate (0 a 31).
;
; Alters the value of AF and BC registers.  
; ---------------------------------------------------------------- 
PointerHR2CharCoord: 
ld a, h     ; A = upper part of memory address. 010T TSSS.
and $18     ; Keeps the third. Bits 4 and 5 of the Y coordinate.
            ; Scanline always at 0, they are character coordinates.
ld b, a     ; B = A.

ld a, l     ; A = lower part of the memory address. RRRC CCCC.
and $e0     ; It stays with the row inside the third.
rrca
rrca
rrca        ; Rotate three times to pass value to bits 0, 1 and 3.
or b        ; Add bits 4 and 5.
ld b, a     ; B = A. Calculated Y-coordinate.

ld a, l     ; A = lower part of the memory address. RRRC CCCC.
and $1f     ; It keeps the column.
ld c, a     ; C = A. Calculated X-coordinate.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHR2CharCoord: Obtiene las coordenadas de carácter</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; correspondientes a la dirección de memoria especificada.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Memory pointer. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: B -&gt; Y-coordinate (0 a 23).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;         C -&gt; X-coordinate (0 a 31).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and BC registers.  </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHR2CharCoord:</span><span style="color: #002339"> </span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of memory address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the third. Bits 4 and 5 of the Y coordinate.</span></span>
<span class="line"><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Scanline always at 0, they are character coordinates.</span></span>
<span class="line"><span style="color: #002339">ld b, a     </span><span style="color: #357B42; font-style: italic">; B = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the memory address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $e0     </span><span style="color: #357B42; font-style: italic">; It stays with the row inside the third.</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca        </span><span style="color: #357B42; font-style: italic">; Rotate three times to pass value to bits 0, 1 and 3.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> b        </span><span style="color: #357B42; font-style: italic">; Add bits 4 and 5.</span></span>
<span class="line"><span style="color: #002339">ld b, a     </span><span style="color: #357B42; font-style: italic">; B = A. Calculated Y-coordinate.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the memory address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$1f</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; It keeps the column.</span></span>
<span class="line"><span style="color: #002339">ld c, a     </span><span style="color: #357B42; font-style: italic">; C = A. Calculated X-coordinate.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<h2 id="next-row-memory-address" class="wp-block-heading has-large-font-size">Next row memory address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRNextLine routine takes a memory address and returns the memory address corresponding to the next row.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRNextLine: gets the memory address
; corresponding to the next row.
;
; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; Dirección de la fila siguiente.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRNextLine:
ld a, l     ; A = lower part of the address. RRRC CCCC.
add a, $20  ; Add one line (RRRC CCCC + 0010 0000).
ld l, a     ; L = A.
ret nc      ; If there is no carry, the row follows between 0 
            ; and 7, it exits.

; There is carry-over, the third of the screen has to be changed.
ld a, h     ; A = upper part of memory address. 010T TSSS.
add a, $08  ; Add one third (010T TSSS + 0000 1000).
ld h, a     ; H = A.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRNextLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the next row.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Dirección de la fila siguiente.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRNextLine:</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$20</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one line (RRRC CCCC + 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339"> nc      </span><span style="color: #357B42; font-style: italic">; If there is no carry, the row follows between 0 </span></span>
<span class="line"><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; and 7, it exits.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; There is carry-over, the third of the screen has to be changed.</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of memory address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$08</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one third (010T TSSS + 0000 1000).</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<h2 id="previous-row-memory-address" class="wp-block-heading has-large-font-size">Previous row memory address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRPreviousLine routine takes a memory address and returns the memory address corresponding to the previous row.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRPreviousLine: gets the memory address
; corresponding to the previous row.
;
; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; Dirección de la fila anterior.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRPreviousLine:
ld a, l     ; A = lower part of the address. RRRC CCCC.
and $e0     ; It keeps the row part (1110 0000).
jr z, PointerHRPreviousLine_continue   ; If line is 0,
                                       ; change of third.

; No change of third.
ld a, l     ; A = lower part of the address, RRRC CCCC.
sub $20     ; Subtract one row (RRRC CCCC - 0010 0000).
ld l, a     ; L = A. Calculated address.

ret

PointerHRPreviousLine_continue:
; It is necessary to change the third party.
ld a, l     ; A = lower part of the address. RRRC CCCC.
or $e0      ; Set the row to 7 (RRRC CCCC or 1110 0000).
ld l, a     ; L = A. Lower part of the calculated address.

ld a, h     ; A = upper part of the address. 010T TSSS.
sub $08     ; Subtract one third (010T TSSS - 0000 1000).
ld h, a     ; H = A. Upper part of the calculated address.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRPreviousLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the previous row.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Dirección de la fila anterior.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousLine:</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $e0     </span><span style="color: #357B42; font-style: italic">; It keeps the row part (1110 0000).</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRPreviousLine_continue   </span><span style="color: #357B42; font-style: italic">; If line is 0,</span></span>
<span class="line"><span style="color: #002339">                                       </span><span style="color: #357B42; font-style: italic">; change of third.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No change of third.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address, RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one row (RRRC CCCC - 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A. Calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousLine_continue:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; It is necessary to change the third party.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> $e0      </span><span style="color: #357B42; font-style: italic">; Set the row to 7 (RRRC CCCC or 1110 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A. Lower part of the calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$08</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one third (010T TSSS - 0000 1000).</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A. Upper part of the calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<h2 id="next-scanline" class="wp-block-heading has-large-font-size">Next scanline</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRNextScanLine routine takes a memory address and returns the address of the next scanline.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRNextScanLine: gets the memory address
; corresponding to the next scanline.
;
; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; address of the next scanline.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRNextScanLine:
ld a, h     ; A = upper part of the address. 010T TSSS.
and $07     ; Keeps the scanline.
cp $07      ; Check if scanline is 7.
jr z, PointerHRNextScanLine_continue     ; Yes, change of line.

; Scanline is not 7.
inc h       ; Increases the scanline by 1 and exits.

ret

PointerHRNextScanLine_continue:
; The row must be changed.
ld a, l     ; A = lower part of the address. RRRC CCCC.
add a, $20  ; Add one line (RRRC CCCC + 0010 0000).
ld l, a     ; L = A.
ld a, h     ; A = upper part of the address. 010T TSSS.
jr nc, PointerHRNextScanLine_end  ; If there is no carriage, skip
                                  ; to finish the calculation.

; There is carriage, it is necessary to change the third party.
add a, $08  ; Add one to the third (010T TSSS + 0000 1000).

PointerHRNextScanLine_end:
and $f8     ; Keeps the fixed part and the third part.
            ; Set the scanline to 0.
ld h, a     ; H = A. Calculated address.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRNextScanLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the next scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; address of the next scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRNextScanLine:</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the scanline.</span></span>
<span class="line"><span style="color: #002339">cp </span><span style="color: #174781">$07</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; Check if scanline is 7.</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRNextScanLine_continue     </span><span style="color: #357B42; font-style: italic">; Yes, change of line.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Scanline is not 7.</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> h       </span><span style="color: #357B42; font-style: italic">; Increases the scanline by 1 and exits.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRNextScanLine_continue:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; The row must be changed.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$20</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one line (RRRC CCCC + 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #002339">jr nc, PointerHRNextScanLine_end  </span><span style="color: #357B42; font-style: italic">; If there is no carriage, skip</span></span>
<span class="line"><span style="color: #002339">                                  </span><span style="color: #357B42; font-style: italic">; to finish the calculation.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; There is carriage, it is necessary to change the third party.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$08</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one to the third (010T TSSS + 0000 1000).</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRNextScanLine_end:</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $f8     </span><span style="color: #357B42; font-style: italic">; Keeps the fixed part and the third part.</span></span>
<span class="line"><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Set the scanline to 0.</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A. Calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To test the routine, let&#8217;s use a small program to see how it draws. Contrary to what we saw in the first example, now we paint a scanline followed by the one right behind it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org $8000

ld hl, $4000    ; HL = VideoRAM start address.
ld b, $08       ; B = to draw 8 scanlines.

loop:
ld (hl), $aa    ; Loads the $AA pattern on screen.
call PointerHRNextScanLine  ; Advances to next scanline.
djnz loop       ; Loop until B reaches 0.

ret

end	 $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld hl, </span><span style="color: #174781">$4000</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; HL = VideoRAM start address.</span></span>
<span class="line"><span style="color: #002339">ld b, </span><span style="color: #174781">$08</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; B = to draw 8 scanlines.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld (hl), $aa    </span><span style="color: #357B42; font-style: italic">; Loads the $AA pattern on screen.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PointerHRNextScanLine  </span><span style="color: #357B42; font-style: italic">; Advances to next scanline.</span></span>
<span class="line"><span style="color: #002339">djnz </span><span style="color: #7B30D0">loop</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; Loop until B reaches 0.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end	 </span><span style="color: #174781">$8000</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If we run the program, it draws the $AA pattern on the first character. The first example did it on the first scanline of the first 8 rows.</p>



<p class="has-medium-font-size wp-block-paragraph">As we did in the first example, we can load multiples of $08 in B to see how the program is drawing; $08, $10, $18, $20, $28, $30, $38, $40, $48….</p>



<h2 id="previous-scanline" class="wp-block-heading has-large-font-size">Previous Scanline</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRPreviousScanLine routine takes a memory address and returns the memory address corresponding to the previous scanline.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRPreviousScanLine: gets the memory address
; corresponding to the previous scanline.
;
; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; address of previous scanline.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRPreviousScanLine:
ld a, h     ; A = upper part of the address. 010T TSSS.
and $07     ; Keeps the scanline.
or a        ; Check if it is at 0.
jr z, PointerHRPreviousScanLine_continue  ; Yes, change line.

; No change of line.
dec h       ; Decrements the scanline and exits.

ret

PointerHRPreviousScanLine_continue:
ld a, l     ; A = lower part of the address. RRRC CCCC.
and $e0     ; Keeps the row part.
or a        ; Check if it is at 0.
jr z, PointerHRPreviousScanLine_change  ; Yes, change third.

; No need to change third.
ld a, l     ; A = lower part of the address. RRRC CCCC.
sub $20     ; Subtract one row, (RRRC CCCC - 0010 0000).
ld l, a     ; L = A.
jr PointerHRPreviousScanLine_end  ; Skip to end of calculation.

PointerHRPreviousScanLine_change:
; Need to change third.
ld a, l     ; A = lower part of the address. RRRC CCCC.
or $e0      ; Sets the line to 7.
ld l, a     ; L = A.

ld a, h     ; A = upper part of the address. 010T TSSS.
sub $08     ; Subtract one to the third.
ld h, a     ; H = A.

PointerHRPreviousScanLine_end:
ld a, h     ; A = upper part of the address. 010T TSSS.
and $F8     ; Keeps the fixed part and the third part.
or $07      ; Set the scanline to 7.
ld h, a     ; H = A.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRPreviousScanLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the previous scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; address of previous scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine:</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the scanline.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> a        </span><span style="color: #357B42; font-style: italic">; Check if it is at 0.</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRPreviousScanLine_continue  </span><span style="color: #357B42; font-style: italic">; Yes, change line.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No change of line.</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339"> h       </span><span style="color: #357B42; font-style: italic">; Decrements the scanline and exits.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine_continue:</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $e0     </span><span style="color: #357B42; font-style: italic">; Keeps the row part.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> a        </span><span style="color: #357B42; font-style: italic">; Check if it is at 0.</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRPreviousScanLine_change  </span><span style="color: #357B42; font-style: italic">; Yes, change third.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No need to change third.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one row, (RRRC CCCC - 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"><span style="color: #002339">jr PointerHRPreviousScanLine_end  </span><span style="color: #357B42; font-style: italic">; Skip to end of calculation.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine_change:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Need to change third.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> $e0      </span><span style="color: #357B42; font-style: italic">; Sets the line to 7.</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$08</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one to the third.</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine_end:</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $F8     </span><span style="color: #357B42; font-style: italic">; Keeps the fixed part and the third part.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; Set the scanline to 7.</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And in order to test the routine, another simple program.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org $8000

ld hl, $57ff    ; HL = VideoRAM end address.
ld b, $08       ; B = to draw 8 scanlines.

loop:
ld (hl), $aa    ; Loads the $AA pattern on the screen.
call PointerHRPreviousScanLine  ; Back to previous scanline.
djnz loop       ; Loop until B reaches 0.

ret

end	 $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld hl, </span><span style="color: #174781">$57ff</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; HL = VideoRAM end address.</span></span>
<span class="line"><span style="color: #002339">ld b, </span><span style="color: #174781">$08</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; B = to draw 8 scanlines.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld (hl), $aa    </span><span style="color: #357B42; font-style: italic">; Loads the $AA pattern on the screen.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PointerHRPreviousScanLine  </span><span style="color: #357B42; font-style: italic">; Back to previous scanline.</span></span>
<span class="line"><span style="color: #002339">djnz </span><span style="color: #7B30D0">loop</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; Loop until B reaches 0.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end	 </span><span style="color: #174781">$8000</span></span></code></pre></div>



<h2 id="attribute-address-from-character-coordinates" class="wp-block-heading has-large-font-size">Attribute address from character coordinates</h2>



<p class="has-medium-font-size wp-block-paragraph">The CharCoord2PointerAttr routine takes character coordinates and gets the corresponding attribute memory address.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; CharCoord2PointerAttr: takes character coordinates and obtains
; the memory address of the corresponding attribute.
;
; Entrada: B -&gt; Y-coordinate
;          C -&gt; X-coordinate
;
; Salida: HL -&gt; Attribute memory address.
;               0101 10TT RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
CharCoord2PointerAttr:
ld   a, b    ; Load row in A
and  $18     ; Keeps the third bits
rrca
rrca
rrca         ; Pass them to bits 0 and 1
or   $58     ; Adds the fixed bits of the high part of the address
ld   h, a    ; H = 0101 10TT
ld   a, b    ; Load row in A
and  $07     ; Keeps the bits of the row
rrca
rrca
rrca         ; Pass them to bits 5, 6, and 7
or   c       ; Adds the bits of the column
ld   l, a    ; L = RRRC CCCC

ret          ; HL = 0101 10TT RRRC CCCC (attribute address)" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; CharCoord2PointerAttr: takes character coordinates and obtains</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; the memory address of the corresponding attribute.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: B -&gt; Y-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;          C -&gt; X-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">CharCoord2PointerAttr:</span></span>
<span class="line"><span style="color: #002339">ld   a, b    </span><span style="color: #357B42; font-style: italic">; Load row in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$18</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the third bits</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Pass them to bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$58</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Adds the fixed bits of the high part of the address</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; H = 0101 10TT</span></span>
<span class="line"><span style="color: #002339">ld   a, b    </span><span style="color: #357B42; font-style: italic">; Load row in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$07</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the row</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Pass them to bits 5, 6, and 7</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   c       </span><span style="color: #357B42; font-style: italic">; Adds the bits of the column</span></span>
<span class="line"><span style="color: #002339">ld   l, a    </span><span style="color: #357B42; font-style: italic">; L = RRRC CCCC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = 0101 10TT RRRC CCCC (attribute address)</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Social networks can be wonderful; <a href="https://twitter.com/NathanielSalis" target="_blank" rel="noreferrer noopener nofollow">NathanielSalis</a> made me see that in this routine we can save bytes and clock cycles by dispensing with three rotations. The code would look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; CharCoord2PointerAttr: takes character coordinates and obtains
; the memory address of the corresponding attribute.
;
; Entrada: B -&gt; Y-coordinate
;          C -&gt; X-coordinate
;
; Salida: HL -&gt; Attribute memory address.
;               0101 10TT RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
CharCoord2PointerAttr:
ld   a, b    ; Load row in A
; Before keeping the bits of the third, we carry out the rotations
rrca
rrca         ; Passes the bits of the third to bits 0 and 1
rrca         ; and those of the row to bits 5, 6 and 7
ld   l, a    ; Load the result in L
and  $03     ; A = bits of the third
or   $58     ; Adds the fixed bits of the high part of the address
ld   h, a    ; H = 0101 10TT
ld   a, l    ; A = row in bits 5, 6 and 7 and third in bits 0 and 1
and  $e0     ; Keeps the bits of the line
or   c       ; Adds the bits of the column
ld   l, a    ; L = RRRC CCCC

ret          ; HL = 0101 10TT RRRC CCCC (attribute address)" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; CharCoord2PointerAttr: takes character coordinates and obtains</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; the memory address of the corresponding attribute.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: B -&gt; Y-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;          C -&gt; X-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">CharCoord2PointerAttr:</span></span>
<span class="line"><span style="color: #002339">ld   a, b    </span><span style="color: #357B42; font-style: italic">; Load row in A</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Before keeping the bits of the third, we carry out the rotations</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Passes the bits of the third to bits 0 and 1</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; and those of the row to bits 5, 6 and 7</span></span>
<span class="line"><span style="color: #002339">ld   l, a    </span><span style="color: #357B42; font-style: italic">; Load the result in L</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; A = bits of the third</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$58</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Adds the fixed bits of the high part of the address</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; H = 0101 10TT</span></span>
<span class="line"><span style="color: #002339">ld   a, l    </span><span style="color: #357B42; font-style: italic">; A = row in bits 5, 6 and 7 and third in bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $e0     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the line</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   c       </span><span style="color: #357B42; font-style: italic">; Adds the bits of the column</span></span>
<span class="line"><span style="color: #002339">ld   l, a    </span><span style="color: #357B42; font-style: italic">; L = RRRC CCCC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = 0101 10TT RRRC CCCC (attribute address)</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The first version of the routine occupies 18 bytes and takes 75 cycles to execute, while the second occupies 16 bytes and takes 67 cycles to execute, so we save two bytes and 8 cycles.</p>



<h2 id="character-coordinates-from-attribute-address" class="wp-block-heading has-large-font-size">Character coordinates from attribute address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerAttr2CharCoord routine takes a memory address of an attribute and returns the corresponding character coordinates.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerAttr2CharCoord: takes an attribute memory address 
; and returns the corresponding character coordinates.
;
; Entrada: HL -&gt; Attribute memory address.
;                0101 10TT RRRC CCCC.
;
; Salida:   B -&gt; Y-coordinate
;           C -&gt; X-coordinate
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerAttr2CharCoord:
ld   a, h    ; Loads the upper part of the address in A
and  $03     ; Keeps the bits of the third
rlca
rlca
rlca         ; Passes them to bits 3 and 4
ld   b, a    ; B = 000TT000
ld   a, l    ; Load the lower part of the address in A
and  $e0     ; Keeps the bits in the row
rlca
rlca
rlca         ; Passes them to bits 0, 1 and 2
or   b       ; Add the third
ld   b, a    ; B = TTCCC (Y-coordinate)
ld   a, l    ; Load the lower part of the address in A
and  $1f     ; It keeps the bits of the column
ld   c, a    ; C = 000CCCCC (X-coordinate)

ret          ; BC = character coordinates" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerAttr2CharCoord: takes an attribute memory address </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; and returns the corresponding character coordinates.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida:   B -&gt; Y-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;           C -&gt; X-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerAttr2CharCoord:</span></span>
<span class="line"><span style="color: #002339">ld   a, h    </span><span style="color: #357B42; font-style: italic">; Loads the upper part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the third</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 3 and 4</span></span>
<span class="line"><span style="color: #002339">ld   b, a    </span><span style="color: #357B42; font-style: italic">; B = 000TT000</span></span>
<span class="line"><span style="color: #002339">ld   a, l    </span><span style="color: #357B42; font-style: italic">; Load the lower part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $e0     </span><span style="color: #357B42; font-style: italic">; Keeps the bits in the row</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 0, 1 and 2</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   b       </span><span style="color: #357B42; font-style: italic">; Add the third</span></span>
<span class="line"><span style="color: #002339">ld   b, a    </span><span style="color: #357B42; font-style: italic">; B = TTCCC (Y-coordinate)</span></span>
<span class="line"><span style="color: #002339">ld   a, l    </span><span style="color: #357B42; font-style: italic">; Load the lower part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$1f</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; It keeps the bits of the column</span></span>
<span class="line"><span style="color: #002339">ld   c, a    </span><span style="color: #357B42; font-style: italic">; C = 000CCCCC (X-coordinate)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; BC = character coordinates</span></span></code></pre></div>



<h2 id="attribute-address-from-graphic-area-address" class="wp-block-heading has-large-font-size">Attribute address from graphic area address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHR2PointerAttr routine takes a memory address from the graphic area and returns the corresponding attribute memory address.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHR2PointerAttr: takes a memory address of the graphic area 
; and returns the memory address of the corresponding attribute.
;
; Entrada: HL -&gt; Memory address of the graphic area.
;                010T TSSS RRRC CCCC.
;
; Salida:  HL -&gt; Attribute memory address.
;                0101 10TT RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHR2PointerAttr:
ld   a, h    ; Loads the upper part of the address in A
and  $18     ; Keeps the bits of the third
rrca
rrca
rrca         ; Passes them to bits 0 and 1
or   $58     ; Add the fixed part
ld   h, a    ; A = 010110TT

ret          ; HL = attribute memory address" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHR2PointerAttr: takes a memory address of the graphic area </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; and returns the memory address of the corresponding attribute.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Memory address of the graphic area.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida:  HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHR2PointerAttr:</span></span>
<span class="line"><span style="color: #002339">ld   a, h    </span><span style="color: #357B42; font-style: italic">; Loads the upper part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$18</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the third</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$58</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Add the fixed part</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; A = 010110TT</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = attribute memory address</span></span></code></pre></div>



<h2 id="graphic-area-address-from-attribute-address" class="wp-block-heading has-large-font-size">Graphic area address from attribute address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerAttr2PointerHR routine takes an attribute memory address and returns the memory address of the graphic area corresponding to scanline 0.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerAttr2PointerHR: takes a memory address of attribute
; and returns the memory address of the corresponding graphic area
; to the first scanline.
;
; Entrada: HL -&gt; Attribute memory address.
;                0101 10TT RRRC CCCC.
;
; Salida:  HL -&gt; Memory address of the graphic area.
;                010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerAttr2PointerHR:
ld   a, h    ; Loads the upper part of the address in A
and  $03     ; Keeps the bits of the third
rlca
rlca
rlca         ; Passes them to bits 0 and 1
or   $40     ; Add the fixed part
ld   h, a    ; A = 010TT000

ret          ; HL = address of the first scanline" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerAttr2PointerHR: takes a memory address of attribute</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; and returns the memory address of the corresponding graphic area</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; to the first scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida:  HL -&gt; Memory address of the graphic area.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerAttr2PointerHR:</span></span>
<span class="line"><span style="color: #002339">ld   a, h    </span><span style="color: #357B42; font-style: italic">; Loads the upper part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the third</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$40</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Add the fixed part</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; A = 010TT000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = address of the first scanline</span></span></code></pre></div>



<h2 id="conclusion" class="wp-block-heading has-large-font-size">Conclusion</h2>



<p class="has-medium-font-size wp-block-paragraph">Everything I have put here, I have learned by following the <a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">assembler course</a> found at speccy.org and reading the book Código Máquina del ZX-Spectrum by Jesús Alonso Cano.</p>



<p class="has-medium-font-size wp-block-paragraph">The routines shown here are the exercises I have done to practice what I have learned.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-medium-font-size wp-block-paragraph">You can also visit the rest of tutorials:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Z80
<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/bcd-numbers/">BCD numbers</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/zx-spectrum-screen">ZX Spectrum screen</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-pong-0x0ff-introduction/">ZX-Pong</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-space-battle-0x00-introduction/">Space Battle</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/">Tic-Tac-Toe</a></li>
</ul>
</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">And remember, if you use it, don&#8217;t just copy it, try to understand it and adapt it to your needs.</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-screen/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5319</post-id>	</item>
		<item>
		<title>Z80 Assembly &#8211; BCD numbers</title>
		<link>https://espamatica.com/bcd-numbers/</link>
					<comments>https://espamatica.com/bcd-numbers/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Tue, 17 Oct 2023 09:28:33 +0000</pubDate>
				<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Amstrad CPC]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5017</guid>

					<description><![CDATA[In this chapter of Z80 Assembly, we will learn how to work with numbers larger than 16 bits. What are]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of Z80 Assembly, we will learn how to work with numbers larger than 16 bits.</p>



<p class="wp-block-paragraph" style="font-size:16px">Translated with <a href="https://www.deepl.com/es/translator/" target="_blank" rel="noreferrer noopener nofollow">DeepL</a>.</p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#what-are-bcd-numbers">What are BCD numbers?</a>
</li>
<li><a href="#how-to-use-them">How to use them?</a>
</li>
<li><a href="#how-do-you-paint-them">How do you paint them?</a>
</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul></div>


<h2 id="what-are-bcd-numbers" class="wp-block-heading has-large-font-size">What are BCD numbers?</h2>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://en.wikipedia.org/wiki/Binary-coded_decimal" target="_blank" rel="noreferrer noopener nofollow">BCD numbers</a> are represented in a natural way, as we would see them; the hexadecimal 99 represents the decimal 99.</p>



<p class="has-medium-font-size wp-block-paragraph">To achieve this, a byte can represent numbers from 0 to 99 by dividing the byte into two nibbles (4 bits). Each nibble can represent numbers from 0 to 9.</p>



<p class="has-medium-font-size wp-block-paragraph">In this way, the hexadecimal 10, which in decimal is 16, in BCD represents the decimal 10. In other words, we see hexadecimal numbers as if they were decimal numbers.</p>



<h2 id="how-to-use-them" class="wp-block-heading has-large-font-size">How to use them?</h2>



<p class="has-medium-font-size wp-block-paragraph">To represent a 10-digit number, 5 bytes are needed. An extra byte can be used to indicate the position of the decimal point, in order to work with decimals. BCD numbers are used to operate with numbers of more than 16 bits.</p>



<p class="has-medium-font-size wp-block-paragraph">When operating with BCD numbers, the DAA (Decimal Adjust Accumulator) instruction must be taken into account. DAA makes adjustments to the results of operations with BCD numbers, and works as follows:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Check bits 0, 1, 2 and 3. If they contain a non-BCD digit (greater than 9) or the H flag is set, add or subtract, depending on the operation, 06h (0000 0110b) to the byte.</li>



<li class="has-medium-font-size">Check bits 4, 5, 6 and 7. If they contain a non-BCD digit (greater than 9) or the C flag is set, add or subtract, depending on the operation, 60h (0110 0000b) to the byte.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">Thus, if we have $09 and add $01, the result is $0A. After executing DAA, the result is $10. After each arithmetic instruction, increment or decrement, DAA must be executed.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld  a, $09    ; A = $09
inc a         ; A = $0A
daa           ; A = $10

dec a         ; A = $0F
daa           ; A = $09

add a, $03    ; A = $0C
daa           ; A = $12

sub $03       ; A = $0F
daa           ; A = $09" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld  a, </span><span style="color: #174781">$09</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; A = $09</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> a         </span><span style="color: #357B42; font-style: italic">; A = $0A</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $10</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339"> a         </span><span style="color: #357B42; font-style: italic">; A = $0F</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $09</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$03</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; A = $0C</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $12</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; A = $0F</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $09</span></span></code></pre></div>



<h2 id="how-do-you-paint-them" class="wp-block-heading has-large-font-size">How do you paint them?</h2>



<p class="has-medium-font-size wp-block-paragraph">The way to obtain the ASCII code of each nibble (four bits) is relatively simple.</p>



<p class="has-medium-font-size wp-block-paragraph">The routine shown below receives a BCD number in register B and displays it on the screen.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -----------------------------------------------------
; Prints a BCD number on the screen.
; Input: B -&gt; BCD number to be printed.
; Alters the value of the AF register.
; ----------------------------------------------------- 
PrintBCD:
ld   a, b   ; Loads in A the number to print
and  $f0    ; Keeps the upper nibble
rrca
rrca
rrca
rrca        ; Rotates it four times to move it to the lower nibble
add  a, '0' ; Adds the ASCII of the 0 to get the ASCII of the number
rst  $10    ; Display the number on the screen (ZX Spectrum)
; call $bb5a  ; Display the number on the screen (Amstrad CPC)

ld   a, b   ; Reloads the number to be printed in A
and  $0f    ; Keeps the bottom nibble
add  a, '0' ; Adds the ASCII of the 0 to get the ASCII of the number
rst  $10    ; Display the number on the screen (ZX Spectrum)
; call $bb5a  ; Displays the number on the screen (Amstrad CPC)
 
ret         ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -----------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Prints a BCD number on the screen.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: B -&gt; BCD number to be printed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF register.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PrintBCD:</span></span>
<span class="line"><span style="color: #002339">ld   a, b   </span><span style="color: #357B42; font-style: italic">; Loads in A the number to print</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $f0    </span><span style="color: #357B42; font-style: italic">; Keeps the upper nibble</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca        </span><span style="color: #357B42; font-style: italic">; Rotates it four times to move it to the lower nibble</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39; </span><span style="color: #357B42; font-style: italic">; Adds the ASCII of the 0 to get the ASCII of the number</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; Display the number on the screen (ZX Spectrum)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; call $bb5a  ; Display the number on the screen (Amstrad CPC)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, b   </span><span style="color: #357B42; font-style: italic">; Reloads the number to be printed in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; Keeps the bottom nibble</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39; </span><span style="color: #357B42; font-style: italic">; Adds the ASCII of the 0 to get the ASCII of the number</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; Display the number on the screen (ZX Spectrum)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; call $bb5a  ; Displays the number on the screen (Amstrad CPC)</span></span>
<span class="line"><span style="color: #002339"> </span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">         </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">RST $10, in ZX Spectrum, prints on screen the character corresponding to the Ascii code loaded in register A. CALL $BB5A does the same in Amstrad CPC.</p>



<h2 id="conclusion" class="wp-block-heading has-large-font-size">Conclusion</h2>



<p class="has-medium-font-size wp-block-paragraph">Working with this type of encoding is relatively simple and allows us to work with large numbers. In exchange, we need more bytes and more processing time.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-medium-font-size wp-block-paragraph">You can also visit the rest of tutorials:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Z80
<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/bcd-numbers/">BCD numbers</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/zx-spectrum-screen">ZX Spectrum screen</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-pong-0x0ff-introduction/">ZX-Pong</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-space-battle-0x00-introduction/">Space Battle</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/">Tic-Tac-Toe</a></li>
</ul>
</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">And remember, if you use it, don&#8217;t just copy it, try to understand it and adapt it to your needs.</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/bcd-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5017</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x06 Final adjustment</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Sat, 07 Oct 2023 06:40:45 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac-Toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4579</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly, we will make the final adjustments. We have reached the last chapter. We are]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>, we will make the final adjustments.</p>



<p class="has-medium-font-size wp-block-paragraph">We have reached the last chapter. We are going to make a couple of tweaks that, while not entirely necessary, I think might be of interest.</p>



<p class="has-medium-font-size wp-block-paragraph">We will add an option in the menu to allow players to select the maximum number of tables they can have.</p>



<p class="has-medium-font-size wp-block-paragraph">As for the tables, currently it is necessary to make all possible moves (occupy all the cells) to complete the point, although sometimes we already know that it is not possible to win and it is a table. We are going to implement the detection of tables to detect when it is no longer possible to win and end the point.</p>



<p class="has-medium-font-size wp-block-paragraph">We will propose some modifications that can save some bytes and clock cycles.</p>



<p class="has-medium-font-size wp-block-paragraph">We will modify the Spectrum&#8217;s moves to make it harder to beat.</p>



<p class="has-medium-font-size wp-block-paragraph">Finally, we&#8217;ll add the loading screen.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#tables-menu">Tables menu</a>
</li>
<li><a href="#table-detection">Table detection</a>
</li>
<li><a href="#we-save-bytes-and-clock-cycles">We save bytes and clock cycles</a>

<ul>
<li><a href="#same-tile-for-both-players">Same tile for both players</a>
</li>
</ul>
</li>
<li><a href="#spectrum-movement">Spectrum movement</a>
</li>
<li><a href="#difficulty">Difficulty</a>
</li>
<li><a href="#loading-screen">Loading screen</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul></div>


<h2 id="tables-menu" class="wp-block-heading has-large-font-size"><a></a><a></a>Tables menu</h2>



<p class="has-medium-font-size wp-block-paragraph">Currently, the maximum number of tables is declared in the MAXTIES constant in var.asm; we delete it.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate MaxPoints and add the new variable for the tables below it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="MaxTies:     db $05        ; Maximum tables" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">MaxTies:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Maximum tables</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate TitleEspamatica and add the Tables menu option just above it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="TitleOptionTies:   db $10, INK7, $16, $10, $08, &quot;4. &quot;, $10, INK6
                   defm &quot;Tables&quot;" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">TitleOptionTies:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">4.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Tables&quot;</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To make it more symmetrical, we locate Title3InStripe and replace $16, $02, $09 at the end of the line with $16, $04, $09.</p>



<p class="has-medium-font-size wp-block-paragraph">In Main.asm, let&#8217;s add the handling of the new menu item. Locate menu_Time and replace JR NZ, menu_op two lines down:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   nz, menu_Ties         ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   nz, menu_Ties         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate menu_TimeDo and three lines down, after JR menu_op, add the handling of the new menu item:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="menu_Ties:  
cp   KEY4                  ; Pushed 4?
jr   nz, menu_op           ; No, loop
ld   a, (MaxTies)          ; A = tables
add  a, $02                ; A += 2
cp   $0a                   ; A < 10?
jr   c, menu_TiesDo        ; Yes, skip
ld   a, $03                ; A = 3
menu_TiesDo:
ld   (MaxTies), a          ; Update tables
jr   menu_op               ; Loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">menu_Ties:</span><span style="color: #002339">  </span></span>
<span class="line"><span style="color: #002339">cp   KEY4                  </span><span style="color: #357B42; font-style: italic">; Pushed 4?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_op           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxTies)          </span><span style="color: #357B42; font-style: italic">; A = tables</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A += 2</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$0a</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A &lt; 10?</span></span>
<span class="line"><span style="color: #002339">jr   c, menu_TiesDo        </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$03</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 3</span></span>
<span class="line"><span style="color: #7EB233">menu_TiesDo:</span></span>
<span class="line"><span style="color: #002339">ld   (MaxTies), a          </span><span style="color: #357B42; font-style: italic">; Update tables</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the four key has been pressed, the maximum number of tables is increased by two, if it is greater than nine, it is reduced to three and loaded into memory.</p>



<p class="has-medium-font-size wp-block-paragraph">Both this range of values and the difference between one value and the other can be modified as required. You can also change it in points and time.</p>



<p class="has-medium-font-size wp-block-paragraph">Once we have the maximum number of tables in memory, we need to use them. Find the loop_reset tag, the LD B, MAXTIES line and replace it with the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (MaxTies)          ; A = maximum tables
ld   b, a                  ; B = A" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (MaxTies)          </span><span style="color: #357B42; font-style: italic">; A = maximum tables</span></span>
<span class="line"><span style="color: #002339">ld   b, a                  </span><span style="color: #357B42; font-style: italic">; B = A</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">All that remains is to print the selected value. In screen.asm, locate PrintOptions and add the following lines just above JP PrintBCD:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintBCD              ; Paints it
ld   b, INI_TOP-$10        ; B = coord Y
call AT                    ; Position cursor
ld   hl, MaxTies           ; HL = time value" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$10</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxTies           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load the emulator and check that we can now define the number of tables and that the game ends when the number of tables is reached.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa270548&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa270548" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" decoding="async" width="703" height="601" data-attachment-id="4581" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?fit=703%2C601&amp;ssl=1" data-orig-size="703,601" data-comments-opened="1" data-image-title="TicTacToe-S06-01" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?fit=703%2C601&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?resize=703%2C601&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4581" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?w=703&amp;ssl=1 703w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?resize=300%2C256&amp;ssl=1 300w" sizes="(max-width: 703px) 100vw, 703px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="table-detection" class="wp-block-heading has-large-font-size">Table detection</h2>



<p class="has-medium-font-size wp-block-paragraph">Currently, in order for boards to be detected, all cells must be occupied and there must be no tic-tac-toe. In reality, it is possible to know if the point will end in a board before the board is full.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to game.asm and implement table detection.</p>



<p class="has-medium-font-size wp-block-paragraph">To know if there is a possibility of a move, we just need to know if there is any combination left in which the cells are occupied by only one player or by no player at all.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Check for tables.
; In order to check if there are tables.
;
; Output: Z  -&gt; No tables.
;         NZ -&gt; There are tables.
;
; Alters the value of the AF, BC, DE, HL and IX registers.
; -------------------------------------------------------------------
CheckTies:
ld   b, $f0                ; B = single-player cell mask
ld   c, $0f                ; B = cell mask another player" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Check for tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; In order to check if there are tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: Z  -&gt; No tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;         NZ -&gt; There are tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC, DE, HL and IX registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">CheckTies:</span></span>
<span class="line"><span style="color: #002339">ld   b, $f0                </span><span style="color: #357B42; font-style: italic">; B = single-player cell mask</span></span>
<span class="line"><span style="color: #002339">ld   c, </span><span style="color: #174781">$0f</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = cell mask another player</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We load in B the mask to keep the cells occupied by one player, and in C the mask for the other player.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check123:
ld   hl, Grid              ; HL = grid address
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 2
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check123:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = grid address</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We place HL on the grid, add the value of the three cells in A and increment HL to move from one cell to another. We check if there are any cells occupied by a player, and if not, we leave. If so, we check if there are any cells occupied by the other player, and if not, we leave.</p>



<p class="has-medium-font-size wp-block-paragraph">The next two checks have the same structure.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check456:
inc  hl                    ; A = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check789:
inc  hl                    ; A = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check456:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; A = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check789:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; A = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Look closely at how we go through the cells with HL, this helps us to save a few bytes in an implementation we did earlier.</p>



<p class="has-medium-font-size wp-block-paragraph">The following checks cannot be done by changing cells with HL, as they are not numerically contiguous; we use IX.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check147:
ld   ix, Grid-$01          ; IX = address Grid - 1
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$04)           ; A+= cell value 4
add  a, (ix+$07)           ; A+= cell value 7
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check147:</span></span>
<span class="line"><span style="color: #002339">ld   ix, Grid-</span><span style="color: #174781">$01</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; IX = address Grid - 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The rest of the checks follow the same structure.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check258:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$08)           ; A+= cell value 8
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check369:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$06)           ; A+= cell value 6
add  a, (ix+$09)           ; A+= cell value 9
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check159:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$09)           ; A+= cell value 9
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check357:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$07)           ; A+= cell value 7
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret                        ; Exits with Z in correct state" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check258:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check369:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check159:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check357:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits with Z in correct state</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">With this we now have the prediction of whether there will be tables, all that remains is to go to main.asm and use it.</p>



<p class="has-medium-font-size wp-block-paragraph">Find loop_tie and three lines down substitute:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $09                ; A = 9
cp   (hl)                  ; Counter = 9?
jr   nz, loop_cont         ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$09</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 9</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter = 9?</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_cont         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call CheckTies             ; Any possible movement?
jr   z, loop_cont          ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CheckTies             </span><span style="color: #357B42; font-style: italic">; Any possible movement?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_cont          </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We no longer have to wait until the board is full to know if there are checkers or not. We call CheckTies and if there are no boards we continue with the point.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa27156b&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa27156b" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="702" height="603" data-attachment-id="4583" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-02/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?fit=702%2C603&amp;ssl=1" data-orig-size="702,603" data-comments-opened="1" data-image-title="TicTacToe-S06-02" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?fit=702%2C603&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?resize=702%2C603&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4583" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?w=702&amp;ssl=1 702w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 702px) 100vw, 702px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">You may see some behaviour that makes you think something is wrong. You see rows one, two, three free, but you know that the Spectrum will occupy one cell, then you another, and so it should mark tables.</p>



<p class="has-medium-font-size wp-block-paragraph">The system is predicting, not guessing. Imagine that there are two free cells in a row and it is the turn of the player whose cell is not occupied to move. Well, he will occupy one of the cells, so we already know it is a draw, but imagine that the player rests on his laurels and loses his turn, the other player occupies another cell and there is only one free cell, and it is still not possible to predict draws, if the other player falls asleep again, he loses his turn again and the first player gets three in a row.</p>



<h2 id="we-save-bytes-and-clock-cycles" class="wp-block-heading has-large-font-size"><a></a><a></a>We save bytes and clock cycles</h2>



<p class="has-medium-font-size wp-block-paragraph">We will implement several modifications to save bytes and clock cycles.</p>



<p class="has-medium-font-size wp-block-paragraph">The first of these will be in game.asm, on the set of CheckWinner and ZxMove routines, applying the way we have implemented CheckTies to the horizontal lines, the numerically contiguous ones.</p>



<p class="has-medium-font-size wp-block-paragraph">Go to the CheckWinner_check tag and find the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+1)             ; A = cell 1
add  a, (ix+2)             ; A+= cell 2
add  a, (ix+3)             ; A+= cell 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">1</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">2</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">3</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we replace them with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid              ; HL = cell address 1
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = cell address 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We locate the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+4)             ; A = cell 4
add  a, (ix+5)             ; A+= cell 5
add  a, (ix+6)             ; A+= cell 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">4</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">5</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">6</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we replace them with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="inc  hl                    ; HL = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We locate the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+7)             ; A = cell 7
add  a, (ix+8)             ; A+= cell 8
add  a, (ix+9)             ; A+= cell 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">7</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">8</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">9</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we replace them with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="inc  hl                    ; HL = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">This part is ready, add the comments of the routine to HL as the affected register.</p>



<p class="has-medium-font-size wp-block-paragraph">This is the comparison between the two versions:</p>



<figure class="wp-block-table"><table><tbody><tr><td>Version</td><td>Cycles</td><td>Bytes</td></tr><tr><td>1</td><td>688/641</td><td>118</td></tr><tr><td>2</td><td>638/591</td><td>111</td></tr></tbody></table><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">As you can see, we save fifty clock cycles and seven bytes. We compile, load into the emulator and see that it still works.</p>



<p class="has-medium-font-size wp-block-paragraph">We continue to modify ZxMove, specifically two parts of this routine set. We locate zxMoveToWin_123 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid              ; HL = cell address 1
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = cell address 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveToWin_456 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$03          ; HL = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$03</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveToWin_789 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$06          ; HL = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$06</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have now completed the first part. The main difference with CheckTies and CheckWinner is that although they are numerically contiguous cells, the move from cell three to cell four and from cell six to cell seven is not with INC HL, as ToMove changes the value of HL.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveAttack_123 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid              ; HL = cell address 1
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = cell address 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveAttack_456 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$03          ; HL = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$03</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveAttack_789 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$06          ; HL = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$06</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We make another modification in zxMoveDefence_cornerBlock34 and zxMoveDefence_cornerBlock67 that saves clock cycles but no bytes.</p>



<p class="has-medium-font-size wp-block-paragraph">Replacement in zxMoveDefence_cornerBlock34:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$04)           ; A+= cell value 4" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$02          ; A = cell address 3
ld   a, (hl)               ; A = cell value 3
inc  hl                    ; HL = cell address 4
add  a, (hl)               ; A+= cell value 4" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$02</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; A = cell address 3</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In zxMoveDefence_cornerBlock67 we replace:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$06)           ; A = cell value 6
add  a, (ix+$07)           ; A+= cell value 7" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$05          ; A = cell address 6
ld   a, (hl)               ; A = cell value 6
inc  hl                    ; HL = cell address 7
add  a, (hl)               ; A+= cell value 7" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$05</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; A = cell address 6</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The changes in game.asm are done. As I said, add HL as affected registry in the comments of the routine, we forgot it before because the HL registry was already affected by the zxMoveGeneric routine.</p>



<p class="has-medium-font-size wp-block-paragraph">This is the comparison between the two versions of ZxMove:</p>



<figure class="wp-block-table"><table><tbody><tr><td>Version</td><td>Cycles</td><td>Bytes</td></tr><tr><td>1</td><td>4632/4079</td><td>808</td></tr><tr><td>2</td><td>4532/3979</td><td>802</td></tr></tbody></table><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Version two occupies six bytes less and takes a hundred cycles less than version one, which seems small when we compare it with the bytes saved by CheckWinner with fewer modifications. Remember that there the transition from cell three to cell four and from cell six to cell seven is done with INC HL, which we cannot do here.</p>



<p class="has-medium-font-size wp-block-paragraph">The difference between INC HL and LD HL, Grid is four clock cycles and two bytes, and that limits our savings.</p>



<h3 id="same-tile-for-both-players" class="wp-block-heading has-large-font-size">Same tile for both players</h3>



<p class="has-medium-font-size wp-block-paragraph">If we have capacity problems, which we don&#8217;t, we can save another handful of bytes by painting the same tile in a different colour for both players.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to sprite.asm, comment out the Sprite_P2 definition and put the Sprite_CROSS, Sprite_SLASH and Sprite_MINUS definitions at the top of the file, like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; File: sprite.asm
;
; Definition of graphs.
; -------------------------------------------------------------------
; Crosshead Sprite
Sprite_CROSS:
db $18, $18, $18, $ff, $ff, $18, $18, $18 ; $90

; Sprite of the vertical line
Sprite_SLASH:
db $18, $18, $18, $18, $18, $18, $18, $18 ; $91

; Sprite of the horizontal line
Sprite_MINUS:
db $00, $00, $00, $ff, $ff, $00, $00, $00 ; $92

; Player 1 Sprite
Sprite_P1:
db $c0, $e0, $70, $38, $1c, $0e, $07, $03 ; $93
db $03, $07, $0e, $1c, $38, $70, $e0, $c0 ; $94

; ; Player Sprite 2
; Sprite_P2:
; db $03, $0f, $1c, $30, $60, $60, $c0, $c0 ; $95 Up/Left
; db $c0, $f0, $38, $0c, $06, $06, $03, $03 ; $96 Top/Right
; db $c0, $c0, $60, $60, $30, $1c, $0f, $03 ; $97 Down/Left
; db $03, $03, $06, $06, $0c, $38, $f0, $c0 ; $98 Down/Right" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; File: sprite.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Definition of graphs.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Crosshead Sprite</span></span>
<span class="line"><span style="color: #7EB233">Sprite_CROSS:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, $ff, $ff, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $90</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite of the vertical line</span></span>
<span class="line"><span style="color: #7EB233">Sprite_SLASH:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $91</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite of the horizontal line</span></span>
<span class="line"><span style="color: #7EB233">Sprite_MINUS:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, $ff, $ff, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $92</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Player 1 Sprite</span></span>
<span class="line"><span style="color: #7EB233">Sprite_P1:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> $c0, $e0, </span><span style="color: #174781">$70</span><span style="color: #002339">, </span><span style="color: #174781">$38</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $93</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$38</span><span style="color: #002339">, </span><span style="color: #174781">$70</span><span style="color: #002339">, $e0, $c0 </span><span style="color: #357B42; font-style: italic">; $94</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ; Player Sprite 2</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite_P2:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $03, $0f, $1c, $30, $60, $60, $c0, $c0 ; $95 Up/Left</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $c0, $f0, $38, $0c, $06, $06, $03, $03 ; $96 Top/Right</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $c0, $c0, $60, $60, $30, $1c, $0f, $03 ; $97 Down/Left</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $03, $03, $06, $06, $0c, $38, $f0, $c0 ; $98 Down/Right</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Commenting on player two&#8217;s sprite not only forces us to change the routine that paints the tiles, it also forces us to change the routine that paints the board as the UDGs change. We have uploaded the board sprites above so that we can only change the board painting routine once, and so that if we decide to repaint the two tiles later, it won&#8217;t be affected.</p>



<p class="has-medium-font-size wp-block-paragraph">By commenting out Sprite_P2, we save thirty-two bytes. It doesn&#8217;t sound like much, but in certain situations it can save us.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to var.asm and change the definition of Board_1 and _2 and leave them as they are:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; Vertical lines of the board.
Board_1:
db $12, $00, $13, $00        
db $20, $20, $20, $20, $91, $20, $20, $20, $20, $91, $20, $20, $20
db $20, $ff

; Horizontal lines of the board.
Board_2:        
db $92, $92, $92, $92, $90, $92, $92, $92, $92, $90, $92, $92, $92
db $92, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; Vertical lines of the board.</span></span>
<span class="line"><span style="color: #7EB233">Board_1:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">        </span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">, $ff</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Horizontal lines of the board.</span></span>
<span class="line"><span style="color: #7EB233">Board_2:</span><span style="color: #002339">        </span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$90</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$90</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$92</span><span style="color: #002339">, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the result, which is the mess we made.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa27386a&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa27386a" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="701" height="602" data-attachment-id="4588" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-03/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?fit=701%2C602&amp;ssl=1" data-orig-size="701,602" data-comments-opened="1" data-image-title="TicTacToe-S06-03" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?fit=701%2C602&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?resize=701%2C602&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4588" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?w=701&amp;ssl=1 701w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 701px) 100vw, 701px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">That player two&#8217;s tile is painted wrong is to be expected, but what about the board? We have changed the position of the sprite definition, but UDG is still pointing to Sprite_P1.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to main.asm and under the Main tag we replace:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Sprite_P1         ; HL = address Sprite_P1" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Sprite_P1         </span><span style="color: #357B42; font-style: italic">; HL = address Sprite_P1</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Sprite_CROSS      ; HL = address Sprite_CROSS" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Sprite_CROSS      </span><span style="color: #357B42; font-style: italic">; HL = address Sprite_CROSS</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Also change the comment in the line below.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load it into the emulator and see that the board looks good, but the parts don&#8217;t. Don&#8217;t worry, this is to be expected.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa274302&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa274302" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="704" height="608" data-attachment-id="4589" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-04/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?fit=704%2C608&amp;ssl=1" data-orig-size="704,608" data-comments-opened="1" data-image-title="TicTacToe-S06-04" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?fit=704%2C608&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?resize=704%2C608&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4589" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?w=704&amp;ssl=1 704w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 704px) 100vw, 704px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">We are going to modify the routine that paints the tiles so that it does it well, or not, you can leave it as it is; three in a bloody line.</p>



<p class="has-medium-font-size wp-block-paragraph">We locate printOXO_X in screen.asm and the lines where the UDG is loaded in A. We add three to the value it loads:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>ld a, $90 becomes ld a, $93<br>ld a, $91 becomes ld a, $94</strong></p>



<p class="has-medium-font-size wp-block-paragraph">We locate printOXO_Y and the lines on which the UDG is loaded in A. We add three to the value it loads:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>ld a, $92 becomes ld a, $95<br>ld a, $93 becomes ld a, $96<br>ld a, $94 becomes ld a, $97<br>ld a, $95 becomes ld a, $98</strong></p>



<p class="has-medium-font-size wp-block-paragraph">So if we decided to paint two different tiles, the circle would be painted.</p>



<p class="has-medium-font-size wp-block-paragraph">As we are now in the situation of painting a single tile, we comment on the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $95                ; A = 1st sprite
ld   a, $98                ; A = 4th sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$95</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$98</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 4th sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And below that we add the line:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $93                ; A = 1st sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We comment on the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $96                ; A = 2nd sprite
ld   a, $97                ; A = 3rd sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$96</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$97</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 3rd sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; And below that we add the line:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $94                ; A = 2nd sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">A bit of a mess, isn&#8217;t it? Here&#8217;s what it looks like:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="printOXO_X:
ld   a, INKPLAYER1         ; A = ink player 1
call INK                   ; Change ink
ld   a, $93                ; A = 1st sprite
rst  $10                   ; Paints it
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
dec  b                     ; B = bottom line
call AT                    ; Position cursor
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
ld   a, $93                ; A = 2nd sprite
rst  $10                   ; Paints it
ret                        ; Exits

printOXO_Y:
ld   a, INKPLAYER2         ; A = ink player 2
call INK                   ; Change ink
; ld   a, $95                ; A = 1st sprite
ld   a, $93                ; A = 1st sprite
rst  $10                   ; Paints it
; ld   a, $96                ; A = 2nd sprite
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
dec  b                     ; B = bottom line
call AT                    ; Position cursor
; ld   a, $97                ; A = 3rd sprite
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
; ld   a, $98                ; A = 4th sprite
ld   a, $93                ; A = 1st sprite
rst  $10                   ; Paints it
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">printOXO_X:</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER1         </span><span style="color: #357B42; font-style: italic">; A = ink player 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; B = bottom line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printOXO_Y:</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER2         </span><span style="color: #357B42; font-style: italic">; A = ink player 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $95                ; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $96                ; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; B = bottom line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $97                ; A = 3rd sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $98                ; A = 4th sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">So, if we want to redraw the two pieces, in sprite.asm we uncomment Sprite_P2, and in printOXO_Y we alternate the comments on the LD A, &#8230; lines.</p>



<p class="has-medium-font-size wp-block-paragraph">This will paint the pieces on the board, but not in the information part of the game.</p>



<p class="has-medium-font-size wp-block-paragraph">We go back to var.asm and modify player1_figure to look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player1_figure:   db $16, $04, $00, $90, $91, $0d, $91, $90" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player1_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$90</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$90</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we leave it at that:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player1_figure:   db $16, $04, $00, $93, $94, $0d, $94, $93" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player1_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$93</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We modify player2_figure so that it now looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player2_figure:   db $16, $04, $1b, $92, $93, $16, $05, $1b
                  db $94, $95, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player2_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span></span>
<span class="line"><span style="color: #002339">                  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$95</span><span style="color: #002339">, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we leave it at that:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player2_figure:   db $16, $04, $1b, $93, $94, $16, $05, $1b
                  db $94, $93, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player2_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span></span>
<span class="line"><span style="color: #002339">                  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Finally, we add the annotated definition for painting different tiles.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; player2_figure:   db $16, $04, $1b, $95, $96, $16, $05, $1b
;                   db $97, $98, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; player2_figure:   db $16, $04, $1b, $95, $96, $16, $05, $1b</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                   db $97, $98, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load in the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa27566b&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa27566b" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="700" height="606" data-attachment-id="4590" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-05/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?fit=700%2C606&amp;ssl=1" data-orig-size="700,606" data-comments-opened="1" data-image-title="TicTacToe-S06-05" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?fit=700%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?resize=700%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4590" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?w=700&amp;ssl=1 700w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 700px) 100vw, 700px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">You can paint the same tile in a different colour for both players, or you can paint a different colour for each player. If we were using a black and white TV, there would be no doubt.</p>



<p class="has-medium-font-size wp-block-paragraph">If you keep all the changes, we have saved forty-five bytes and one hundred and fifty clock cycles. If you decide to paint different tiles for each player, the byte saving is thirteen bytes.</p>



<h2 id="spectrum-movement" class="wp-block-heading has-large-font-size"><a></a><a></a>Spectrum movement</h2>



<p class="has-medium-font-size wp-block-paragraph">If you have played several games against the Spectrum, you will have worked out how to beat it every time you start the game, because there is at least one move that the Spectrum does not know how to defend, we have not programmed it.</p>



<p class="has-medium-font-size wp-block-paragraph">The specific move is to occupy two corner cells: two and six or six and eight. If we occupy cell two, the Spectrum occupies cell five, we move to cell six and the Spectrum moves to cell seven, we move to cell three and we have two tic-tac-toe moves: cells one, two and three and cells three, six and nine.</p>



<p class="has-medium-font-size wp-block-paragraph">In game.asm we locate zxMoveAttack_123 and above it we implement the lines that make it impossible to beat the Spectrum with this move.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Defensive corner movement.
; -------------------------------------------------------------------
zxMoveDefence_corner24:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$04)           ; A+= cell value 4
cp   b                     ; A = B?
jr   nz, zxMoveDefence_corner26 ; No, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Defensive corner movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_corner24:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner26 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We check if player one has occupied squares two and four, in which case we move to square one if possible.</p>



<p class="has-medium-font-size wp-block-paragraph">The rest of the checks follow the same structure.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveDefence_corner26:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveDefence_corner84 ; No, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
zxMoveDefence_corner84:
ld   a, (ix+$08)           ; A = cell value 8
add  a, (ix+$04)           ; A+= cell value 4
cp   b                     ; A = B?
jr   nz, zxMoveDefence_corner86 ; No, skip
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
zxMoveDefence_corner86:
ld   a, (ix+$08)           ; A = cell value 8
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveAttack_123  ; No, skip
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell nine
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveDefence_corner26:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner84 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_corner84:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner86 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_corner86:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_123  </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell nine</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Although the play is in two corners, we cover all four.</p>



<p class="has-medium-font-size wp-block-paragraph">Just above zxMoveDefence_cornerBlock2938Cont is the JR NZ line, zxMoveAttack_123, we modify it and leave it as it is:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   nz, zxMoveDefence_corner24 ; No, no mov cross, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner24 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If you compile and play some games against Spectrum, you will see that it is no longer possible to beat him with this move, but it is still possible to beat him, there are still moves that he does not know how to defend.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa276c3d&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa276c3d" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="701" height="604" data-attachment-id="4591" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-06/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?fit=701%2C604&amp;ssl=1" data-orig-size="701,604" data-comments-opened="1" data-image-title="TicTacToe-S06-06" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?fit=701%2C604&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?resize=701%2C604&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4591" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?w=701&amp;ssl=1 701w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 701px) 100vw, 701px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="difficulty" class="wp-block-heading has-large-font-size">Difficulty</h2>



<p class="has-medium-font-size wp-block-paragraph">Now it is more difficult to beat the Spectrum, we have to wait for him to start the game and depending on the move he makes, we can beat him.</p>



<p class="has-medium-font-size wp-block-paragraph">We will add another option to the menu to be able to choose the level of difficulty: one to not cover the play in the corner, two to cover the play in the corner.</p>



<p class="has-medium-font-size wp-block-paragraph">In var.asm, after MaxTies, we add the variable for the level of difficulty:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Level:       db $02        ; Difficulty level" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Level:</span><span style="color: #002339">       </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$02</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Difficulty level</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">When a new menu item is added, it is appended to the Spamatica line. We move all the menu items up one line, like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="TitleOptionStart:  db $10, INK1, $13, $01, $16, $07, $08, &quot;0. &quot;
                   db $10, INK5
                   defm &quot;Start&quot;
TitleOptionPlayer: db $10, INK7, $13, $01, $16, $09, $08, &quot;1. &quot;
                   db $10, INK6
                   defm &quot;Players&quot;
TitleOptionPoint:  db $10, INK7, $16, $0b, $08, &quot;2. &quot;, $10, INK6
                   defm &quot;Points&quot;
TitleOptionTime:   db $10, INK7, $16, $0d, $08, &quot;3. &quot;, $10, INK6
                   defm &quot;Time&quot;
TitleOptionTies:   db $10, INK7, $16, $0f, $08, &quot;4. &quot;, $10, INK6
                   defm &quot;Tables&quot;" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">TitleOptionStart:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK1, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">0.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Start&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPlayer:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$09</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">1.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Players&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPoint:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">2.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Points&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionTime:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">3.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Time&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionTies:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0f</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">4.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Tables&quot;</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">After the definition of TitleOptionTies, we add the definition of Difficulty:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="TitleOptionLevel:  db $10, INK7, $16, $11, $08, &quot;5. &quot;, $10, INK6
                   defm &quot;Difficulty&quot;" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">TitleOptionLevel:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$11</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">5.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Difficulty&quot;</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In screen.asm we find PrintOptions and subtract one from all Y coordinate assignments:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>LD B, INI_TOP-$0A go to LD B,INI_TOP-$09<br>LD B, INI_TOP-$0C go to LD B,INI_TOP-$0B<br>LD B, INI_TOP-$0E go to LD B,INI_TOP-$0D<br>LD B, INI_TOP-$10 go to LD B,INI_TOP-$0F</strong> </p>



<p class="has-medium-font-size wp-block-paragraph">Above JP PrintBCD we add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintBCD              ; Paints it
ld   b, INI_TOP-$11        ; B = coord Y
call AT                    ; Position cursor
ld   hl, Level             ; HL = difficulty value" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$11</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Level             </span><span style="color: #357B42; font-style: italic">; HL = difficulty value</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The final aspect of the routine is as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the values of the options.
;
; Alters the value of the AF, BC and HL registers.
; -------------------------------------------------------------------
PrintOptions:
ld   a, INK4               ; A = green ink
call INK                   ; Change ink
ld   b, INI_TOP-$09        ; B = coord Y
ld   c, INI_LEFT-$15       ; C = coord X
call AT                    ; Position cursor
ld   hl, MaxPlayers        ; HL = value players
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0b        ; B = coord Y
call AT                    ; Position cursor
ld   hl, MaxPoints         ; HL = points value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0d        ; B = coord Y
call AT                    ; Position cursor
ld   hl, seconds           ; HL = time value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0f        ; B = coord Y
call AT                    ; Position cursor
ld   hl, MaxTies           ; HL = time value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$11        ; B = coord Y
call AT                    ; Position cursor
ld   hl, Level             ; HL = difficulty value
jp   PrintBCD              ; Paints it in and exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the values of the options.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintOptions:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4               </span><span style="color: #357B42; font-style: italic">; A = green ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$09</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$15</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPlayers        </span><span style="color: #357B42; font-style: italic">; HL = value players</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0b</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPoints         </span><span style="color: #357B42; font-style: italic">; HL = points value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0d</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, seconds           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0f</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxTies           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$11</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Level             </span><span style="color: #357B42; font-style: italic">; HL = difficulty value</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it in and exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In main.asm find menu_Ties and then the line CP KEY4. After this line, replace JR NZ, menu_op with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   nz, menu_Level        ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   nz, menu_Level        </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">After menu_TiesDo, after the JR line menu_op, we add the processing lines of the new menu item:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="menu_Level:
cp   KEY5                  ; Pushed 5?
jr   nz, menu_op           ; No, loop
ld   a, (Level)            ; A = difficulty
xor  $03                   ; Alternates between 1 and 2
ld   (Level), a            ; Refresh in memory
jr   menu_op               ; Loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">menu_Level:</span></span>
<span class="line"><span style="color: #002339">cp   KEY5                  </span><span style="color: #357B42; font-style: italic">; Pushed 5?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_op           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #002339">ld   a, (Level)            </span><span style="color: #357B42; font-style: italic">; A = difficulty</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Alternates between 1 and 2</span></span>
<span class="line"><span style="color: #002339">ld   (Level), a            </span><span style="color: #357B42; font-style: italic">; Refresh in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Finally, in game.asm we locate zxMoveDefence_corner24, and just below it we add:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (Level)            ; A = difficulty
cp   $01                   ; Difficulty = 1?
jr   z, zxMoveAttack_123   ; Yes, jumps" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (Level)            </span><span style="color: #357B42; font-style: italic">; A = difficulty</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Difficulty = 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_123   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">These lines do not check for corner play when the difficulty level is one. Compile and test.</p>



<h2 id="loading-screen" class="wp-block-heading has-large-font-size">Loading screen</h2>



<p class="has-medium-font-size wp-block-paragraph">Finally, we&#8217;re going to add the loading screen, but what&#8217;s the difference when we&#8217;ve already seen how it&#8217;s done in ZX-Pong and Space Battle? Well, this time we&#8217;re going to do it differently, because we&#8217;re not going to load the screen into VideoRAM, we&#8217;re going to load it into a different memory address and then make it appear all at once.</p>



<p class="has-medium-font-size wp-block-paragraph">This is the loading screen you can download from <a href="https://tinyurl.com/2qe2txpz" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa277de7&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa277de7" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="511" height="382" data-attachment-id="4509" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/tictactoe-s06-07/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?fit=511%2C382&amp;ssl=1" data-orig-size="511,382" data-comments-opened="1" data-image-title="TicTacToe-S06-07" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?fit=511%2C382&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?resize=511%2C382&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4509" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?w=511&amp;ssl=1 511w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?resize=300%2C224&amp;ssl=1 300w" sizes="auto, (max-width: 511px) 100vw, 511px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Please be kind, art has never been my thing. I will be happy if you design your own loading screen, which I am sure will be better than this one; it shouldn&#8217;t be difficult.</p>



<p class="has-medium-font-size wp-block-paragraph">In order to load the loading screen into a memory area and then dump it all at once into VideoRAM, we need to implement the routine to do this in a separate file and compile it separately.</p>



<p class="has-medium-font-size wp-block-paragraph">The loading process will do the following:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Load the loader.</li>



<li class="has-medium-font-size">Load the routine that dumps the loading screen into memory address 24200.</li>



<li class="has-medium-font-size">Load the loading screen at address 24250.</li>



<li class="has-medium-font-size">Runs the routine that dumps the loading screen into VideoRAM.</li>



<li class="has-medium-font-size">Load the tic-tac-toe routine at address 24200.</li>



<li class="has-medium-font-size">Load the interrupt routine at address 32348.</li>



<li class="has-medium-font-size">Run the tic-tac-toe program.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">The first thing we are going to look at is the loader we developed in Basic to do all of the above.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">VB</span><span role="button" tabindex="0" data-code=" 10 CLEAR 24200
 20 INK 0: PAPER 4: BORDER 4: CLS
 30 POKE 23610,255: POKE 23739,111
 40 LOAD &quot;&quot;CODE 24200
 50 LOAD &quot;&quot;CODE 24250
 60 RANDOMIZE USR 24200
 70 LOAD &quot;&quot;CODE 24200
 80 LOAD &quot;&quot;CODE 32348
 90 CLS
100 RANDOMIZE USR 24200" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339"> </span><span style="color: #174781">10</span><span style="color: #002339"> </span><span style="color: #1AB394">CLEAR</span><span style="color: #002339"> </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">20</span><span style="color: #002339"> INK </span><span style="color: #174781">0</span><span style="color: #002339">: PAPER </span><span style="color: #174781">4</span><span style="color: #002339">: BORDER </span><span style="color: #174781">4</span><span style="color: #002339">: CLS</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">30</span><span style="color: #002339"> POKE </span><span style="color: #174781">23610</span><span style="color: #002339">,</span><span style="color: #174781">255</span><span style="color: #002339">: POKE </span><span style="color: #174781">23739</span><span style="color: #002339">,</span><span style="color: #174781">111</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">40</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">50</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24250</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">60</span><span style="color: #0991B6"> RANDOMIZE </span><span style="color: #002339">USR </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">70</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">80</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">32348</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">90</span><span style="color: #002339"> CLS</span></span>
<span class="line"><span style="color: #174781">100</span><span style="color: #0991B6"> RANDOMIZE </span><span style="color: #002339">USR </span><span style="color: #174781">24200</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Don&#8217;t forget to save it with SAVE «OXO» LINE 10.</p>



<p class="has-medium-font-size wp-block-paragraph">The next step is to implement the routine that dumps the loading screen into the VideoRAM. We create the file loadScr.asm and add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; LoadScr.asm
;
; The loading screen will be loaded in $5eba, and this routine will 
; be passed by the VideoRAM to appear all at once and will then 
; clear the area.
; Where it was initially loaded.
; The cleaning of this memory area is not necessary, but we do it.
; in case we need to debug, not to find code that actually are 
; remnants of the loading screen.
; -------------------------------------------------------------------
org  $5e88                 ; Loading address

ld   hl, $5eba             ; HL = address where the screen is located
ld   de, $4000             ; DE = VideoRAM address
ld   bc, $1b00             ; Screen length
ldir                       ; Flip the screen

ld   hl, $5eba             ; HL = address where the screen is located
ld   de, $5ebb             ; Next address
ld   bc, $1aff             ; Screen length - 1
ld   (hl), $00             ; Clear the first position
ldir                       ; Clean up the rest

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; LoadScr.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; The loading screen will be loaded in $5eba, and this routine will </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; be passed by the VideoRAM to appear all at once and will then </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; clear the area.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Where it was initially loaded.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; The cleaning of this memory area is not necessary, but we do it.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; in case we need to debug, not to find code that actually are </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; remnants of the loading screen.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$5e88</span><span style="color: #002339">                 </span><span style="color: #357B42; font-style: italic">; Loading address</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, </span><span style="color: #174781">$5eba</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; HL = address where the screen is located</span></span>
<span class="line"><span style="color: #002339">ld   de, </span><span style="color: #174781">$4000</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; DE = VideoRAM address</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$1b00</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Screen length</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Flip the screen</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, </span><span style="color: #174781">$5eba</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; HL = address where the screen is located</span></span>
<span class="line"><span style="color: #002339">ld   de, </span><span style="color: #174781">$5ebb</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Next address</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$1aff</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Screen length - 1</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$00</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Clear the first position</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Clean up the rest</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The loading screen is loaded at $5eba. This routine copies $1B00 positions (6912 bytes), the entire area of pixels and attributes, from this address into VideoRAM.</p>



<p class="has-medium-font-size wp-block-paragraph">Once copied, we clean up the memory area where the screen was loaded; it is not necessary, but if we have to debug, we will avoid residual code that could confuse us.</p>



<p class="has-medium-font-size wp-block-paragraph">Now we just need to modify the script we need to compile and generate the final .tap. The Windows version looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="echo off
cls
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Compiling loadscr
pasmo --name LoadScr --tap loadScr.asm loadscr.tap loadscr.log
echo Generating Tic-Tac-Toe
copy /b /y loader.tap+loadscr.tap+TresEnRayaScr.tap+oxo.tap+int.tap TicTacToe.tap" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">off</span></span>
<span class="line"><span style="color: #7EB233">cls</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">LoadScr</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadScr.asm</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-Tac-Toe</span></span>
<span class="line"><span style="color: #7EB233">copy</span><span style="color: #002339"> </span><span style="color: #A44185">/b</span><span style="color: #002339"> </span><span style="color: #A44185">/y</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap+loadscr.tap+TresEnRayaScr.tap+oxo.tap+int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The Linux version would look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="clear
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Compiling loadscr
pasmo --name LoadScr --tap loadScr.asm loadscr.tap loadscr.log
echo Generating Tic-Tac-Toe
cat loader.tap loadscr.tap TresEnRayaScr.Tap oxo.tap int.tap &gt; TicTacToe.tap" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">clear</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">LoadScr</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadScr.asm</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-Tac-Toe</span></span>
<span class="line"><span style="color: #7EB233">cat</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.tap</span><span style="color: #002339"> </span><span style="color: #A44185">TresEnRayaScr.Tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #7B30D0">&gt;</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And that is it.</p>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">Thank you very much for joining me on this journey, I hope this is just the beginning, that it has helped you to learn and become passionate about assembly programming for the ZX Spectrum. I will continue to study and learn, and if I can gather more material in the future, I may write another tutorial.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/2bwwwsjw" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4579</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x05 Me versus the Spectrum</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x05-me-versus-zx/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x05-me-versus-zx/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Fri, 06 Oct 2023 05:05:34 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac-Toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4570</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we will implement the possibility to play against the Spectrum. Me versus the Spectrum Once]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we will implement the possibility to play against the Spectrum.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#me-versus-the-spectrum">Me versus the Spectrum</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul></div>


<h2 id="me-versus-the-spectrum" class="wp-block-heading has-large-font-size">Me versus the Spectrum</h2>



<p class="has-medium-font-size wp-block-paragraph">Once we can play against friends and family, the possibility remains that we may not have anyone to play with, hence the need to be able to play against the Spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">We wrote the bulk of this implementation in game.asm, starting with a routine that generates semi-random numbers between one and nine, so that when the Spectrum starts the game, it does so in a different way.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Gets a semi-random number between 1 and 9.
;
; Return: A -&gt; Number obtained.
; -------------------------------------------------------------------
GetRandomN:
ld   a, r                  ; A = R
and  $0f                   ; Leave bits 0 to 3
inc  a                     ; A+=1
cp   $0a                   ; A &gt; 9?
ret  c                     ; No, exit
rra                        ; A/=2, because Carry = 0, otherwise SRL A
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Gets a semi-random number between 1 and 9.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Return: A -&gt; Number obtained.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">GetRandomN:</span></span>
<span class="line"><span style="color: #002339">ld   a, r                  </span><span style="color: #357B42; font-style: italic">; A = R</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Leave bits 0 to 3</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A+=1</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$0a</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A &gt; 9?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; No, exit</span></span>
<span class="line"><span style="color: #002339">rra                        </span><span style="color: #357B42; font-style: italic">; A/=2, because Carry = 0, otherwise SRL A</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">GetRandomN gets a semi-random number between one and nine, using register R. In the RRA line, as seen in the comments, we divide A by two because the carry is zero and because we only do it once, otherwise we would have to do it with SRL A. RRA is one byte and takes four clock cycles, SRL A is only twice as long. It is only divided by two if the number obtained is greater than 9.</p>



<p class="has-medium-font-size wp-block-paragraph">The part that carries out the movements of the spectrum is very long, as there are several combinations that are evaluated, so we will see it in blocks. However, as there are many similarities between the different blocks, we will only explain the most important ones.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Moves ZX Spectrum.
;
; Alters the value of the AF, BC and IX registers.
; -------------------------------------------------------------------
ZxMove:
ld   a, (MoveCounter)      ; A = moves
or   a                     ; Movements = 0?
jr   nz, zxMove_center     ; No, skip
call GetRandomN            ; A = number between 1 and 9
add  a, '0'                ; A = ascii code
ld   c, a                  ; C = A
call ToMove                ; Move to cell
ret                        ; Exits
zxMove_center:
cp   $01                   ; Moves &gt; 1?
jr   nz, zxMove_cont       ; Yes, skip
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Moves ZX Spectrum.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and IX registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">ZxMove:</span></span>
<span class="line"><span style="color: #002339">ld   a, (MoveCounter)      </span><span style="color: #357B42; font-style: italic">; A = moves</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Movements = 0?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMove_center     </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> GetRandomN            </span><span style="color: #357B42; font-style: italic">; A = number between 1 and 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; A = ascii code</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; C = A</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"><span style="color: #7EB233">zxMove_center:</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Moves &gt; 1?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMove_cont       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In this first part we decide if it is the first move of the game, and if so we take a random number between one and nine and move to that cell. If it is the second move, we try to move to cell five (middle). If these conditions are not met, we continue with the checks to see if the spectrum can win or lose.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMove_cont:
ld   ix, Grid-$01          ; IX = dir Grid-1
ld   b, $20                ; B = Spectrum value can earn
call zxMoveToWin_123       ; Move to gain Spectrum
ret  z                     ; If valid, exits
ld   b, $02                ; B = value player 1 can win
call zxMoveToWin_123       ; Move to avoid it
ret  z                     ; If valid, exits
jp   zxMoveDefence_diagonally ; Defensive movements" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMove_cont:</span></span>
<span class="line"><span style="color: #002339">ld   ix, Grid-</span><span style="color: #174781">$01</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; IX = dir Grid-1</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$20</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = Spectrum value can earn</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> zxMoveToWin_123       </span><span style="color: #357B42; font-style: italic">; Move to gain Spectrum</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If valid, exits</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = value player 1 can win</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> zxMoveToWin_123       </span><span style="color: #357B42; font-style: italic">; Move to avoid it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If valid, exits</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   zxMoveDefence_diagonally </span><span style="color: #357B42; font-style: italic">; Defensive movements</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Remember that the moves of player two, in this case the Spectrum, are signalled in bit four of the cells, so we load $20 into B, which would be the value if there were two cells occupied by the Spectrum in a row.</p>



<p class="has-medium-font-size wp-block-paragraph">We call zxMoveToWin_123 so that if Spectrum wins, it makes the move.</p>



<p class="has-medium-font-size wp-block-paragraph">If the Spectrum has not won, we load two into B to check if player one has the move to win. We call zxMoveToWin_123 a second time to prevent it from winning.</p>



<p class="has-medium-font-size wp-block-paragraph">If none of the above has happened, we go on the defensive.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Evaluates whether the Spectrum has the movement to win.
; -------------------------------------------------------------------
zxMoveToWin_123:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3
cp   b                     ; A = B?
jp   nz, zxMoveToWin_456   ; No, skip
; Spectrum can win
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
inc  c                     ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
inc  c                     ; C = key 3
call ToMove                ; Move to cell 3
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Evaluates whether the Spectrum has the movement to win.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_123:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   nz, zxMoveToWin_456   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum can win</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the Spectrum occupies two squares, we try to move to square one, if not, to square two and if not, to square three. To go from one square to the next, we increment C, numerically they are contiguous.</p>



<p class="has-medium-font-size wp-block-paragraph">The checks for the combinations four, five, six and seven, eight, nine are the same as above.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveToWin_456:
ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveToWin_789   ; No, skip
; Spectrum can win
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
inc  c                     ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
inc  c                     ; C = key 6
call ToMove                ; Move to cell 6
ret                        ; Sale

zxMoveToWin_789:
ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   nz, zxMoveToWin_147   ; No, skip
; Spectrum can win
Ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits
inc  c                     ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits
inc  c                     ; C = key 9
call ToMove                ; Move to cell 9
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveToWin_456:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_789   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum can win</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Sale</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_789:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_147   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum can win</span></span>
<span class="line"><span style="color: #002339">Ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The rest of the tests change slightly.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveToWin_147:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$04)           ; A+= cell value 4
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A=B?
jr   nz, zxMoveToWin_258   ; No, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveToWin_147:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_258   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">As the squares are not numerically contiguous, at the end we load each key instead of incrementing C. From here on, all the checks to see if the Spectrum can win or lose are the same.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveToWin_258:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$08)           ; A+= cell value 8
cp   b                     ; A=B?
jr   nz, zxMoveToWin_369   ; No, skip
ld   c, KEY2               ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY8               ; C = key 8
call ToMove                ; Move to cell 8
ret                        ; Exits
    
zxMoveToWin_369:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$06)           ; A+= cell value 6
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A=B?
jr   nz, zxMoveToWin_159   ; No, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret                        ; Exits
    
zxMoveToWin_159:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A=B?
jr   nz, zxMoveToWin_357   ; No, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret                        ; Exits

zxMoveToWin_357:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A = B?
ret  nz
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveToWin_258:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_369   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY2               </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY8               </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"><span style="color: #002339">    </span></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_369:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_159   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"><span style="color: #002339">    </span></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_159:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_357   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_357:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nz</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The set of routines zxMoveToWin is used both to know if Spectrum can win and make the move to do so, and to know if Spectrum can lose and make the move to avoid it.</p>



<p class="has-medium-font-size wp-block-paragraph">If both player one and Spectrum have no chance of winning, we try to anticipate player one&#8217;s move. First we check if the player makes a diagonal move that occupies both vertices, and if so we make a cross block.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Diagonal defensive movement.
; -------------------------------------------------------------------
zxMoveDefence_diagonally:
; Checks if player 1 has diagonal checkers
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   z, zxMoveDefence_crossBlock ; Yes, mov diagonal, skip
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A = B?
jr   nz, zxMoveDefence_crossBlock1 ; No, skip

zxMoveDefence_crossBlock:
; Cross locking
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits
ld   c, KEY2               ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
ld   c, KEY8               ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Diagonal defensive movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_diagonally:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Checks if player 1 has diagonal checkers</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock </span><span style="color: #357B42; font-style: italic">; Yes, mov diagonal, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_crossBlock1 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Cross locking</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY2               </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY8               </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Then we check if he has tried a diagonal move with the centre occupied, and if so, depending on which vertex is occupied, we block the vertical.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Defensive diagonal movement with the centre occupied.
; -------------------------------------------------------------------
zxMoveDefence_crossBlock1:
ld   a, (ix+$05)           ; A = cell value 5
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_cornerBlock16 ; Z = not occupied, skip
ld   a, (ix+$01)           ; A = cell value 1
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_crossBlock3 ; Z = not occupied, skip
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits

zxMoveDefence_crossBlock3:
ld   a, (ix+$03)           ; A = cell value 3
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_crossBlock7 ; Z = not occupied, skip
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveDefence_crossBlock7:
ld   a, (ix+$07)           ; A = cell value 7
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_crossBlock9 ; Z = not occupied, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits

zxMoveDefence_crossBlock9:
ld   a, (ix+$09)           ; A = cell value 9
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_cornerBlock16 ; Z = not occupied, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Defensive diagonal movement with the centre occupied.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock1:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock16 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock3 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock3:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock7 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock7:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock9 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock9:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 9</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock16 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If player one has not attempted a diagonal move, we will see if he has attempted a cross move and try to block it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Defensive cross movement.
; -------------------------------------------------------------------
zxMoveDefence_cornerBlock16:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock34 ; No, no mov cross, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock34:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$04)           ; A+= cell value 4
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock67 ; No, no mov cross, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock67:
ld   a, (ix+$06)           ; A = cell value 6
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock49 ; No, no mov cross, skip
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock49:
ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock1827 ; No, no mov cross, skip
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock1827:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$08)           ; A+= cell value 8
cp   b                     ; A = B?
jr   z, zxMoveDefence_cornerBlock1827Cont ; Yes, mov cross, block
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$07)           ; A+= Value cell 7
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock2938 ; No, no mov cross, skip

zxMoveDefence_cornerBlock1827Cont:
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock2938:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   z, zxMoveDefence_cornerBlock2938Cont ; Yes, mov cross, block
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$08)           ; A+= cell value 8
cp   b                     ; A = B?
jr   nz, zxMoveAttack_123  ; No, no mov cross, skip

zxMoveDefence_cornerBlock2938Cont:
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Defensive cross movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock16:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock34 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock34:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock67 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock67:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock49 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock49:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock1827 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock1827:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock1827Cont </span><span style="color: #357B42; font-style: italic">; Yes, mov cross, block</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= Value cell 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock2938 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock1827Cont:</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock2938:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock2938Cont </span><span style="color: #357B42; font-style: italic">; Yes, mov cross, block</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_123  </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock2938Cont:</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If we&#8217;ve got this far, we&#8217;ve established that Spectrum has no move to win, that player one has no move to win, that player one is in no danger from player one&#8217;s diagonal or cross moves; it&#8217;s time for Spectrum to go on the attack.</p>



<p class="has-medium-font-size wp-block-paragraph">This last part will only be done in the first few moves, as the board fills up we will be more concerned with defending than attacking.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Horizontal, vertical and diagonal offensive movement.
; -------------------------------------------------------------------
zxMoveAttack_123:
ld   b, $20                ; B = Spectrum value with two boxes
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_456  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
Jr   z, zxMoveAttack_456   ; Yes, jumps
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
inc  c                     ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
inc  c                     ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Horizontal, vertical and diagonal offensive movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_123:</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$20</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = Spectrum value with two boxes</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_456  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">Jr   z, zxMoveAttack_456   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">For the attack, we look for combinations where player one has no cells occupied and the Spectrum has only one.</p>



<p class="has-medium-font-size wp-block-paragraph">The rest of the checks are very similar to the previous one.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(3 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveAttack_456:
ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_789  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_789   ; Yes, jumps
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
inc  c                     ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
inc  c                     ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits

zxMoveAttack_789:
ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_147  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_147   ; Yes, jumps
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits
inc  c                     ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits
inc  c                     ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveAttack_147:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$04)           ; A+= cell value 4
add  a, (ix+$07)           ; A+= cell value 7
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 boxes
jr   nz, zxMoveAttack_258  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_258   ; Yes, jumps
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret   z                    ; If correct, exits

zxMoveAttack_258:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$08)           ; A+= cell value 8
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_369  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_369   ; Yes, jumps
ld   c, KEY2               ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY8               ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits

zxMoveAttack_369:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$06)           ; A+= cell value 6
add  a, (ix+$09)           ; A+= cell value 9
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_159  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
Jr   z, zxMoveAttack_159   ; Yes, jumps
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveAttack_159:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$09)           ; A+= cell value 9
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_357  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
Jr   z, zxMoveAttack_357   ; Yes, jumps
Ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveAttack_357:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$07)           ; A+= cell value 7
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveGeneric     ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveGeneric      ; Yes, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7	
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveAttack_456:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_789  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_789   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_789:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_147  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_147   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_147:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 boxes</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_258  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_258   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">   z                    </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_258:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_369  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_369   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY2               </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY8               </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_369:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_159  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">Jr   z, zxMoveAttack_159   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_159:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_357  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">Jr   z, zxMoveAttack_357   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">Ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_357:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveGeneric     </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveGeneric      </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7	</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">When we arrive here, the Spectrum has not found a place to go and will move to the first free cell.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Generic movement. 
; If with all of the above, you have not made any movement
; moves to the first free cell.
; -------------------------------------------------------------------
zxMoveGeneric:
ld   c, '1'                ; C = ascii 1 (cell 1)
ld   b, $09                ; B = total cells
ld   a, $00                ; A = empty cell
ld   hl, Grid              ; HL = address 1st cell

zxMoveGeneric_loop:
cp   (hl)                  ; Free cell?
jr   z, zxMoveGeneric_end  ; Yes, skip
inc  c                     ; C = ascii next cell
inc  hl                    ; HL = next cell
djnz zxMoveGeneric_loop    ; Loop until B = 0

zxMoveGeneric_end:
call ToMove                ; Move to free cell
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Generic movement. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; If with all of the above, you have not made any movement</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; moves to the first free cell.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveGeneric:</span></span>
<span class="line"><span style="color: #002339">ld   c, &#39;</span><span style="color: #174781">1</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; C = ascii 1 (cell 1)</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$09</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = total cells</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = empty cell</span></span>
<span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = address 1st cell</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveGeneric_loop:</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Free cell?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveGeneric_end  </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = ascii next cell</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = next cell</span></span>
<span class="line"><span style="color: #002339">djnz zxMoveGeneric_loop    </span><span style="color: #357B42; font-style: italic">; Loop until B = 0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveGeneric_end:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to free cell</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have already implemented the way the Spectrum behaves in single player games. It&#8217;s not perfect, it has some loopholes and sometimes the Spectrum makes some erratic moves. That&#8217;s fine, if we made it perfect we could never beat the Spectrum and nobody likes that.</p>



<p class="has-medium-font-size wp-block-paragraph">Now we need to integrate what we have implemented inside the main loop into main.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">Find the loop_lostMov tag, and five lines down replace JR Z, loop_keyCont with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   z, loop_players       ; Not active, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   z, loop_players       </span><span style="color: #357B42; font-style: italic">; Not active, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_keyCont tag and replace the line above it, JR loop_cont, with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jp   loop_cont             ; Jump" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   loop_cont             </span><span style="color: #357B42; font-style: italic">; Jump</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Between this line and loop_keyCont we add the Spectrum movement for single player games, this causes JR to give an out of range error, so we have replaced it with JP.</p>



<p class="has-medium-font-size wp-block-paragraph">Finally, between this line and loop_keyCont, we add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="loop_players:
ld   a, (PlayerMoves)      ; A = player who moves
or   a                     ; Player 1?
jr   z, loop_keyCont       ; Yes, skip
ld   a, (MaxPlayers)       ; A = players
cp   $02                   ; 2 players?
jr   z, loop_keyCont       ; Yes, skip
call ZxMove                ; Move Spectrum
jr   nz, loop_players      ; NZ = incorrect, loop
push bc                    ; Preserve BC
ld   bc, SoundSpectrum     ; BC = sound address
call PlayMusic             ; Outputs sound
pop  bc                    ; Retrieves BC
jr   loop_print            ; Paints movement" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">loop_players:</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player who moves</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_keyCont       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)       </span><span style="color: #357B42; font-style: italic">; A = players</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; 2 players?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_keyCont       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ZxMove                </span><span style="color: #357B42; font-style: italic">; Move Spectrum</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_players      </span><span style="color: #357B42; font-style: italic">; NZ = incorrect, loop</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserve BC</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundSpectrum     </span><span style="color: #357B42; font-style: italic">; BC = sound address</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Outputs sound</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Retrieves BC</span></span>
<span class="line"><span style="color: #002339">jr   loop_print            </span><span style="color: #357B42; font-style: italic">; Paints movement</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We get which player is moving, if it is two we get how many players there are and if it is one it moves the spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile, load into the emulator and see the result. If all went well, we can now play against the Spectrum. If you think it is too easy, you can tweak the Spectrum&#8217;s movements, which we will do in the next chapter.</p>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">In the next chapter of ZX Spectrum Assembly, we will make the final adjustments.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/227mah88" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x05-me-versus-zx/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4570</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x04 Options and end of game</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Thu, 05 Oct 2023 05:17:28 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac-Toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4554</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we will implement the different options of the game and the end of the game;]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we will implement the different options of the game and the end of the game; as we left off in the previous chapter, the game continues indefinitely. The options will be selected from a start menu.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#colours">Colours</a>
</li>
<li><a href="#menu">Menu</a>
</li>
<li><a href="#start-of-game">Start of game</a>
</li>
<li><a href="#shift-time">Shift time</a>
</li>
<li><a href="#end-of-game">End of game</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul></div>


<h2 id="colours" class="wp-block-heading has-large-font-size">Colours</h2>



<p class="has-medium-font-size wp-block-paragraph">In var.asm we will add the following constants for the colours to avoid having to use numbers and to make it easier to change the colours during the game.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="INK0: equ $00
INK1: equ $01
INK2: equ $02
INK3: equ $03
INK4: equ $04
INK5: equ $05
INK6: equ $06
INK7: equ $07
INK0: equ $00
INK1: equ $01
INK2: equ $02
INK3: equ $03
INK4: equ $04
INK5: equ $05
INK6: equ $06
INK7: equ $07" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">INK0:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">INK1:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7EB233">INK2:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$02</span></span>
<span class="line"><span style="color: #7EB233">INK3:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$03</span></span>
<span class="line"><span style="color: #7EB233">INK4:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$04</span></span>
<span class="line"><span style="color: #7EB233">INK5:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #7EB233">INK6:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$06</span></span>
<span class="line"><span style="color: #7EB233">INK7:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$07</span></span>
<span class="line"><span style="color: #7EB233">INK0:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">INK1:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7EB233">INK2:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$02</span></span>
<span class="line"><span style="color: #7EB233">INK3:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$03</span></span>
<span class="line"><span style="color: #7EB233">INK4:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$04</span></span>
<span class="line"><span style="color: #7EB233">INK5:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #7EB233">INK6:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$06</span></span>
<span class="line"><span style="color: #7EB233">INK7:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$07</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">As an exercise, go through the title and text declarations and where you see $10, change the number that follows it to the corresponding INK tag.</p>



<p class="has-medium-font-size wp-block-paragraph">In the same way, look for INK calls in the rest of the files, and instead of loading the ink number in A, load the corresponding INK tag.</p>



<h2 id="menu" class="wp-block-heading has-large-font-size">Menu</h2>



<p class="has-medium-font-size wp-block-paragraph">Some of the start menu texts are already defined in ar.asm: the TitleEspamatica tag and from TitleOptionStart to TitleOptionTime.</p>



<p class="has-medium-font-size wp-block-paragraph">We will delete all these lines as we are going to reimplement it, adding colours and positions, and it will look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Title3InStripe:    db $10, INK2, $13, $01, $16, $02, $09
                   defm &quot;Three in a row&quot;
TitleOptionStart:  db $10, INK1, $13, $01, $16, $08, $08, &quot;0. &quot;
                   db $10, INK5
                   defm &quot;Start&quot;
TitleOptionPlayer: db $10, INK7, $13, $01, $16, $0a, $08, &quot;1. &quot;
                   db $10, INK6
                   defm &quot;Players&quot;
TitleOptionPoint:  db $10, INK7, $16, $0c, $08, &quot;2. &quot;, $10, INK6
                   defm &quot;Points&quot;
TitleOptionTime:   db $10, INK7, $16, $0e, $08, &quot;3. &quot;, $10, INK6
                   defm &quot;Time&quot;
TitleEspamatica:   db $16, $14, $08
                   db $10, INK2, &quot;E&quot;, $10, INK6, &quot;s&quot;
                   db $10, INK4, &quot;p&quot;, $10, INK5, &quot;a&quot; 
                   db $10, INK2, &quot;m&quot;, $10, INK6, &quot;a&quot;
                   db $10, INK4, &quot;t&quot;, $10, INK5, &quot;i&quot;
                   db $10, INK2, &quot;c&quot;, $10, INK6, &quot;a&quot;
                   db $10, INK7, &quot; 2019&quot;, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Title3InStripe:</span><span style="color: #002339">    </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$09</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Three </span><span style="color: #7B30D0">in</span><span style="color: #002339"> a row&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionStart:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK1, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">0.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Start&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPlayer:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">1.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Players&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPoint:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0c</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">2.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Points&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionTime:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">3.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Time&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleEspamatica:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$14</span><span style="color: #002339">, </span><span style="color: #174781">$08</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, &quot;E&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6, &quot;s&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK4, &quot;p&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5, &quot;a&quot; </span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, &quot;m&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6, &quot;a&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK4, &quot;t&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5, &quot;i&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, &quot;c&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6, &quot;a&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, &quot; </span><span style="color: #174781">2019</span><span style="color: #002339">&quot;, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Now we have the definition of the start menu almost ready to paint, we just need a routine to paint the values of the options. We implement this in screen.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the values of the options.
;
; Alters the value of the AF, BC and HL registers.
; -------------------------------------------------------------------
PrintOptions:
ld   a, INK4               ; A = green ink
call INK                   ; Change ink
ld   b, INI_TOP-$0a        ; B = coord Y
ld   c, INI_LEFT-$15       ; C = coord X
call AT                    ; Positions cursor
ld   hl, MaxPlayers        ; HL = value players
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0c        ; B = coord Y
call AT                    ; Positions cursor
ld   hl, MaxPoints         ; HL = points value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0e        ; B = coord Y
call AT                    ; Positions cursor
ld   hl, MaxSeconds        ; HL = time value
jp   PrintBCD              ; Paints it and exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the values of the options.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintOptions:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4               </span><span style="color: #357B42; font-style: italic">; A = green ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0a</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$15</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Positions cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPlayers        </span><span style="color: #357B42; font-style: italic">; HL = value players</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0c</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Positions cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPoints         </span><span style="color: #357B42; font-style: italic">; HL = points value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0e</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Positions cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxSeconds        </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it and exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintOptions paints the values of the options in green. It positions the cursor and paints each of the values.</p>



<p class="has-medium-font-size wp-block-paragraph">Let&#8217;s see what the start menu looks like. We locate Main in main.asm, and under CALL OPENCHAN we add these lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Menu:
di                         ; Deactivate interruptions
im   $01                   ; Interruptions = mode 1
ei                         ; Activates interruptions" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Menu:</span></span>
<span class="line"><span style="color: #174781">di</span><span style="color: #002339">                         </span><span style="color: #357B42; font-style: italic">; Deactivate interruptions</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Interruptions = mode 1</span></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have added a label for the menu part, disabled the interrupts to switch to mode one and enabled them again. We will then use the interrupts in mode two, so these lines are needed here.</p>



<p class="has-medium-font-size wp-block-paragraph">At the bottom we find CALL CLS and then add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Title3InStripe    ; HL = address three-inline	
call PrintString           ; Paints the menu
menu_op:    
call PrintOptions          ; Paint the options
jr   menu_op               ; Menu loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Title3InStripe    </span><span style="color: #357B42; font-style: italic">; HL = address three-inline	</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString           </span><span style="color: #357B42; font-style: italic">; Paints the menu</span></span>
<span class="line"><span style="color: #7EB233">menu_op:</span><span style="color: #002339">    </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintOptions          </span><span style="color: #357B42; font-style: italic">; Paint the options</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Menu loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We draw the start screen and the values of the options. We remain in an infinite loop as this is where we will implement the menu logic.</p>



<p class="has-medium-font-size wp-block-paragraph">With the menu definition and a few lines we have painted it, but we still need to make it work.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile it, load it into the emulator and see how it looks.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa2cf1f4&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa2cf1f4" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="698" height="605" data-attachment-id="4556" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?fit=698%2C605&amp;ssl=1" data-orig-size="698,605" data-comments-opened="1" data-image-title="TicTacToe-S04-01" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?fit=698%2C605&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?resize=698%2C605&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4556" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?w=698&amp;ssl=1 698w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 698px) 100vw, 698px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Before implementing the menu logic, we will implement a routine in game.asm that uses the ROM to read the keyboard and return the ASCII code of the last key pressed. This routine will be used for the menu options and for the player name query.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Wait for a key to be pressed and return its Ascii code.
;
; Output: A -&gt; Ascii code of the key.
;
; Alters the value of the AF and HL registers.
; -------------------------------------------------------------------
WaitKeyAlpha:
ld   hl, FLAGS_KEY         ; HL = address flag keyboard
set  $03, (hl)             ; Input mode L

; Loop until a key is obtained.
WaitKeyAlpha_loop:
bit  $05, (hl)             ; Key pressed?
jr   z, WaitKeyAlpha_loop  ; Not pressed, loop
res  $05, (hl)             ; Bit set to 0 for future inspections

; Gets the Ascii of the key pressed.
; Valid Ascii 12 ($0C), 13 ($0D) and from 32 ($20) to 127 ($7F)
; If the key pressed is Space, load ' ' in A
WaitKeyAlpha_loadKey:
ld   hl, LAST_KEY          ; HL = last key pressed address
ld   a, (hl)               ; A = last key pressed
cp   $80                   ; Ascii &gt; 127?
jr   nc, WaitKeyAlpha      ; Yes, invalid key, loop
cp   KEYDEL                ; Pushed Delete?
ret  z                     ; Yes, exit
cp   KEYENT                ; Press Enter?
ret  z                     ; Yes, exit
cp   KEYSPC                ; Powered Space?
jr   c, WaitKeyAlpha       ; Ascii < space, invalid, loop
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Wait for a key to be pressed and return its Ascii code.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: A -&gt; Ascii code of the key.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">WaitKeyAlpha:</span></span>
<span class="line"><span style="color: #002339">ld   hl, FLAGS_KEY         </span><span style="color: #357B42; font-style: italic">; HL = address flag keyboard</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$03</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Input mode L</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Loop until a key is obtained.</span></span>
<span class="line"><span style="color: #7EB233">WaitKeyAlpha_loop:</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$05</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Key pressed?</span></span>
<span class="line"><span style="color: #002339">jr   z, WaitKeyAlpha_loop  </span><span style="color: #357B42; font-style: italic">; Not pressed, loop</span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$05</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Bit set to 0 for future inspections</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Gets the Ascii of the key pressed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Valid Ascii 12 ($0C), 13 ($0D) and from 32 ($20) to 127 ($7F)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; If the key pressed is Space, load &#39; &#39; in A</span></span>
<span class="line"><span style="color: #7EB233">WaitKeyAlpha_loadKey:</span></span>
<span class="line"><span style="color: #002339">ld   hl, LAST_KEY          </span><span style="color: #357B42; font-style: italic">; HL = last key pressed address</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = last key pressed</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$80</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Ascii &gt; 127?</span></span>
<span class="line"><span style="color: #002339">jr   nc, WaitKeyAlpha      </span><span style="color: #357B42; font-style: italic">; Yes, invalid key, loop</span></span>
<span class="line"><span style="color: #002339">cp   KEYDEL                </span><span style="color: #357B42; font-style: italic">; Pushed Delete?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Yes, exit</span></span>
<span class="line"><span style="color: #002339">cp   KEYENT                </span><span style="color: #357B42; font-style: italic">; Press Enter?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Yes, exit</span></span>
<span class="line"><span style="color: #002339">cp   KEYSPC                </span><span style="color: #357B42; font-style: italic">; Powered Space?</span></span>
<span class="line"><span style="color: #002339">jr   c, WaitKeyAlpha       </span><span style="color: #357B42; font-style: italic">; Ascii &lt; space, invalid, loop</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">WaitKeyAlpha waits until a key is pressed that is valid for us, that is: delete, enter or an ASCII code between thirty-two and one hundred and twenty-seven. When a valid key is pressed, it returns the ASCII code for that key in A.</p>



<p class="has-medium-font-size wp-block-paragraph">We implement the menu routine in main.asm, just after CALL PrintOptions, and the final aspect is this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="menu_op:
call PrintOptions          ; Paint the options
call WaitKeyAlpha          ; Wait for keypress
cp   KEY0                  ; Pushed 0?
jr   z, Init               ; Yes, start game
menu_Players:
cp   KEY1                  ; Pushed 1?
jr   nz, menu_Points       ; No, skip
ld   a, (MaxPlayers)       ; A = players
xor  $03                   ; Alternates between 1 and 2
ld   (MaxPlayers), a       ; Update in memory
jr   menu_op               ; Loop
menu_Points:
cp   KEY2                  ; Pushed 2?
jr   nz, menu_Time         ; No, skip
ld   a, (MaxPoints)        ; A = points
inc  a                     ; A += 1
cp   $06                   ; A = 6?
jr   nz, menu_PointsDo     ; No, skip
ld   a, $01                ; A = 1
menu_PointsDo:  
ld   (MaxPoints), a        ; Update in memory
add  a, '0'                ; A = value ascii
ld   (info_points), a      ; Update in memory
cp   '1'                   ; Points 1?
jr   z, menu_Points1       ; Yes, skip
ld   a, 's'                ; Plural
jr   menu_PointsEnd        ; Jump
menu_Points1:
ld   a, ' '                ; Singular
menu_PointsEnd:
ld   (info_gt1), a         ; Update in memory
jr   menu_op               ; Loop
menu_Time:
cp   KEY3                  ; Pushed 3?
jr   nz, menu_op           ; No, loop
ld   a, (MaxSeconds)       ; A = seconds
add  a, $05                ; A += 5
daa                        ; Decimal setting
cp   $35                   ; A = 35 BCD?
jr   nz, menu_TimeDo       ; No, skip
ld   a, $05                ; A = 5
menu_TimeDo:
ld   (MaxSeconds), a       ; Refreshes in memory
jr   menu_op               ; Loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">menu_op:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintOptions          </span><span style="color: #357B42; font-style: italic">; Paint the options</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha          </span><span style="color: #357B42; font-style: italic">; Wait for keypress</span></span>
<span class="line"><span style="color: #002339">cp   KEY0                  </span><span style="color: #357B42; font-style: italic">; Pushed 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, Init               </span><span style="color: #357B42; font-style: italic">; Yes, start game</span></span>
<span class="line"><span style="color: #7EB233">menu_Players:</span></span>
<span class="line"><span style="color: #002339">cp   KEY1                  </span><span style="color: #357B42; font-style: italic">; Pushed 1?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_Points       </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)       </span><span style="color: #357B42; font-style: italic">; A = players</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Alternates between 1 and 2</span></span>
<span class="line"><span style="color: #002339">ld   (MaxPlayers), a       </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span>
<span class="line"><span style="color: #7EB233">menu_Points:</span></span>
<span class="line"><span style="color: #002339">cp   KEY2                  </span><span style="color: #357B42; font-style: italic">; Pushed 2?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_Time         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPoints)        </span><span style="color: #357B42; font-style: italic">; A = points</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A += 1</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$06</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = 6?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_PointsDo     </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1</span></span>
<span class="line"><span style="color: #7EB233">menu_PointsDo:</span><span style="color: #002339">  </span></span>
<span class="line"><span style="color: #002339">ld   (MaxPoints), a        </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; A = value ascii</span></span>
<span class="line"><span style="color: #002339">ld   (info_points), a      </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">cp   &#39;</span><span style="color: #174781">1</span><span style="color: #002339">&#39;                   </span><span style="color: #357B42; font-style: italic">; Points 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, menu_Points1       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, &#39;s&#39;                </span><span style="color: #357B42; font-style: italic">; Plural</span></span>
<span class="line"><span style="color: #002339">jr   menu_PointsEnd        </span><span style="color: #357B42; font-style: italic">; Jump</span></span>
<span class="line"><span style="color: #7EB233">menu_Points1:</span></span>
<span class="line"><span style="color: #002339">ld   a, &#39; &#39;                </span><span style="color: #357B42; font-style: italic">; Singular</span></span>
<span class="line"><span style="color: #7EB233">menu_PointsEnd:</span></span>
<span class="line"><span style="color: #002339">ld   (info_gt1), a         </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span>
<span class="line"><span style="color: #7EB233">menu_Time:</span></span>
<span class="line"><span style="color: #002339">cp   KEY3                  </span><span style="color: #357B42; font-style: italic">; Pushed 3?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_op           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxSeconds)       </span><span style="color: #357B42; font-style: italic">; A = seconds</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, </span><span style="color: #174781">$05</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A += 5</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Decimal setting</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$35</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = 35 BCD?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_TimeDo       </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$05</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 5</span></span>
<span class="line"><span style="color: #7EB233">menu_TimeDo:</span></span>
<span class="line"><span style="color: #002339">ld   (MaxSeconds), a       </span><span style="color: #357B42; font-style: italic">; Refreshes in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">After calling the routine that checks if a valid key has been pressed, it evaluates if it is any key between zero and three, and depending on which it is, it acts in one way or another. Note that it only does the decimal adjustment when incrementing seconds; it is the only option whose value is greater than nine.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load the emulator and see the results. Everything seems to go well until we press zero, start the game and the menu remains.</p>



<p class="has-medium-font-size wp-block-paragraph">To fix this, we add the following line below the Init tag:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call CLS                   ; Clear screen" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLS                   </span><span style="color: #357B42; font-style: italic">; Clear screen</span></span></code></pre></div>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa2cffc8&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa2cffc8" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="703" height="605" data-attachment-id="4557" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-02/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?fit=703%2C605&amp;ssl=1" data-orig-size="703,605" data-comments-opened="1" data-image-title="TicTacToe-S04-02" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?fit=703%2C605&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?resize=703%2C605&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4557" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?w=703&amp;ssl=1 703w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 703px) 100vw, 703px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="start-of-game" class="wp-block-heading has-large-font-size">Start of game</h2>



<p class="has-medium-font-size wp-block-paragraph">When the game starts, the first thing we will do is ask for the names of the players. First, we add another constant to rom.asm; this address contains the coordinates of the cursor.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; Cursor position on screen 2. 
; If loaded at BC -&gt; B = Y, C = X.
CURSOR: equ $5c88" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; Cursor position on screen 2. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; If loaded at BC -&gt; B = Y, C = X.</span></span>
<span class="line"><span style="color: #7EB233">CURSOR:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c88</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We implement the routine responsible for retrieving the names of the players in game.asm. Although we won&#8217;t go through it instruction by instruction, due to the size of the routine we will implement it in blocks.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="GetPlayersName:
ld   hl, Name_p1      
ld   de, Name_p1+$01  
ld   bc, LENNAME*2-$01
ld   (hl), &quot; &quot; 
ldir    " style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">GetPlayersName:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1      </span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p1+</span><span style="color: #174781">$01</span><span style="color: #002339">  </span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME*</span><span style="color: #174781">2</span><span style="color: #002339">-</span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot; &quot; </span></span>
<span class="line"><span style="color: #002339">ldir    </span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have cleared the names of the players.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   e, $01
getPlayersName_loop:
ld   a, INK4
call INK
ld   b, INI_TOP-$0f
ld   c, INI_LEFT-$01
call CLL
call AT

ld   hl, TitlePlayerNumber
ld   (hl), &quot;1&quot;
ld   a, $01
cp   e
jr   z, getPlayersName_cont
ld   (hl), &quot;2&quot;
getPlayersName_cont:
ld   hl, TitlePlayerName
call PrintString

ld   hl, Name_p1
ld   a, $01
cp   e
jr   z, getPlayersName_cont2
ld   hl, Name_p2" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   e, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_loop:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0f</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLL</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerNumber</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">1</span><span style="color: #002339">&quot;</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">cp   e</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">2</span><span style="color: #002339">&quot;</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_cont:</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerName</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">cp   e</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont2</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We make a loop of at most two iterations, one per player, change the colour, delete the line where the names are requested, position the cursor, prepare the title depending on the player, and point HL to the player&#8217;s name.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_cont2:
ld   d, $00
ld   a, INK3    
call INK   
call getPlayersName_getName 

ld   a, (MaxPlayers)           
cp   $02			 
jr   nz, getPlayersName_onlyOne
inc  e 
cp   e 
jr   z, getPlayersName_loop    
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_cont2:</span></span>
<span class="line"><span style="color: #002339">ld   d, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #002339">ld   a, INK3    </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK   </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> getPlayersName_getName </span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)           </span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">			 </span></span>
<span class="line"><span style="color: #002339">jr   nz, getPlayersName_onlyOne</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  e </span></span>
<span class="line"><span style="color: #002339">cp   e </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_loop    </span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We use D to control the length of the name, change the ink and ask for the player&#8217;s name. We get the players, see if there are two players, and if not, default the name to two. If there are two players, we check if the name entered is the name of player two, and if not, we loop to ask for it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_onlyOne:
ld   hl, Name_p2Default
ld   de, Name_p2       
ld   bc, LENNAME       
ldir    
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_onlyOne:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2Default</span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p2       </span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME       </span></span>
<span class="line"><span style="color: #002339">ldir    </span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If it is a single player, give the second player the default name.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_getName:
push hl  
call WaitKeyAlpha            
pop  hl  

cp   KEYDEL      
jr   z, getPlayersName_delete 
cp   KEYENT      
jr   z, getPlayersName_enter		
push de  
ld   e, a 
ld   a, LENNAME       
cp   d 
ld   a, e 
pop  de 
jr   z, getPlayersName_getName
                                 
ld   (hl), a 
inc  hl 
rst  $10
inc  d 
jr   getPlayersName_getName" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_getName:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl  </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha            </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl  </span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">cp   KEYDEL      </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_delete </span></span>
<span class="line"><span style="color: #002339">cp   KEYENT      </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_enter		</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de  </span></span>
<span class="line"><span style="color: #002339">ld   e, a </span></span>
<span class="line"><span style="color: #002339">ld   a, LENNAME       </span></span>
<span class="line"><span style="color: #002339">cp   d </span></span>
<span class="line"><span style="color: #002339">ld   a, e </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName</span></span>
<span class="line"><span style="color: #002339">                                 </span></span>
<span class="line"><span style="color: #002339">ld   (hl), a </span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl </span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  d </span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We wait for a valid key to be pressed, check if it is Delete and if so, we jump to its operation. We check if it is enter and if it is, we jump to its operation.</p>



<p class="has-medium-font-size wp-block-paragraph">If it is neither delete nor enter, we check if we have reached the maximum length and if not, we add the character to the name and draw it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_delete:
ld   a, $00   
cp   d 
jr   z, getPlayersName_getName

dec  d 
dec  hl  
ld   a, ' ' 
ld   (hl), a 
ld   bc, (CURSOR)        
inc  c 
call AT  
rst  $10   
call AT  
jr   getPlayersName_getName" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_delete:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$00</span><span style="color: #002339">   </span></span>
<span class="line"><span style="color: #002339">cp   d </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  d </span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  hl  </span></span>
<span class="line"><span style="color: #002339">ld   a, &#39; &#39; </span></span>
<span class="line"><span style="color: #002339">ld   (hl), a </span></span>
<span class="line"><span style="color: #002339">ld   bc, (CURSOR)        </span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT  </span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">   </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT  </span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the key pressed is delete and the length of the name is not zero, we delete the previous character of the name and the display.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_enter:
ld   a, 0 
cp   d 
jr   z, getPlayersName_getName
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_enter:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">0</span><span style="color: #002339"> </span></span>
<span class="line"><span style="color: #002339">cp   d </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the key pressed is enter and the length of the name is not zero, the name request is terminated.</p>



<p class="has-medium-font-size wp-block-paragraph">The final aspect of the routine is as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(3 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Ask for the names of the players.
;
; Alters the value of the AF, BC, DE and HL registers.
; -------------------------------------------------------------------
GetPlayersName:
ld   hl, Name_p1           ; HL = address player name 1
ld   de, Name_p1+$01       ; DE = HL+1
ld   bc, LENNAME*2-$01     ; BC = length names - 1
ld   (hl), &quot; &quot;             ; Clears first position
ldir                       ; Clean up the rest

ld   e, $01                ; E = 1
getPlayersName_loop:
ld   a, INK4               ; A = ink 4
call INK                   ; Change ink
ld   b, INI_TOP-$0f        ; B = coord Y
ld   c, INI_LEFT-$01       ; X = coord X
call CLL                   ; Delete the line
call AT                    ; Position cursor

ld   hl, TitlePlayerNumber ; HL = player number
ld   (hl), &quot;1&quot;             ; Player 1
ld   a, $01                ; A = 1
cp   e                     ; Player 1?
jr   z, getPlayersName_cont; Yes, skip
ld   (hl), &quot;2&quot;             ; Player 2 
getPlayersName_cont:
ld   hl, TitlePlayerName   ; HL = title player name
call PrintString           ; Paints it

ld   hl, Name_p1           ; HL = address player name 1
ld   a, $01                ; A = 1
cp   e                     ; Player 1?
jr   z, getPlayersName_cont2 ; Yes, skip
ld   hl, Name_p2           ; HL = address player name 2
getPlayersName_cont2:
ld   d, $00                ; D = counter length name
ld   a, INK3               ; A = ink 3
call INK                   ; Change ink
call getPlayersName_getName; Request player name

ld   a, (MaxPlayers)       ; A = players
cp   $02                   ; Two players?
jr   nz, getPlayersName_onlyOne ; One player, default name
inc  e                     ; E+=1
cp   e                     ; Compare with players
jr   z, getPlayersName_loop; Equals, jumps
ret

; Single player
; Copies the default name of player 2
getPlayersName_onlyOne:
ld   hl, Name_p2Default    ; HL = default player name 2
ld   de, Name_p2           ; DE = name player 2
ld   bc, LENNAME           ; Length name
ldir                       ; Copy default name
ret                        ; Sale

; Requests the player's name
getPlayersName_getName:
push hl                    ; Preserve HL
call WaitKeyAlpha          ; Wait for valid key
pop  hl                    ; Retrieve HL

cp   KEYDEL                ; Delete?
jr   z, getPlayersName_delete ; Yes, skip
cp   KEYENT                ; Enter?
jr   z, getPlayersName_enter ; Yes, skip	
push de                    ; Preserves DE
ld   e, a                  ; E = code Ascii
ld   a, LENNAME            ; A = maximum length name
cp   d                     ; D = maximum length?
ld   a, e                  ; A = code Ascii
pop  de                    ; Retrieve DE
jr   z, getPlayersName_getName ; D = maximum length
                               ; other character
                               ; Enter or Delete

ld   (hl), a               ; Append character to name
inc  hl                    ; HL = next position
rst  $10                   ; Print character
inc  d                     ; D+=1
jr   getPlayersName_getName; Request another character

getPlayersName_delete:
ld   a, $00                ; A = 0
cp   d                     ; Length 0?
jr   z, getPlayersName_getName ; Yes, another character

dec  d                     ; D-=1
dec  hl                    ; HL-=1, previous character
ld   a, ' '                ; A = space
ld   (hl), a               ; Clear previous character
ld   bc, (CURSOR)          ; BC = cursor position
inc  c                     ; BC = previous column for AT
call AT                    ; Position cursor
rst  $10                   ; Delete the display character	
call AT                    ; Position cursor
jr   getPlayersName_getName; Other character

getPlayersName_enter:
ld   a, 0                  ; A = 0
cp   d                     ; Length 0?
jr   z, getPlayersName_getName ; Yes, another character
ret                        ; End name" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Ask for the names of the players.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC, DE and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">GetPlayersName:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1           </span><span style="color: #357B42; font-style: italic">; HL = address player name 1</span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p1+</span><span style="color: #174781">$01</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; DE = HL+1</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME*</span><span style="color: #174781">2</span><span style="color: #002339">-</span><span style="color: #174781">$01</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; BC = length names - 1</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot; &quot;             </span><span style="color: #357B42; font-style: italic">; Clears first position</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Clean up the rest</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   e, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; E = 1</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_loop:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4               </span><span style="color: #357B42; font-style: italic">; A = ink 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0f</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$01</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; X = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLL                   </span><span style="color: #357B42; font-style: italic">; Delete the line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerNumber </span><span style="color: #357B42; font-style: italic">; HL = player number</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">1</span><span style="color: #002339">&quot;             </span><span style="color: #357B42; font-style: italic">; Player 1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1</span></span>
<span class="line"><span style="color: #002339">cp   e                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">2</span><span style="color: #002339">&quot;             </span><span style="color: #357B42; font-style: italic">; Player 2 </span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_cont:</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerName   </span><span style="color: #357B42; font-style: italic">; HL = title player name</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString           </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1           </span><span style="color: #357B42; font-style: italic">; HL = address player name 1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1</span></span>
<span class="line"><span style="color: #002339">cp   e                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont2 </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2           </span><span style="color: #357B42; font-style: italic">; HL = address player name 2</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_cont2:</span></span>
<span class="line"><span style="color: #002339">ld   d, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; D = counter length name</span></span>
<span class="line"><span style="color: #002339">ld   a, INK3               </span><span style="color: #357B42; font-style: italic">; A = ink 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> getPlayersName_getName</span><span style="color: #357B42; font-style: italic">; Request player name</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)       </span><span style="color: #357B42; font-style: italic">; A = players</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Two players?</span></span>
<span class="line"><span style="color: #002339">jr   nz, getPlayersName_onlyOne </span><span style="color: #357B42; font-style: italic">; One player, default name</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  e                     </span><span style="color: #357B42; font-style: italic">; E+=1</span></span>
<span class="line"><span style="color: #002339">cp   e                     </span><span style="color: #357B42; font-style: italic">; Compare with players</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_loop</span><span style="color: #357B42; font-style: italic">; Equals, jumps</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Single player</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Copies the default name of player 2</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_onlyOne:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2Default    </span><span style="color: #357B42; font-style: italic">; HL = default player name 2</span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p2           </span><span style="color: #357B42; font-style: italic">; DE = name player 2</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME           </span><span style="color: #357B42; font-style: italic">; Length name</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Copy default name</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Sale</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Requests the player&#39;s name</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_getName:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl                    </span><span style="color: #357B42; font-style: italic">; Preserve HL</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha          </span><span style="color: #357B42; font-style: italic">; Wait for valid key</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; Retrieve HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">cp   KEYDEL                </span><span style="color: #357B42; font-style: italic">; Delete?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_delete </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">cp   KEYENT                </span><span style="color: #357B42; font-style: italic">; Enter?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_enter </span><span style="color: #357B42; font-style: italic">; Yes, skip	</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de                    </span><span style="color: #357B42; font-style: italic">; Preserves DE</span></span>
<span class="line"><span style="color: #002339">ld   e, a                  </span><span style="color: #357B42; font-style: italic">; E = code Ascii</span></span>
<span class="line"><span style="color: #002339">ld   a, LENNAME            </span><span style="color: #357B42; font-style: italic">; A = maximum length name</span></span>
<span class="line"><span style="color: #002339">cp   d                     </span><span style="color: #357B42; font-style: italic">; D = maximum length?</span></span>
<span class="line"><span style="color: #002339">ld   a, e                  </span><span style="color: #357B42; font-style: italic">; A = code Ascii</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de                    </span><span style="color: #357B42; font-style: italic">; Retrieve DE</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName </span><span style="color: #357B42; font-style: italic">; D = maximum length</span></span>
<span class="line"><span style="color: #002339">                               </span><span style="color: #357B42; font-style: italic">; other character</span></span>
<span class="line"><span style="color: #002339">                               </span><span style="color: #357B42; font-style: italic">; Enter or Delete</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   (hl), a               </span><span style="color: #357B42; font-style: italic">; Append character to name</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = next position</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Print character</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  d                     </span><span style="color: #357B42; font-style: italic">; D+=1</span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span><span style="color: #357B42; font-style: italic">; Request another character</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">getPlayersName_delete:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 0</span></span>
<span class="line"><span style="color: #002339">cp   d                     </span><span style="color: #357B42; font-style: italic">; Length 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName </span><span style="color: #357B42; font-style: italic">; Yes, another character</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  d                     </span><span style="color: #357B42; font-style: italic">; D-=1</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL-=1, previous character</span></span>
<span class="line"><span style="color: #002339">ld   a, &#39; &#39;                </span><span style="color: #357B42; font-style: italic">; A = space</span></span>
<span class="line"><span style="color: #002339">ld   (hl), a               </span><span style="color: #357B42; font-style: italic">; Clear previous character</span></span>
<span class="line"><span style="color: #002339">ld   bc, (CURSOR)          </span><span style="color: #357B42; font-style: italic">; BC = cursor position</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; BC = previous column for AT</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Delete the display character	</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span><span style="color: #357B42; font-style: italic">; Other character</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">getPlayersName_enter:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">0</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; A = 0</span></span>
<span class="line"><span style="color: #002339">cp   d                     </span><span style="color: #357B42; font-style: italic">; Length 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName </span><span style="color: #357B42; font-style: italic">; Yes, another character</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; End name</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Let&#8217;s see if it works. In main.asm we find Init, and under CALL CLS we add the call to the name request, and another call to CLS.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call GetPlayersName        ; Request player names
call CLS                   ; Clear screen" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> GetPlayersName        </span><span style="color: #357B42; font-style: italic">; Request player names</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLS                   </span><span style="color: #357B42; font-style: italic">; Clear screen</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the results. We can now enter the names of the players and they will appear in the game information. If it is a single player game, we are playing against ZX Spectrum.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa2d144c&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa2d144c" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="704" height="606" data-attachment-id="4558" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-03/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?fit=704%2C606&amp;ssl=1" data-orig-size="704,606" data-comments-opened="1" data-image-title="TicTacToe-S04-03" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?fit=704%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?resize=704%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4558" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?w=704&amp;ssl=1 704w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 704px) 100vw, 704px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="shift-time" class="wp-block-heading has-large-font-size">Shift time</h2>



<p class="has-medium-font-size wp-block-paragraph">One of the options is the number of seconds each player has to make their move. If the move has not been made within these seconds, the player loses the turn and it passes to the other player.</p>



<p class="has-medium-font-size wp-block-paragraph">We will use interrupts to control the time. We create the file int.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">At the top of main.asm, just below ORG $5E88, we add the following lines (we already did this in Space Battle):</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Flags
; bit 0 -&gt; Reset countdown
; bit 1 -&gt; Lose turn
; bit 2 -&gt; Paint countdown
; bit 3 -&gt; Warning sound, countdown ends
; -------------------------------------------------------------------
flags:     db $00
; Value countdown
countdown: db $00
; Seconds per shift
seconds:   db $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Flags</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 0 -&gt; Reset countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 1 -&gt; Lose turn</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 2 -&gt; Paint countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 3 -&gt; Warning sound, countdown ends</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #174781">flags</span><span style="color: #002339">:     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Value countdown</span></span>
<span class="line"><span style="color: #7EB233">countdown:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Seconds per shift</span></span>
<span class="line"><span style="color: #7EB233">seconds:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Seconds is used to tell the interrupts how many seconds per turn the players have chosen. With seconds, the MaxSeconds variable is no longer needed, so we will remove it.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate Main and after the CALL OPENCHAN line, initialise seconds:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $10                ; A = $10 BCD
ld   (seconds), a          ; Update seconds
ld   (countdown), a        ; Update countdown" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$10</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = $10 BCD</span></span>
<span class="line"><span style="color: #002339">ld   (seconds), a          </span><span style="color: #357B42; font-style: italic">; Update seconds</span></span>
<span class="line"><span style="color: #002339">ld   (countdown), a        </span><span style="color: #357B42; font-style: italic">; Update countdown</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate menu_Time and after JR NZ, menu_op modify the line LD A, (MaxSeconds), as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (seconds)          ; A = seconds" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (seconds)          </span><span style="color: #357B42; font-style: italic">; A = seconds</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate menu_TimeDo, delete LD, (MaxSecond), A and add the following lines instead:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   (seconds), a          ; Update seconds
ld   (countdown), a        ; Update countdown" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   (seconds), a          </span><span style="color: #357B42; font-style: italic">; Update seconds</span></span>
<span class="line"><span style="color: #002339">ld   (countdown), a        </span><span style="color: #357B42; font-style: italic">; Update countdown</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In screen.asm, at the end of the PrintOptions routine, we replace LD HL, MaxSeconds with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, seconds           ; HL = time value" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, seconds           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Finally, we delete the MaxSeconds definition in var.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">The interrupt routine will change the value of the flags and the countdown, we need to take this into account in the main loop.</p>



<p class="has-medium-font-size wp-block-paragraph">For the interrupt routine to run fifty times per second (in PAL, sixty in NTSC), mode two of the interrupts must be enabled. Locate Loop in main.asm and add over it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="di                         ; Disables interruptions
ld   a, $28                ; A = $28
ld   i, a                  ; I = A (interruptions in $7e5c)
im   $02                   ; Interruptions = mode 2
ei                         ; Activates interruptions" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #174781">di</span><span style="color: #002339">                         </span><span style="color: #357B42; font-style: italic">; Disables interruptions</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$28</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = $28</span></span>
<span class="line"><span style="color: #002339">ld   i, a                  </span><span style="color: #357B42; font-style: italic">; I = A (interruptions in $7e5c)</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">$02</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Interruptions = mode 2</span></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We disable the interrupts, load $28 into the interrupt register, set it to mode two and enable it.</p>



<p class="has-medium-font-size wp-block-paragraph">We look for loop_key and just below it we implement the flag handling logic.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (flags)            ; A = Flags
bit  $02, a                ; Bit 2 active?
res  $02, a                ; Disables bit 2
jr   z, loop_warning       ; Not active, skips
push af                    ; Preserve AF
call PrintCountDown        ; Paint countdown
pop  af                    ; Retrieve AF" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">flags</span><span style="color: #002339">)            </span><span style="color: #357B42; font-style: italic">; A = Flags</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$02</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Bit 2 active?</span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$02</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Disables bit 2</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_warning       </span><span style="color: #357B42; font-style: italic">; Not active, skips</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> af                    </span><span style="color: #357B42; font-style: italic">; Preserve AF</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintCountDown        </span><span style="color: #357B42; font-style: italic">; Paint countdown</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  af                    </span><span style="color: #357B42; font-style: italic">; Retrieve AF</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We load the flags in A and evaluate if bit two is active and deactivate it. If it is not active, we jump, if it is, we draw the countdown.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="loop_warning:
bit  $03, a                ; Bit 3 active?             
res  $03, a                ; Disable bit 3
jr   z, loop_lostMov       ; Not active, skip
ld   bc, SoundCountDown    ; BC = sound direction 
call PlayMusic             ; Outputs sound" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">loop_warning:</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$03</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Bit 3 active?             </span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$03</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Disable bit 3</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_lostMov       </span><span style="color: #357B42; font-style: italic">; Not active, skip</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundCountDown    </span><span style="color: #357B42; font-style: italic">; BC = sound direction </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Outputs sound</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We evaluate whether bit three is active and deactivate it. If it is active, we jump, if it is not, we give the warning sound.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="loop_lostMov:
bit  $01, a                ; Bit 1 active?
res  $01, a                ; Disables bit 1
ld   (flags), a            ; Update in memory
halt                       ; Synchronise with interrupts
jr   z, loop_keyCont       ; Not active, skip
ld   hl, TitleLostMovement ; HL = address mov lost
ld   de, TitleLostMov_name ; DE = address name
call DoMsg                 ; Paint message
ld   bc, SoundLostMovement ; BC = sound direction
call PlayMusic             ; Outputs sound
jr   loop_cont             ; Jump
loop_keyCont:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">loop_lostMov:</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$01</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Bit 1 active?</span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$01</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Disables bit 1</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">flags</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">halt                       </span><span style="color: #357B42; font-style: italic">; Synchronise with interrupts</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_keyCont       </span><span style="color: #357B42; font-style: italic">; Not active, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleLostMovement </span><span style="color: #357B42; font-style: italic">; HL = address mov lost</span></span>
<span class="line"><span style="color: #002339">ld   de, TitleLostMov_name </span><span style="color: #357B42; font-style: italic">; DE = address name</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg                 </span><span style="color: #357B42; font-style: italic">; Paint message</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundLostMovement </span><span style="color: #357B42; font-style: italic">; BC = sound direction</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Outputs sound</span></span>
<span class="line"><span style="color: #002339">jr   loop_cont             </span><span style="color: #357B42; font-style: italic">; Jump</span></span>
<span class="line"><span style="color: #7EB233">loop_keyCont:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We check if bit one is active, disable it, update the flags and synchronise with the interrupts. If it is, we draw the lost motion message and play the sound. The loop_keyCont tag is above the CALL WaitKeyBoard.</p>



<p class="has-medium-font-size wp-block-paragraph">Finally, locate loop_cont, delete JR Loop and add the following lines in its place:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $01                ; A = restart countdown
ld   (flags), a            ; Update flags
jp   Loop                  ; Main loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = restart countdown</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">flags</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Update flags</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   </span><span style="color: #7B30D0">Loop</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Main loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have updated the flags so that the interrupt routine restarts the countdown. We also replaced JR with JP because the lines we added make JR out of range.</p>



<p class="has-medium-font-size wp-block-paragraph">In screen.asm we will implement the routine that draws the countdown.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the countdown.
;
; Alters the value of the AF, BC and HL registers.
; -------------------------------------------------------------------
PrintCountDown:
ld   a, INK3               ; A = ink 3
call INK                   ; Change ink
ld   b, INI_TOP-$0c        ; B = coord Y
ld   c, INI_LEFT           ; C = coord X
call AT                    ; Position cursor
ld   hl, countdown         ; HL = direction countdown
call PrintBCD              ; Paint countdown left
ld   c, INI_LEFT-$1e       ; C = coord X
call AT                    ; Position cursor
jp   PrintBCD              ; Paints countdown right and exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the countdown.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintCountDown:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK3               </span><span style="color: #357B42; font-style: italic">; A = ink 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0c</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT           </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, countdown         </span><span style="color: #357B42; font-style: italic">; HL = direction countdown</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paint countdown left</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$1e</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints countdown right and exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We paint the countdown in two positions, on the left and right of the board. We set the ink to magenta, move the cursor to the left, draw the countdown, move the cursor to the right and draw the countdown.</p>



<p class="has-medium-font-size wp-block-paragraph">The interrupt handling routine is implemented in int.asm (do not include this file in main.asm). We will also look at this implementation in blocks.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; int.asm
;
; Mode 2 Interrupt Handling
; -------------------------------------------------------------------
org $7e5c

; -------------------------------------------------------------------
; Indicators
; bit 0 -&gt; Reset countdown
; bit 1 -&gt; Lose turn
; bit 2 -&gt; Paint countdown
; bit 3 -&gt; Warning sound, countdown ends
; -------------------------------------------------------------------
FLAGS:     equ $5e88

COUNTDOWN: equ FLAGS+$01   ; Countdown Value
SECONDS:   equ FLAGS+$02   ; Seconds per turn" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; int.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Mode 2 Interrupt Handling</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #002339">org </span><span style="color: #174781">$7e5c</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Indicators</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 0 -&gt; Reset countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 1 -&gt; Lose turn</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 2 -&gt; Paint countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 3 -&gt; Warning sound, countdown ends</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #174781">FLAGS</span><span style="color: #002339">:     </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5e88</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">COUNTDOWN:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">FLAGS</span><span style="color: #002339">+</span><span style="color: #174781">$01</span><span style="color: #002339">   </span><span style="color: #357B42; font-style: italic">; Countdown Value</span></span>
<span class="line"><span style="color: #7EB233">SECONDS:</span><span style="color: #002339">   </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">FLAGS</span><span style="color: #002339">+</span><span style="color: #174781">$02</span><span style="color: #002339">   </span><span style="color: #357B42; font-style: italic">; Seconds per turn</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The interrupt routine is loaded at address $7E5C. Then we add the constants with the memory addresses we will use for the information exchange between main.asm and int.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="CountDownISR:
push af  
push bc  
push de  
push hl  
push ix                    ; Preserves records" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">CountDownISR:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> af  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> ix                    </span><span style="color: #357B42; font-style: italic">; Preserves records</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The first step of the routine is to preserve the value of the registers.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDown_flags:
ld   a, (FLAGS)            ; A = flags
and  $01                   ; Reset countdown?
jr   z, countDown_cont     ; No, skip
ld   a, (SECONDS)          ; A = SECONDS
ld   (COUNTDOWN), a        ; Refreshes in memory
ld   a, $04                ; A = paint countdown
ld   (FLAGS), a            ; Update in memory
jr   countDownISR_end      ; End" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDown_flags:</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">FLAGS</span><span style="color: #002339">)            </span><span style="color: #357B42; font-style: italic">; A = flags</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Reset countdown?</span></span>
<span class="line"><span style="color: #002339">jr   z, countDown_cont     </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (SECONDS)          </span><span style="color: #357B42; font-style: italic">; A = SECONDS</span></span>
<span class="line"><span style="color: #002339">ld   (COUNTDOWN), a        </span><span style="color: #357B42; font-style: italic">; Refreshes in memory</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$04</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = paint countdown</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">FLAGS</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">jr   countDownISR_end      </span><span style="color: #357B42; font-style: italic">; End</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We check if main.asm indicates that the countdown should be restarted (shift change) and jump if it does not. If there is a shift, it restarts the countdown at the value specified in the menu, indicates in flags that the countdown should be painted, and jumps to exit the routine.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDown_cont:
ld   hl, countDownTicks    ; HL = ticks counter
inc  (hl)                  ; Counter ticks+=1
ld   a, $32                ; A = 50
cp   (hl)                  ; Counter ticks = 50?
jr   nz, countDownISR_end  ; No, skip.
xor  a                     ; A = 0, Z = 1, Carry = 0
ld   (hl), a               ; Counter ticks = 0" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDown_cont:</span></span>
<span class="line"><span style="color: #002339">ld   hl, countDownTicks    </span><span style="color: #357B42; font-style: italic">; HL = ticks counter</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter ticks+=1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$32</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 50</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter ticks = 50?</span></span>
<span class="line"><span style="color: #002339">jr   nz, countDownISR_end  </span><span style="color: #357B42; font-style: italic">; No, skip.</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A = 0, Z = 1, Carry = 0</span></span>
<span class="line"><span style="color: #002339">ld   (hl), a               </span><span style="color: #357B42; font-style: italic">; Counter ticks = 0</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In countDownTicks we add one at each pause, and when we reach fifty it is a sign that a second has passed, which we check in this block. If it has not reached fifty, it jumps to exit the routine, if it has, we set the ticks counter to zero and continue with the routine.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; It's reached 50, it's been a second.
ld   a, (COUNTDOWN)        ; A = countdown value
dec  a                     ; A-=1
daa                        ; Decimal setting
ld   (COUNTDOWN), a        ; Update in memory
ld   b, $04                ; B = paint countdown / 4 sec	
cp   b                     ; Less than 4 seconds?
jr   nc, countDownISR_reset; A &gt;= 4, skip
set  $03, b                ; Warning sound
or   a                     ; A = 0?
jr   nz, countDownISR_reset; No, skip
set  $01, b                ; Misses turn" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; It&#39;s reached 50, it&#39;s been a second.</span></span>
<span class="line"><span style="color: #002339">ld   a, (COUNTDOWN)        </span><span style="color: #357B42; font-style: italic">; A = countdown value</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A-=1</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Decimal setting</span></span>
<span class="line"><span style="color: #002339">ld   (COUNTDOWN), a        </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$04</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = paint countdown / 4 sec	</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Less than 4 seconds?</span></span>
<span class="line"><span style="color: #002339">jr   nc, countDownISR_reset</span><span style="color: #357B42; font-style: italic">; A &gt;= 4, skip</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$03</span><span style="color: #002339">, b                </span><span style="color: #357B42; font-style: italic">; Warning sound</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; A = 0?</span></span>
<span class="line"><span style="color: #002339">jr   nz, countDownISR_reset</span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$01</span><span style="color: #002339">, b                </span><span style="color: #357B42; font-style: italic">; Misses turn</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If one second has passed, we calculate what kind of information to pass to main.asm. We subtract one second from the countdown, see if it is below four (it also communicates to main.asm that the countdown has to be painted), and jump if it is not. If it is, we set the bit for the warning sound, evaluate whether the countdown has reached zero, and jump if it has not. Turn on the shift loss bit if it has reached zero.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDownISR_reset:
ld   a, b                  ; A = B
ld   (FLAGS), a            ; Updates in memory" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDownISR_reset:</span></span>
<span class="line"><span style="color: #002339">ld   a, b                  </span><span style="color: #357B42; font-style: italic">; A = B</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">FLAGS</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Updates in memory</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Update the value of flags with the information you want to pass to main.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDownISR_end:
pop  ix     
pop  hl     
pop  de     
pop  bc     
pop  af                    ; Retrieves records
	

ei                         ; Activates interruptions
reti                       ; Exits

countDownTicks: db $00     ; Ticks (50*sec)" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDownISR_end:</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  ix     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  af                    </span><span style="color: #357B42; font-style: italic">; Retrieves records</span></span>
<span class="line"><span style="color: #002339">	</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span>
<span class="line"><span style="color: #002339">reti                       </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">countDownTicks:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Ticks (50*sec)</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We get the value of the registers, enable interrupts and exit. Finally we declare the tick counter.</p>



<p class="has-medium-font-size wp-block-paragraph">Remember that we now need to compile two separate .tap&#8217;s and make our own loader.</p>



<p class="has-medium-font-size wp-block-paragraph">The basic loader looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">VB</span><span role="button" tabindex="0" data-code="10 CLEAR 24200
20 LOAD &quot;&quot;CODE 24200
30 LOAD &quot;&quot;CODE 32348
40 RANDOMIZE USR 24200" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #174781">10</span><span style="color: #002339"> </span><span style="color: #1AB394">CLEAR</span><span style="color: #002339"> </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #174781">20</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #174781">30</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">32348</span></span>
<span class="line"><span style="color: #174781">40</span><span style="color: #0991B6"> RANDOMIZE </span><span style="color: #002339">USR </span><span style="color: #174781">24200</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The script to compile on Windows looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="echo off
cls
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Generating Tic-tac-toe
copy /y /b loader.tap+oxo.tap+int.tap TicTacToe.tap
echo Process completed" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">off</span></span>
<span class="line"><span style="color: #7EB233">cls</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-tac-toe</span></span>
<span class="line"><span style="color: #7EB233">copy</span><span style="color: #002339"> </span><span style="color: #A44185">/y</span><span style="color: #002339"> </span><span style="color: #A44185">/b</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap+oxo.tap+int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Process</span><span style="color: #002339"> </span><span style="color: #A44185">completed</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The Linux version looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="clear
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Generating Tic-tac-toe
cat loader.tap oxo.tap int.tap &gt; TicTacToe.tap
echo Process completed" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">clear</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-tac-toe</span></span>
<span class="line"><span style="color: #7EB233">cat</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #7B30D0">&gt;</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Process</span><span style="color: #002339"> </span><span style="color: #A44185">completed</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile it, load it into the emulator and check that the countdown is painted, that an alarm goes off if it goes under four seconds and that it loses the turn if it goes to zero. We also see the missed turn message.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa2d45b2&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa2d45b2" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="702" height="606" data-attachment-id="4561" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-04/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?fit=702%2C606&amp;ssl=1" data-orig-size="702,606" data-comments-opened="1" data-image-title="TicTacToe-S04-04" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?fit=702%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?resize=702%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4561" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?w=702&amp;ssl=1 702w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 702px) 100vw, 702px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="end-of-game" class="wp-block-heading has-large-font-size">End of game</h2>



<p class="has-medium-font-size wp-block-paragraph">At the end of this chapter we will implement the end of the game, which occurs when one of the players reaches the points defined in the menu, or when the maximum number of tables is reached.</p>



<p class="has-medium-font-size wp-block-paragraph">At the end of the game we are asked if we want another game; we add the keys of the answer as constants in var.asm. We also add the maximum number of tables.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="KEYN:       equ $4e        ; N key
KEYn:       equ $6e        ; Key n
KEYS:       equ $53        ; S key
KEYs:       equ $73        ; Key s
MAXTIES:    equ $05        ; Maximum number of tables" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">KEYN:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$4e</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; N key</span></span>
<span class="line"><span style="color: #7EB233">KEYn:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$6e</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key n</span></span>
<span class="line"><span style="color: #7EB233">KEYS:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$53</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; S key</span></span>
<span class="line"><span style="color: #7EB233">KEYs:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$73</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key s</span></span>
<span class="line"><span style="color: #7EB233">MAXTIES:</span><span style="color: #002339">    </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Maximum number of tables</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Depending on whether you want to play another game or not, you will jump to one place or another in main.asm. We locate the Init tag, and after CALL GetPlayersName we add the following tag:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Start:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Start:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We locate the loop_reset tag and just below it we will implement the check to see if someone has won the game.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintPoints           ; Paint the dots
ld   a, (MaxPoints)        ; A = maximum points
ld   b, a                  ; B = A
ld   a, (Points_p1)        ; A = points player 1
cp   b                     ; Player 1 wins?
jr   z, EndPlay            ; Yes, skip 
ld   a, (Points_p2)        ; A = points player 2
cp   b                     ; Player 2 wins?
jr   z, EndPlay            ; Yes, skip
ld   b, MAXTIES            ; B = maximum tables
ld   a, (Points_tie)       ; A = points tables
cp   b                     ; A = B?
jr   z, EndPlay            ; Yes, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintPoints           </span><span style="color: #357B42; font-style: italic">; Paint the dots</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPoints)        </span><span style="color: #357B42; font-style: italic">; A = maximum points</span></span>
<span class="line"><span style="color: #002339">ld   b, a                  </span><span style="color: #357B42; font-style: italic">; B = A</span></span>
<span class="line"><span style="color: #002339">ld   a, (Points_p1)        </span><span style="color: #357B42; font-style: italic">; A = points player 1</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Player 1 wins?</span></span>
<span class="line"><span style="color: #002339">jr   z, EndPlay            </span><span style="color: #357B42; font-style: italic">; Yes, skip </span></span>
<span class="line"><span style="color: #002339">ld   a, (Points_p2)        </span><span style="color: #357B42; font-style: italic">; A = points player 2</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Player 2 wins?</span></span>
<span class="line"><span style="color: #002339">jr   z, EndPlay            </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   b, MAXTIES            </span><span style="color: #357B42; font-style: italic">; B = maximum tables</span></span>
<span class="line"><span style="color: #002339">ld   a, (Points_tie)       </span><span style="color: #357B42; font-style: italic">; A = points tables</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, EndPlay            </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We draw the points and compare the players&#8217; scores with the set maximum score. If one of the players has the required number of points, they win the game.</p>



<p class="has-medium-font-size wp-block-paragraph">At the end of main.asm, before the includes, we add the split end routine.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="EndPlay:
di                         ; Disables interrupts
im   $01                   ; Mode 1 interrupts
ei                         ; Activates interruptions
ld   hl, TitleGameOver     ; HL = title game over
call PrintMsg              ; Paints the message
endPlay_waitKey:
call WaitKeyAlpha          ; Wait key
cp   KEYN                  ; Pressed N?
jp   z, Menu               ; Yes, menu
cp   KEYn                  ; Pushed n?
jp   z, Menu               ; Yes, menu
cp   KEYS                  ; Pulsed S?
jp   z, Start              ; Yes, start
cp   KEYs                  ; Pushed s?
jp   z, Start              ; Yes, start
jr   endPlay_waitKey       ; Not pressed, loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">EndPlay:</span></span>
<span class="line"><span style="color: #174781">di</span><span style="color: #002339">                         </span><span style="color: #357B42; font-style: italic">; Disables interrupts</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Mode 1 interrupts</span></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleGameOver     </span><span style="color: #357B42; font-style: italic">; HL = title game over</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg              </span><span style="color: #357B42; font-style: italic">; Paints the message</span></span>
<span class="line"><span style="color: #7EB233">endPlay_waitKey:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha          </span><span style="color: #357B42; font-style: italic">; Wait key</span></span>
<span class="line"><span style="color: #002339">cp   KEYN                  </span><span style="color: #357B42; font-style: italic">; Pressed N?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Menu               </span><span style="color: #357B42; font-style: italic">; Yes, menu</span></span>
<span class="line"><span style="color: #002339">cp   KEYn                  </span><span style="color: #357B42; font-style: italic">; Pushed n?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Menu               </span><span style="color: #357B42; font-style: italic">; Yes, menu</span></span>
<span class="line"><span style="color: #002339">cp   KEYS                  </span><span style="color: #357B42; font-style: italic">; Pulsed S?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Start              </span><span style="color: #357B42; font-style: italic">; Yes, start</span></span>
<span class="line"><span style="color: #002339">cp   KEYs                  </span><span style="color: #357B42; font-style: italic">; Pushed s?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Start              </span><span style="color: #357B42; font-style: italic">; Yes, start</span></span>
<span class="line"><span style="color: #002339">jr   endPlay_waitKey       </span><span style="color: #357B42; font-style: italic">; Not pressed, loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We deactivate interruptions, switch to mode one, activate interruptions, wait for the question to be answered and, depending on the answer, we jump to one place or another.</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Menu: Return to the main menu. We can select the different options and enter new player names.</li>



<li class="has-medium-font-size">Start: is the label we have added. It clears the screen, paints the screen, resets the game data and switches interrupts to mode two.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">If none of the expected keys have been pressed, it will loop until one is.</p>



<p class="has-medium-font-size wp-block-paragraph">If you go to the Menu tag, you will see that the first three lines are for switching to interrupt mode one, which is what we do in EndPlay. Delete the three lines below the Menu tag.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the results. Everything seems to work fine, but at the end of the game the score is flashing. This is something we inherited from the beginning, although we didn&#8217;t notice it until now.</p>



<p class="has-medium-font-size wp-block-paragraph">To change the behaviour, locate PrintPoints in screen.asm and add the lines needed to disable the flickering just below it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a,(ATTR_T)            ; A = temporary attributes
and  $7f                   ; Removes flicker 
ld   (ATTR_T), a           ; Update in memory" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a,(ATTR_T)            </span><span style="color: #357B42; font-style: italic">; A = temporary attributes</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$7f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Removes flicker </span></span>
<span class="line"><span style="color: #002339">ld   (ATTR_T), a           </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span></code></pre></div>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa2d5f65&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa2d5f65" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="702" height="606" data-attachment-id="4562" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-05/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?fit=702%2C606&amp;ssl=1" data-orig-size="702,606" data-comments-opened="1" data-image-title="TicTacToe-S04-05" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?fit=702%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?resize=702%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4562" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?w=702&amp;ssl=1 702w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 702px) 100vw, 702px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">At this point, we can play the first full two-player games.</p>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">Want to play against the Spectrum and beat it? We will implement this in the next chapter of ZX Spectrum Assembly, </p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/28bdpg8p" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4554</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x03 Sound</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x03-sound/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x03-sound/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Wed, 04 Oct 2023 07:00:00 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac_toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4521</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we will implement different sounds and use them for timing by pausing between events. Sound]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we will implement different sounds and use them for timing by pausing between events.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#sound">Sound</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul></div>


<h2 id="sound" class="wp-block-heading has-large-font-size">Sound</h2>



<p class="has-medium-font-size wp-block-paragraph">We will create the file sound.asm and add the include to main.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">In sound.asm we will add the definition of the different sounds and melodies.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Sounds.
;
; Sounds end in $00, the byte indicating the end.
; -------------------------------------------------------------------
; Countdown
SoundCountDown:
db $03, $8c, $3a, $00

; Error
SoundError:
db $0d, $c6, $1e, $16, $13, $13, $00

; Loss of movement
SoundLostMovement:	
db $0d, $07, $10, $0b, $96, $12, $0a, $4d, $14
db $0b, $96, $12, $1a, $2c, $08, $00

; Next player
SoundNextPlayer:
db $01, $9d, $7b, $00

; Spectrum Movement
SoundSpectrum:
db $06, $6e, $20, $06, $6e, $20, $05, $b7, $24
db $06, $6e, $20, $05, $13, $29, $05, $b7, $24
db $06, $6e, $20, $00

; Tables
SoundTie:
db $0d, $07, $10, $0b, $96, $12, $06, $d4, $1e
db $0b, $96, $12, $07, $a6, $1b, $0b, $96, $12
db $08, $a5, $18, $0b, $96, $12, $09, $b4, $15
db $0b, $96, $12, $0a, $4d, $14, $0b, $96, $12
db $0d, $07, $10, $00

; Point
SoundWinGame:
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $c7, $2b
db $05, $13, $29, $05, $b7, $24, $05, $13, $29
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $c7, $2b
db $05, $13, $29, $05, $b7, $24, $06, $6e, $20
db $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sounds.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sounds end in $00, the byte indicating the end.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Countdown</span></span>
<span class="line"><span style="color: #7EB233">SoundCountDown:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$8c</span><span style="color: #002339">, </span><span style="color: #174781">$3a</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Error</span></span>
<span class="line"><span style="color: #7EB233">SoundError:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, $c6, </span><span style="color: #174781">$1e</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Loss of movement</span></span>
<span class="line"><span style="color: #7EB233">SoundLostMovement:</span><span style="color: #002339">	</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, </span><span style="color: #174781">$4d</span><span style="color: #002339">, </span><span style="color: #174781">$14</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$1a</span><span style="color: #002339">, </span><span style="color: #174781">$2c</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Next player</span></span>
<span class="line"><span style="color: #7EB233">SoundNextPlayer:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$9d</span><span style="color: #002339">, </span><span style="color: #174781">$7b</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum Movement</span></span>
<span class="line"><span style="color: #7EB233">SoundSpectrum:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Tables</span></span>
<span class="line"><span style="color: #7EB233">SoundTie:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, $d4, </span><span style="color: #174781">$1e</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, $a6, </span><span style="color: #174781">$1b</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$08</span><span style="color: #002339">, $a5, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$09</span><span style="color: #002339">, $b4, </span><span style="color: #174781">$15</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, </span><span style="color: #174781">$4d</span><span style="color: #002339">, </span><span style="color: #174781">$14</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Point</span></span>
<span class="line"><span style="color: #7EB233">SoundWinGame:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, $c7, </span><span style="color: #174781">$2b</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, $c7, </span><span style="color: #174781">$2b</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">All sounds have a zero byte as their last byte, which we will use in the routine that plays them to know when they are finished.</p>



<p class="has-medium-font-size wp-block-paragraph">In these definitions, each note is made up of three bytes instead of four, the first two being the note and the third being the low byte of the duration, all of which are sixteenths and/or half notes.</p>



<p class="has-medium-font-size wp-block-paragraph">We implement the routine that plays the tones.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Plays a melody or sound.
;
; Input: BC = Start address of the melody.
;
; To save is done byte by byte, with the duration always being fuzzy
; or half-fuse; only one byte is needed for the duration.
; This routine is adapted for this operation.
; -------------------------------------------------------------------
PlayMusic:
push af
push bc
push de
push hl
push ix                    ; Preserves records

playMusic_loop:
ld   a, (bc)               ; A = high byte note
ld   h, a                  ; H = A
or   a                     ; A = 0?
jr   z, playMusic_end      ; A = 0, end

inc  bc                    ; BC = next value
ld   a, (bc)               ; A = byte under note
ld   l, a                  ; L = A

inc  bc                    ; BC = next value
ld   a, (bc)               ; A = duration note
ld   e, a                  ; E = A
ld   d, $00                ; D = 0 (fuse or half-fuse)
inc  bc                    ; BC = next value

push bc                    ; Preserves BC
call BEEPER                ; Play note
pop  bc                    ; Recover BC

jr   playMusic_loop        ; Loop until it reaches 0 end melody

playMusic_end:
pop  ix
pop  hl
pop  de
pop  bc
pop  af                    ; Retrieves records
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Plays a melody or sound.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: BC = Start address of the melody.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; To save is done byte by byte, with the duration always being fuzzy</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; or half-fuse; only one byte is needed for the duration.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; This routine is adapted for this operation.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PlayMusic:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> af</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> ix                    </span><span style="color: #357B42; font-style: italic">; Preserves records</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">playMusic_loop:</span></span>
<span class="line"><span style="color: #002339">ld   a, (bc)               </span><span style="color: #357B42; font-style: italic">; A = high byte note</span></span>
<span class="line"><span style="color: #002339">ld   h, a                  </span><span style="color: #357B42; font-style: italic">; H = A</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; A = 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, playMusic_end      </span><span style="color: #357B42; font-style: italic">; A = 0, end</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; BC = next value</span></span>
<span class="line"><span style="color: #002339">ld   a, (bc)               </span><span style="color: #357B42; font-style: italic">; A = byte under note</span></span>
<span class="line"><span style="color: #002339">ld   l, a                  </span><span style="color: #357B42; font-style: italic">; L = A</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; BC = next value</span></span>
<span class="line"><span style="color: #002339">ld   a, (bc)               </span><span style="color: #357B42; font-style: italic">; A = duration note</span></span>
<span class="line"><span style="color: #002339">ld   e, a                  </span><span style="color: #357B42; font-style: italic">; E = A</span></span>
<span class="line"><span style="color: #002339">ld   d, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; D = 0 (fuse or half-fuse)</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; BC = next value</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserves BC</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> BEEPER                </span><span style="color: #357B42; font-style: italic">; Play note</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Recover BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">jr   playMusic_loop        </span><span style="color: #357B42; font-style: italic">; Loop until it reaches 0 end melody</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">playMusic_end:</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  ix</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  af                    </span><span style="color: #357B42; font-style: italic">; Retrieves records</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PlayMusic receives the address of the sound to be played in BC, loads the note in HL, the duration in DE, plays it and loops until it reaches the zero byte which marks the end of the sound. D is always zero, only one byte is used for the duration.</p>



<p class="has-medium-font-size wp-block-paragraph">In main.asm we will add the necessary calls to play the sounds.</p>



<p class="has-medium-font-size wp-block-paragraph">We locate the Loop tag and delete from LD B, $19 to DJNZ loop_wait as we will be timing with the sound. Below this, after CALL PrintPoints, we add the next player sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundNextPlayer
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundNextPlayer</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_print tag and just above it the JR Loop line. Above this line we add the error sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundError
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundError</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_win tag and just below it the INC (HL) line. Below this line we add the dot winner sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundWinGame
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundWinGame</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_reset tag and just above it add the table sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundTie
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundTie</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and listen to every single sound.</p>



<p class="has-medium-font-size wp-block-paragraph">The main.asm file has grown considerably since we started, so it&#8217;s time to see what it looks like after commenting.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org  $5e88                 ; Loading address

Main:
ld   hl, Sprite_P1         ; HL = address Sprite_P1
ld   (UDG), hl             ; UDG = address Sprite_p1
ld   a, $02                ; A = 2
call OPENCHAN              ; Open channel 2

xor  a                     ; A = 0, Z = 0, C = 0
call BORDER                ; Change border
call CLA                   ; Changes display attributes
call CLS                   ; Clear screen

Init:
call PrintBoard            ; Paint board

ld   hl, Name_p1           ; HL = name player 1
ld   de, player1_name      ; DE = player1_name
ld   bc, LENNAME           ; BC = length name
ldir                       ; Pass data
ld   hl, Name_p2           ; HL = name player 2
ld   de, player2_name      ; DE = player 2 name
ld   bc, LENNAME           ; BC = length name
ldir                       ; Pass data
call PrintInfo             ; Paint info

ld   bc, LENDATA           ; BC = length of starting data
call ResetValues           ; Clear data

Loop:
ld   hl, TitleTurn         ; HL = shift address
ld   de, TitleTurn_name    ; DE = address name in turn name in turn
call DoMsg                 ; Compose and paint shift
call PrintPoints           ; PaintPoints
ld   bc, SoundNextPlayer   ; BC = sound direction
call PlayMusic             ; Play sound
loop_key:
call WaitKeyBoard          ; Wait for key press
ld   a, c                  ; A = C
cp   KEY0                  ; Key pressed?
jr   z, loop_key           ; No, loop
call ToMove                ; Check movement
jr   z, loop_print         ; Correct, skip
ld   hl, TitleError        ; HL = address error
call PrintMsg              ; Paint Error
ld   bc, SoundError        ; BC = sound address
call PlayMusic             ; Play sound
jr   Loop                  ; Main loop
loop_print:
call PrintOXO              ; Paint token
loop_checkWinner:
call CheckWinner           ; Any winners?
jr   nz, loop_tie          ; No, check tables
call PrintWinnerLine       ; Paints winning line
ld   hl, TitlePointFor     ; HL = winning address
ld   de, TitlePointName    ; HL = address winning name
call DoMsg                 ; Compose and paint winner
ld   hl, Points_p1         ; HL = address points player 1
ld   a, (PlayerMoves)      ; A = player that moved
or   a                     ; Player 1?
jr   z, loop_win           ; Yes, skip
inc  hl                    ; HL = address points player 2
loop_win:
inc  (hl)                  ; Increases score
ld   bc, SoundWinGame      ; BC = sound address
call PlayMusic             ; Play sound
jr   loop_reset            ; Jump
loop_tie:
ld   hl, MoveCounter       ; HL = move counter address
inc  (hl)                  ; Increments counter
ld   a, $09                ; A = 9
cp   (hl)                  ; Counter = 9?
jr   nz, loop_cont         ; No, skip
ld   hl, Points_tie        ; HL = address points tables
inc  (hl)                  ; Increases score
ld   hl, TitleTie          ; HL = address tables
call PrintMsg              ; Paint tables
ld   bc, SoundTie          ; BC = sound direction
call PlayMusic             ; Play sound
loop_reset:
ld   bc, LENDATA-$04       ; BC = data length clean data
call ResetValues           ; Clear data
call PrintBoard            ; Paint board
loop_cont:
ld   a, (PlayerMoves)      ; A = player who moved
xor  $01                   ; A = next player
ld   (PlayerMoves), a      ; Refresh in memory
jr   Loop                  ; Main loop

include &quot;game.asm&quot;
include &quot;rom.asm&quot;
include &quot;screen.asm&quot;
include &quot;sprite.asm&quot;
include &quot;sound.asm&quot;
include &quot;var.asm&quot;

end  Main" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$5e88</span><span style="color: #002339">                 </span><span style="color: #357B42; font-style: italic">; Loading address</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Main:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Sprite_P1         </span><span style="color: #357B42; font-style: italic">; HL = address Sprite_P1</span></span>
<span class="line"><span style="color: #002339">ld   (UDG), hl             </span><span style="color: #357B42; font-style: italic">; UDG = address Sprite_p1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> OPENCHAN              </span><span style="color: #357B42; font-style: italic">; Open channel 2</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A = 0, Z = 0, C = 0</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> BORDER                </span><span style="color: #357B42; font-style: italic">; Change border</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLA                   </span><span style="color: #357B42; font-style: italic">; Changes display attributes</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLS                   </span><span style="color: #357B42; font-style: italic">; Clear screen</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Init:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBoard            </span><span style="color: #357B42; font-style: italic">; Paint board</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1           </span><span style="color: #357B42; font-style: italic">; HL = name player 1</span></span>
<span class="line"><span style="color: #002339">ld   de, player1_name      </span><span style="color: #357B42; font-style: italic">; DE = player1_name</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME           </span><span style="color: #357B42; font-style: italic">; BC = length name</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Pass data</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2           </span><span style="color: #357B42; font-style: italic">; HL = name player 2</span></span>
<span class="line"><span style="color: #002339">ld   de, player2_name      </span><span style="color: #357B42; font-style: italic">; DE = player 2 name</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME           </span><span style="color: #357B42; font-style: italic">; BC = length name</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Pass data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintInfo             </span><span style="color: #357B42; font-style: italic">; Paint info</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   bc, LENDATA           </span><span style="color: #357B42; font-style: italic">; BC = length of starting data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ResetValues           </span><span style="color: #357B42; font-style: italic">; Clear data</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">Loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleTurn         </span><span style="color: #357B42; font-style: italic">; HL = shift address</span></span>
<span class="line"><span style="color: #002339">ld   de, TitleTurn_name    </span><span style="color: #357B42; font-style: italic">; DE = address name in turn name in turn</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg                 </span><span style="color: #357B42; font-style: italic">; Compose and paint shift</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintPoints           </span><span style="color: #357B42; font-style: italic">; PaintPoints</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundNextPlayer   </span><span style="color: #357B42; font-style: italic">; BC = sound direction</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #7EB233">loop_key:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyBoard          </span><span style="color: #357B42; font-style: italic">; Wait for key press</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; A = C</span></span>
<span class="line"><span style="color: #002339">cp   KEY0                  </span><span style="color: #357B42; font-style: italic">; Key pressed?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_key           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Check movement</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_print         </span><span style="color: #357B42; font-style: italic">; Correct, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleError        </span><span style="color: #357B42; font-style: italic">; HL = address error</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg              </span><span style="color: #357B42; font-style: italic">; Paint Error</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundError        </span><span style="color: #357B42; font-style: italic">; BC = sound address</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">Loop</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Main loop</span></span>
<span class="line"><span style="color: #7EB233">loop_print:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintOXO              </span><span style="color: #357B42; font-style: italic">; Paint token</span></span>
<span class="line"><span style="color: #7EB233">loop_checkWinner:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CheckWinner           </span><span style="color: #357B42; font-style: italic">; Any winners?</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_tie          </span><span style="color: #357B42; font-style: italic">; No, check tables</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintWinnerLine       </span><span style="color: #357B42; font-style: italic">; Paints winning line</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePointFor     </span><span style="color: #357B42; font-style: italic">; HL = winning address</span></span>
<span class="line"><span style="color: #002339">ld   de, TitlePointName    </span><span style="color: #357B42; font-style: italic">; HL = address winning name</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg                 </span><span style="color: #357B42; font-style: italic">; Compose and paint winner</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_p1         </span><span style="color: #357B42; font-style: italic">; HL = address points player 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player that moved</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_win           </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = address points player 2</span></span>
<span class="line"><span style="color: #7EB233">loop_win:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Increases score</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundWinGame      </span><span style="color: #357B42; font-style: italic">; BC = sound address</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #002339">jr   loop_reset            </span><span style="color: #357B42; font-style: italic">; Jump</span></span>
<span class="line"><span style="color: #7EB233">loop_tie:</span></span>
<span class="line"><span style="color: #002339">ld   hl, MoveCounter       </span><span style="color: #357B42; font-style: italic">; HL = move counter address</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Increments counter</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$09</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 9</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter = 9?</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_cont         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_tie        </span><span style="color: #357B42; font-style: italic">; HL = address points tables</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Increases score</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleTie          </span><span style="color: #357B42; font-style: italic">; HL = address tables</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg              </span><span style="color: #357B42; font-style: italic">; Paint tables</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundTie          </span><span style="color: #357B42; font-style: italic">; BC = sound direction</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #7EB233">loop_reset:</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENDATA-</span><span style="color: #174781">$04</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; BC = data length clean data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ResetValues           </span><span style="color: #357B42; font-style: italic">; Clear data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBoard            </span><span style="color: #357B42; font-style: italic">; Paint board</span></span>
<span class="line"><span style="color: #7EB233">loop_cont:</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player who moved</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = next player</span></span>
<span class="line"><span style="color: #002339">ld   (PlayerMoves), a      </span><span style="color: #357B42; font-style: italic">; Refresh in memory</span></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">Loop</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Main loop</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">include &quot;game.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;rom.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;screen.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;sprite.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;sound.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;var.asm&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end  Main</span></span></code></pre></div>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">We have a large part of the game developed and working. We can play our first games with friends and/or family and we have sound.</p>



<p class="has-medium-font-size wp-block-paragraph">In the next chapter of ZX Spectrum Assembly, we will implement the different options of the game and the end of the game.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/283e38v7" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x03-sound/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4521</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x02 Scoreboards and a two player game</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Tue, 03 Oct 2023 05:06:53 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac_toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4510</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;will be to implement the scoreboards and the two-player game, which will form a large]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;will be to implement the scoreboards and the two-player game, which will form a large part of the programme.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#information">Information</a>
</li>
<li><a href="#twoplayer-game">Two-player game</a>

<ul>
<li><a href="#resetting-the-board">Resetting the board</a>
</li>
<li><a href="#information-messages">Information messages</a>
</li>
<li><a href="#table-check">Table check</a>
</li>
<li><a href="#tictactoe-check">Tic-tac-toe check</a>
</li>
<li><a href="#scoreboard-update">Scoreboard update</a>
</li>
</ul>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul></div>


<h2 id="information" class="wp-block-heading has-large-font-size">Information</h2>



<p class="has-medium-font-size wp-block-paragraph">First we add the name of the programme, the players, the markers and the moving piece.</p>



<p class="has-medium-font-size wp-block-paragraph">In var.asm we add the necessary data.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="LENNAME: equ $0c           ; Length name players

; Information
Info:              db $10, $07, $16, $00, $03, &quot;Three in a row at &quot;
info_points:       db &quot;5 point&quot;
info_gt1:          db &quot;s&quot;

player1_title:     db $10, $05, $16, $02, $00
player1_name:      defs LENNAME, &quot; &quot;
player1_figure:    db $16, $04, $00, $90, $91, $0d, $91, $90

player_tie:        db $10, $02, $16, $02, $0d, &quot;Tables&quot;

player2_title:     db $10, $06, $16, $02, $14
player2_name:      defs LENNAME, &quot; &quot;
player2_figure:    db $16, $04, $1b, $92, $93, $16, $05, $1b
                   db $94, $95, $ff

; Titles
TitleTurn:         db $16, $15, $05, $13, $01
                   db &quot;Shift for &quot;
TitleTurn_name:    defs LENNAME, &quot; &quot;
                   db $ff
TitleError:        db $10, $02, $16, $15, $0a, $12, $01, $13, $01
                   defm &quot;Box occupied&quot;, $ff
TitleEspamatica:   defm &quot;Espamatica 2019&quot;, $ff
TitleGameOver:     db $10, $07, $16, $15, $03, $12, $01, $13, $01
                   defm &quot;Game terminated. Other?&quot;, $ff
TitleLostMovement: db $10, $02, $12, $01, $13, $01, $16, $15, $05
                   defm &quot;Lose turn &quot;
TitleLostMov_name: defs LENNAME, &quot; &quot;
                   db $ff
TitleOptionStart:  defm &quot;0. Start&quot;, $ff
TitleOptionPlayer: defm &quot;1. Players&quot;, $ff
TitleOptionPoint:  defm &quot;2. Points&quot;, $ff
TitleOptionTime:   defm &quot;3. Time&quot;, $ff
TitlePlayerName:   defm &quot;Player &quot;
TitlePlayerNumber: db &quot; : &quot;, $ff
TitlePointFor:     db $10, $07, $16, $15, $05, $12, $01, $13, $01
                   defm &quot;Point for &quot;
TitlePointName:    defs LENNAME, &quot; &quot;
                   db $ff
TitleTie:          db $10, $07, $16, $15, $0d, $12, $01, $13, $01
                   defm &quot;Tables&quot;, $ff

; -------------------------------------------------------------------
; Development of the game.
; -------------------------------------------------------------------
; Board squares. One byte per square, from 1 to 9.
; Bit 0, square occupied by player 1.
; Bit 4, square occupied by player 2.
Grid:          db $00, $00, $00, $00, $00, $00, $00, $00, $00
; Positions of the pieces on the board, 2 bytes per piece X, Y
GridPos:       db POS1_LEFT, POS1_TOP ; 1
               db POS2_LEFT, POS1_TOP ; 2
               db POS3_LEFT, POS1_TOP ; 3
               db POS1_LEFT, POS2_TOP ; 4
               db POS2_LEFT, POS2_TOP ; 5
               db POS3_LEFT, POS2_TOP ; 6
               db POS1_LEFT, POS3_TOP ; 7
               db POS2_LEFT, POS3_TOP ; 8
               db POS3_LEFT, POS3_TOP ; 9

Points_p1:     db $00            ; Points player 1
Points_p2:     db $00            ; Points player 2
Points_tie:    db $00            ; Points tables
PlayerMoves:   db $00            ; Player who moves

MaxPlayers:    db $01            ; Maximum players
MaxPoints:     db $05            ; Maximum Points
MaxSeconds:    db $10            ; Maximum seconds

Name_p1:       db &quot;Amstrad CPC &quot; ; Player name 1
Name_p2:       db &quot;ZX Spectrum &quot; ; Player name 2
Name_p2Default:db &quot;ZX Spectrum &quot; ; Default name" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">LENNAME:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$0c</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; Length name players</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Information</span></span>
<span class="line"><span style="color: #7EB233">Info:</span><span style="color: #002339">              </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339">, &quot;Three </span><span style="color: #7B30D0">in</span><span style="color: #002339"> a row at &quot;</span></span>
<span class="line"><span style="color: #7EB233">info_points:</span><span style="color: #002339">       </span><span style="color: #0991B6">db</span><span style="color: #002339"> &quot;</span><span style="color: #174781">5</span><span style="color: #002339"> point&quot;</span></span>
<span class="line"><span style="color: #7EB233">info_gt1:</span><span style="color: #002339">          </span><span style="color: #0991B6">db</span><span style="color: #002339"> &quot;s&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">player1_title:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">player1_name:</span><span style="color: #002339">      defs LENNAME, &quot; &quot;</span></span>
<span class="line"><span style="color: #7EB233">player1_figure:</span><span style="color: #002339">    </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$90</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$90</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">player_tie:</span><span style="color: #002339">        </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, &quot;Tables&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">player2_title:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$14</span></span>
<span class="line"><span style="color: #7EB233">player2_name:</span><span style="color: #002339">      defs LENNAME, &quot; &quot;</span></span>
<span class="line"><span style="color: #7EB233">player2_figure:</span><span style="color: #002339">    </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$95</span><span style="color: #002339">, $ff</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Titles</span></span>
<span class="line"><span style="color: #7EB233">TitleTurn:</span><span style="color: #002339">         </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> &quot;Shift for &quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleTurn_name:</span><span style="color: #002339">    defs LENNAME, &quot; &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleError:</span><span style="color: #002339">        </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Box occupied&quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleEspamatica:</span><span style="color: #002339">   defm &quot;Espamatica </span><span style="color: #174781">2019</span><span style="color: #002339">&quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleGameOver:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Game terminated. Other?&quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleLostMovement:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Lose turn &quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleLostMov_name:</span><span style="color: #002339"> defs LENNAME, &quot; &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionStart:</span><span style="color: #002339">  defm &quot;</span><span style="color: #174781">0.</span><span style="color: #002339"> Start&quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPlayer:</span><span style="color: #002339"> defm &quot;</span><span style="color: #174781">1.</span><span style="color: #002339"> Players&quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPoint:</span><span style="color: #002339">  defm &quot;</span><span style="color: #174781">2.</span><span style="color: #002339"> Points&quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionTime:</span><span style="color: #002339">   defm &quot;</span><span style="color: #174781">3.</span><span style="color: #002339"> Time&quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitlePlayerName:</span><span style="color: #002339">   defm &quot;Player &quot;</span></span>
<span class="line"><span style="color: #7EB233">TitlePlayerNumber:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> &quot; : &quot;, $ff</span></span>
<span class="line"><span style="color: #7EB233">TitlePointFor:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Point for &quot;</span></span>
<span class="line"><span style="color: #7EB233">TitlePointName:</span><span style="color: #002339">    defs LENNAME, &quot; &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> $ff</span></span>
<span class="line"><span style="color: #7EB233">TitleTie:</span><span style="color: #002339">          </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Tables&quot;, $ff</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Development of the game.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Board squares. One byte per square, from 1 to 9.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 0, square occupied by player 1.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 4, square occupied by player 2.</span></span>
<span class="line"><span style="color: #7EB233">Grid:</span><span style="color: #002339">          </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Positions of the pieces on the board, 2 bytes per piece X, Y</span></span>
<span class="line"><span style="color: #7EB233">GridPos:</span><span style="color: #002339">       </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 1</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 2</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 3</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 4</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 5</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 6</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 7</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 8</span></span>
<span class="line"><span style="color: #002339">               </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 9</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Points_p1:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Points player 1</span></span>
<span class="line"><span style="color: #7EB233">Points_p2:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Points player 2</span></span>
<span class="line"><span style="color: #7EB233">Points_tie:</span><span style="color: #002339">    </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Points tables</span></span>
<span class="line"><span style="color: #7EB233">PlayerMoves:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Player who moves</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">MaxPlayers:</span><span style="color: #002339">    </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$01</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Maximum players</span></span>
<span class="line"><span style="color: #7EB233">MaxPoints:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Maximum Points</span></span>
<span class="line"><span style="color: #7EB233">MaxSeconds:</span><span style="color: #002339">    </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Maximum seconds</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Name_p1:</span><span style="color: #002339">       </span><span style="color: #0991B6">db</span><span style="color: #002339"> &quot;Amstrad CPC &quot; </span><span style="color: #357B42; font-style: italic">; Player name 1</span></span>
<span class="line"><span style="color: #7EB233">Name_p2:</span><span style="color: #002339">       </span><span style="color: #0991B6">db</span><span style="color: #002339"> &quot;ZX Spectrum &quot; </span><span style="color: #357B42; font-style: italic">; Player name 2</span></span>
<span class="line"><span style="color: #7EB233">Name_p2Default:</span><span style="color: #0991B6">db</span><span style="color: #002339"> &quot;ZX Spectrum &quot; </span><span style="color: #357B42; font-style: italic">; Default name</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In the Info tag, we define the literals that will be displayed at the top of the screen. Although it may seem strange, the division into several labels is due to the fact that there are values that change depending on the choices made by the players:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Points needed to win the game.</li>



<li class="has-medium-font-size">If it is more than one point, it is written in the plural.</li>



<li class="has-medium-font-size">The names of the players.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">We also define the labels we will use to keep track of each player&#8217;s score and name.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to screen.asm and implement one routine to draw the information and another to draw the score.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the information of the game.
;
; Alters the value of HL.
; -------------------------------------------------------------------
PrintInfo:
ld   hl, Info              ; HL = information
jp   PrintString           ; Paints it" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the information of the game.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of HL.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintInfo:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Info              </span><span style="color: #357B42; font-style: italic">; HL = information</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintString           </span><span style="color: #357B42; font-style: italic">; Paints it</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintInfo paints the information to be displayed at the top of the screen. It does not assign ink colours or positions, as this is all defined in the Info tag.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the score.
;
; Alters the value of AF, BC, DE and HL.
; -------------------------------------------------------------------
PrintPoints:
ld   a, $05                ; A = cyan ink
call INK                   ; Change ink
ld   b, INI_TOP-$04        ; B = coord Y
ld   c, INI_LEFT-$03       ; C = coord X
call AT                    ; Position cursor
ld   hl, Points_p1         ; HL = points player 1
call PrintBCD              ; Paints them

ld   a, $02                ; A = red ink
call INK                   ; Change ink
ld   b, INI_TOP-$04        ; B = coord Y
ld   c, INI_LEFT-$0f       ; C = coord X
call AT                    ; Position cursor
ld   hl, Points_tie        ; HL = points tables
call PrintBCD              ; Paints them

ld   a, $06                ; A = yellow ink
call INK                   ; Change ink
ld   b, INI_TOP-$04        ; B = coord Y
ld   c, INI_LEFT-$1e       ; C = coord X
call AT                    ; Position cursor
ld   hl, Points_p2         ; HL = points player 2
jp   PrintBCD              ; Paints them in and out" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the score.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF, BC, DE and HL.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintPoints:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$05</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = cyan ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$04</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$03</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_p1         </span><span style="color: #357B42; font-style: italic">; HL = points player 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints them</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = red ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$04</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$0f</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_tie        </span><span style="color: #357B42; font-style: italic">; HL = points tables</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints them</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$06</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = yellow ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$04</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$1e</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_p2         </span><span style="color: #357B42; font-style: italic">; HL = points player 2</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints them in and out</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintPoints paints the players&#8217; scores and tables. For each score, it changes the ink, positions the cursor and paints the score.</p>



<p class="has-medium-font-size wp-block-paragraph">Now we have everything ready. We go to main.asm and after the call to PrintBoard we add the code that paints the information and the score.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Name_p1
ld   de, player1_name
ld   bc, LENNAME
ldir
ld   hl, Name_p2
ld   de, player2_name
ld   bc, LENNAME
ldir
call PrintInfo

xor  a
ld   hl, Points_p1
ld   de, Points_p1+$01
ld   bc, $03
ld   (hl), a
ldir
call PrintPoints" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Name_p1</span></span>
<span class="line"><span style="color: #002339">ld   de, player1_name</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME</span></span>
<span class="line"><span style="color: #002339">ldir</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2</span></span>
<span class="line"><span style="color: #002339">ld   de, player2_name</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME</span></span>
<span class="line"><span style="color: #002339">ldir</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintInfo</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  a</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_p1</span></span>
<span class="line"><span style="color: #002339">ld   de, Points_p1+</span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$03</span></span>
<span class="line"><span style="color: #002339">ld   (hl), a</span></span>
<span class="line"><span style="color: #002339">ldir</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintPoints</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The names of the players are in the Name_p1 and Name_p2 tags, so we need to pass them to the appropriate place in the information. We initialise the score and draw it.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa315c0a&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa315c0a" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="705" height="605" data-attachment-id="4511" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/tictactoe-s02-01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-01.png?fit=705%2C605&amp;ssl=1" data-orig-size="705,605" data-comments-opened="1" data-image-title="TicTacToe-S02-01" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-01.png?fit=705%2C605&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-01.png?resize=705%2C605&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4511" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-01.png?w=705&amp;ssl=1 705w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-01.png?resize=300%2C257&amp;ssl=1 300w" sizes="auto, (max-width: 705px) 100vw, 705px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="twoplayer-game" class="wp-block-heading has-large-font-size">Two-player game</h2>



<p class="has-medium-font-size wp-block-paragraph">We will implement the two-player game: controls, tile moves, checks for correctness, and tic-tac-toe performance.</p>



<p class="has-medium-font-size wp-block-paragraph">In var.asm we include a series of constants with ink colours, lines on which the three dashes occur, and key codes, which we will use together with the ROM routine to check the controls.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="INKWARNING: equ $C2        ; Ink Warnings
INKPLAYER1: equ $05        ; Player ink 1
INKPLAYER2: equ $06        ; Player 2 ink
INKTIE:     equ $07        ; Ink tables

KEYDEL:     equ $0c        ; Delete key
KEYENT:     equ $0d        ; Enter Key
KEYSPC:     equ $20        ; Space Key
KEY0:       equ $30        ; Key 0
KEY1:       equ $31        ; Key 1
KEY2:       equ $32        ; Key 2
KEY3:       equ $33        ; Key 3
KEY4:       equ $34        ; Key 4
KEY5:       equ $35        ; Key 5
KEY6:       equ $36        ; Key 6
KEY7:       equ $37        ; Key 7
KEY8:       equ $38        ; Key 8
KEY9:       equ $39        ; Key 9

WINNERLINE123: equ $01     ; Winning line 123
WINNERLINE456: equ $02     ; Winning line 456
WINNERLINE789: equ $03     ; Winning line 789
WINNERLINE147: equ $04     ; Winning line 147
WINNERLINE258: equ $05     ; Winning line 258
WINNERLINE369: equ $06     ; Winning line 369
WINNERLINE159: equ $07     ; Winning line 159
WINNERLINE357: equ $08     ; Winning line 357" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">INKWARNING:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> $C2        </span><span style="color: #357B42; font-style: italic">; Ink Warnings</span></span>
<span class="line"><span style="color: #7EB233">INKPLAYER1:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Player ink 1</span></span>
<span class="line"><span style="color: #7EB233">INKPLAYER2:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Player 2 ink</span></span>
<span class="line"><span style="color: #7EB233">INKTIE:</span><span style="color: #002339">     </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Ink tables</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">KEYDEL:</span><span style="color: #002339">     </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$0c</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Delete key</span></span>
<span class="line"><span style="color: #7EB233">KEYENT:</span><span style="color: #002339">     </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Enter Key</span></span>
<span class="line"><span style="color: #7EB233">KEYSPC:</span><span style="color: #002339">     </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Space Key</span></span>
<span class="line"><span style="color: #7EB233">KEY0:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$30</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 0</span></span>
<span class="line"><span style="color: #7EB233">KEY1:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$31</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 1</span></span>
<span class="line"><span style="color: #7EB233">KEY2:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$32</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 2</span></span>
<span class="line"><span style="color: #7EB233">KEY3:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$33</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 3</span></span>
<span class="line"><span style="color: #7EB233">KEY4:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$34</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 4</span></span>
<span class="line"><span style="color: #7EB233">KEY5:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$35</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 5</span></span>
<span class="line"><span style="color: #7EB233">KEY6:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$36</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 6</span></span>
<span class="line"><span style="color: #7EB233">KEY7:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$37</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 7</span></span>
<span class="line"><span style="color: #7EB233">KEY8:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$38</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 8</span></span>
<span class="line"><span style="color: #7EB233">KEY9:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$39</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key 9</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">WINNERLINE123:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$01</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 123</span></span>
<span class="line"><span style="color: #7EB233">WINNERLINE456:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$02</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 456</span></span>
<span class="line"><span style="color: #7EB233">WINNERLINE789:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 789</span></span>
<span class="line"><span style="color: #7EB233">WINNERLINE147:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$04</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 147</span></span>
<span class="line"><span style="color: #7EB233">WINNERLINE258:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 258</span></span>
<span class="line"><span style="color: #7EB233">WINNERLINE369:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 369</span></span>
<span class="line"><span style="color: #7EB233">WINNERLINE159:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 159</span></span>
<span class="line"><span style="color: #7EB233">WINNERLINE357:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$08</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Winning line 357</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We implement the logic in the file game.asm. We build it and add the include to main.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Wait for a key to be pressed on the board.
; During the game the interruptions are activated in order
; to perform the countdown. 
; With interrupts enabled, it does not update FLAGS_KEY/LAST_KEY.
;
; Output: C -&gt; Key pressed.
;
; Alters the value of the AF and BC registers.
; -------------------------------------------------------------------
WaitKeyBoard:
ld   a, $f7                ; A = half-row 1-5
in   a, ($fe)              ; Read half-row
cpl                        ; Invert bits, keys pressed to 1
and   $1F                  ; A = bits 1 to 4
jr   z, waitKey_cont       ; No key pressed, skips

; Evaluates the key pressed from 1 to 5.
ld   c, KEY1               ; C = key 1
ld   b, $05                ; B = keys to check
waitKey_1_5:
rra                        ; Pulsed key?
ret  c                     ; Pressed, jump
inc  c                     ; C = code next key
djnz waitKey_1_5           ; Loop for 5 keys

waitKey_cont:
ld   a, $ef                ; A = half-row 0-6
in   a, ($fe)              ; Read half-row
cpl                        ; Invert bits, keys pressed to 1
and  $1F                   ; A = bits 1 to 4
jr   z, waitKey_end        ; No key pressed, skip

; Evaluates the key pressed 9 to 6
rra                        ; Skip zero key
ld   c, KEY9               ; C = key 9
ld   b, $04                ; B = keys to check
waitKey_9_6:
rra                        ; Pulsed key?
ret  c                     ; Pressed, jump
dec  c                     ; C = next key code
djnz waitKey_9_6           ; Loop for 4 keys

; No key has been pressed
waitKey_end:
ld   c, KEY0
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Wait for a key to be pressed on the board.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; During the game the interruptions are activated in order</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; to perform the countdown. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; With interrupts enabled, it does not update FLAGS_KEY/LAST_KEY.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: C -&gt; Key pressed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF and BC registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">WaitKeyBoard:</span></span>
<span class="line"><span style="color: #002339">ld   a, $f7                </span><span style="color: #357B42; font-style: italic">; A = half-row 1-5</span></span>
<span class="line"><span style="color: #7B30D0">in</span><span style="color: #002339">   a, ($fe)              </span><span style="color: #357B42; font-style: italic">; Read half-row</span></span>
<span class="line"><span style="color: #002339">cpl                        </span><span style="color: #357B42; font-style: italic">; Invert bits, keys pressed to 1</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">   </span><span style="color: #174781">$1F</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; A = bits 1 to 4</span></span>
<span class="line"><span style="color: #002339">jr   z, waitKey_cont       </span><span style="color: #357B42; font-style: italic">; No key pressed, skips</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Evaluates the key pressed from 1 to 5.</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$05</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = keys to check</span></span>
<span class="line"><span style="color: #7EB233">waitKey_1_5:</span></span>
<span class="line"><span style="color: #002339">rra                        </span><span style="color: #357B42; font-style: italic">; Pulsed key?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; Pressed, jump</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = code next key</span></span>
<span class="line"><span style="color: #002339">djnz waitKey_1_5           </span><span style="color: #357B42; font-style: italic">; Loop for 5 keys</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">waitKey_cont:</span></span>
<span class="line"><span style="color: #002339">ld   a, $ef                </span><span style="color: #357B42; font-style: italic">; A = half-row 0-6</span></span>
<span class="line"><span style="color: #7B30D0">in</span><span style="color: #002339">   a, ($fe)              </span><span style="color: #357B42; font-style: italic">; Read half-row</span></span>
<span class="line"><span style="color: #002339">cpl                        </span><span style="color: #357B42; font-style: italic">; Invert bits, keys pressed to 1</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$1F</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = bits 1 to 4</span></span>
<span class="line"><span style="color: #002339">jr   z, waitKey_end        </span><span style="color: #357B42; font-style: italic">; No key pressed, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Evaluates the key pressed 9 to 6</span></span>
<span class="line"><span style="color: #002339">rra                        </span><span style="color: #357B42; font-style: italic">; Skip zero key</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$04</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = keys to check</span></span>
<span class="line"><span style="color: #7EB233">waitKey_9_6:</span></span>
<span class="line"><span style="color: #002339">rra                        </span><span style="color: #357B42; font-style: italic">; Pulsed key?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; Pressed, jump</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = next key code</span></span>
<span class="line"><span style="color: #002339">djnz waitKey_9_6           </span><span style="color: #357B42; font-style: italic">; Loop for 4 keys</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No key has been pressed</span></span>
<span class="line"><span style="color: #7EB233">waitKey_end:</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY0</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">With WaitKeyBoard we will check if any of the keys on the board, from one to nine, have been pressed. In this case we will not use the ROM routine, because later we will have the mode two interrupts active, and neither FLAGS_KEY nor LAST_KEY will be updated, and the ROM will not be able to tell us the last key pressed.</p>



<p class="has-medium-font-size wp-block-paragraph">The key check is very similar to the one we did in Space Battle, with the following variations:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">CPL: we invert the bits and evaluate if any keys were pressed with AND $1F; we skip if none were pressed.</li>



<li class="has-medium-font-size">What we return, in this case in C, is the ASCII code of the key pressed; of the zero key if none was pressed.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">We can also see that, before checking the keys 9 to 6, we make a rotation to ignore the 0 key.</p>



<p class="has-medium-font-size wp-block-paragraph">We now continue with the logic of the token movement.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Check and carry out the movement if it is correct.
;
; Input:  C  -&gt; Key pressed
; Output: Z  -&gt; Correct movement
;         NZ -&gt; Incorrect movement
;
; Alters the value of the AF and HL registers.
; -------------------------------------------------------------------
ToMove:
push bc                    ; Preserves BC
ld   hl, Grid              ; HL = grid address
ld   a, c                  ; A = C (key code)
sub  $30                   ; A = numeric value key
dec  a                     ; A-=1, so that it does not add up 
                           ; to one more
ld   b, $00                ; B = 0
ld   c, a                  ; C = A, BC = offset
add  hl, bc                ; HL = key box address
pop  bc                    ; Retrieves BC

ld   a, (hl)               ; A = box value
or   a                     ; Are you free?
ret  nz                    ; Busy, exits

ld   a, (PlayerMoves)      ; A = player moving
or   a                     ; Player 1?
jr   nz, toMove_p2         ; Player 2, jump
set  $00, (hl)             ; Activate box player 1
jr   toMove_end            ; Skip

toMove_p2:
set  $04, (hl)             ; Activate player 2 box

toMove_end:
xor  a                     ; Puts flag Z
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Check and carry out the movement if it is correct.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input:  C  -&gt; Key pressed</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: Z  -&gt; Correct movement</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;         NZ -&gt; Incorrect movement</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">ToMove:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserves BC</span></span>
<span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = grid address</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; A = C (key code)</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = numeric value key</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A-=1, so that it does not add up </span></span>
<span class="line"><span style="color: #002339">                           </span><span style="color: #357B42; font-style: italic">; to one more</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = 0</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; C = A, BC = offset</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  hl, bc                </span><span style="color: #357B42; font-style: italic">; HL = key box address</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Retrieves BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = box value</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Are you free?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nz                    </span><span style="color: #357B42; font-style: italic">; Busy, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player moving</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   nz, toMove_p2         </span><span style="color: #357B42; font-style: italic">; Player 2, jump</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$00</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Activate box player 1</span></span>
<span class="line"><span style="color: #002339">jr   toMove_end            </span><span style="color: #357B42; font-style: italic">; Skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">toMove_p2:</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$04</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Activate player 2 box</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">toMove_end:</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; Puts flag Z</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">ToMove receives the code of the key pressed in C. We calculate the value by subtracting the ASCII code from zero, subtracting one so as not to add too much to the offset, and point to the cell corresponding to the key pressed. We check that the cell is not occupied, if it is not, we activate bits one or four of the cell according to the player who is moving. If the cell is occupied, it goes out with NZ, if not, it goes out with Z (XOR A).</p>



<p class="has-medium-font-size wp-block-paragraph">We need a routine that paints the tile in the right place; we implement this in screen.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the card.
;
; Input: C -&gt; Key pressed.
;
; Alters the value of the AF, BC and HL registers.
; -------------------------------------------------------------------
PrintOXO:
; Calculation of the card position
ld   a, c                  ; A = key
sub  $30                   ; A = key value
dec  a                     ; A-=1, so as not to over-add in offset
add  a, a                  ; A+=A, offset, two bytes per position
ld   b, $00                ; B = 0
ld   c, a                  ; C = A
ld   hl, GridPos           ; HL = address grid positions
add  hl, bc                ; HL+=BC, address coord X cell
ld   c, (hl)               ; C = coord X cell
inc  hl                    ; HL = address coord Y
ld   b, (hl)               ; B = coord Y cell
call AT                    ; Position cursor

; Calculation sheet
ld   a, (PlayerMoves)      ; A = player who moves
or   a                     ; Check player
jr   nz, printOXO_Y        ; Non-zero, skip

printOXO_X:
ld   a, INKPLAYER1         ; A = ink player 1
call INK                   ; Change ink
ld   a, $90                ; A = 1st sprite
rst  $10                   ; Paints it
ld   a, $91                ; A = 2nd sprite
rst  $10                   ; Paints it
dec  b                     ; B = bottom line
call AT                    ; Position cursor
ld   a, $91                ; A = 2nd sprite
rst  $10                   ; Paints it
ld   a, $90                ; A = 1st sprite
rst  $10                   ; Paints it
ret                        ; Exists

printOXO_Y:
ld   a, INKPLAYER2         ; A = ink player 2
call INK                   ; Change ink
ld   a, $92                ; A = 1st sprite
rst  $10                   ; Paints it
ld   a, $93                ; A = 2nd sprite
rst  $10                   ; Paints it
dec  b                     ; B = bottom line
call AT                    ; Position cursor
ld   a, $94                ; A = 3rd sprite
rst  $10                   ; Paints it
ld   a, $95                ; A = 4th sprite
rst  $10                   ; Paints it
ret " style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the card.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: C -&gt; Key pressed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintOXO:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Calculation of the card position</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; A = key</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = key value</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A-=1, so as not to over-add in offset</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, a                  </span><span style="color: #357B42; font-style: italic">; A+=A, offset, two bytes per position</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = 0</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; C = A</span></span>
<span class="line"><span style="color: #002339">ld   hl, GridPos           </span><span style="color: #357B42; font-style: italic">; HL = address grid positions</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  hl, bc                </span><span style="color: #357B42; font-style: italic">; HL+=BC, address coord X cell</span></span>
<span class="line"><span style="color: #002339">ld   c, (hl)               </span><span style="color: #357B42; font-style: italic">; C = coord X cell</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = address coord Y</span></span>
<span class="line"><span style="color: #002339">ld   b, (hl)               </span><span style="color: #357B42; font-style: italic">; B = coord Y cell</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Calculation sheet</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player who moves</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Check player</span></span>
<span class="line"><span style="color: #002339">jr   nz, printOXO_Y        </span><span style="color: #357B42; font-style: italic">; Non-zero, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printOXO_X:</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER1         </span><span style="color: #357B42; font-style: italic">; A = ink player 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$90</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$91</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; B = bottom line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$91</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$90</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exists</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printOXO_Y:</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER2         </span><span style="color: #357B42; font-style: italic">; A = ink player 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$92</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; B = bottom line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 3rd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$95</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 4th sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339"> </span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In the first part of PrintOXO, the offset is calculated to obtain the coordinates where to paint the piece, the cursor is obtained and positioned. Then you get the moving player.</p>



<p class="has-medium-font-size wp-block-paragraph">The way to paint one tile or the other is almost identical: change the colour, paint the top part, position the cursor on the bottom line and paint the bottom part before leaving.</p>



<p class="has-medium-font-size wp-block-paragraph">Now we can start testing how everything we have implemented looks in the program. We go to main.asm and under the Loop tag, before JR Loop, we add the calls to the implemented routines.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call WaitKeyBoard
ld   a, c
cp   KEY0
jr   z, loop_cont
call ToMove
jr   nz, loop_cont
call PrintOXO
ld   a, (PlayerMoves)
xor  $01
ld   (PlayerMoves), a
loop_cont:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyBoard</span></span>
<span class="line"><span style="color: #002339">ld   a, c</span></span>
<span class="line"><span style="color: #002339">cp   KEY0</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_cont</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_cont</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintOXO</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">ld   (PlayerMoves), a</span></span>
<span class="line"><span style="color: #7EB233">loop_cont:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We wait for the player to press a key, and when he does, we check that the move is correct, and if it is, we paint the piece and change players.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa31700e&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa31700e" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="700" height="606" data-attachment-id="4513" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/tictactoe-s02-02/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-02.png?fit=700%2C606&amp;ssl=1" data-orig-size="700,606" data-comments-opened="1" data-image-title="TicTacToe-S02-02" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-02.png?fit=700%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-02.png?resize=700%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4513" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-02.png?w=700&amp;ssl=1 700w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-02.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 700px) 100vw, 700px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">We see how the tiles are painted and once the whole board is occupied we can only restart and reload, so we have to implement the following:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">When the board is full, reset it.</li>



<li class="has-medium-font-size">Display information messages.</li>



<li class="has-medium-font-size">Check if there is a tic-tac-toe or a board.</li>



<li class="has-medium-font-size">Refresh the scoreboard.</li>
</ul>



<h3 id="resetting-the-board" class="wp-block-heading has-large-font-size">Resetting the board</h3>



<p class="has-medium-font-size wp-block-paragraph">In var.asm, before PlayersMoves, we add a tag to keep track of the number of moves. The board is full when the value reaches nine.</p>



<p class="has-medium-font-size wp-block-paragraph">We put it first because PlayersMoves is only restarted at the start of a game. If we reset it with every point, player one would always move first.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="MoveCounter: db $00        ; Move counter" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">MoveCounter:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Move counter</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Also in var.asm, we add a constant at the beginning where we specify the length of data to initialise in each line item.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="LENDATA: equ $04           ; Length of data to be initialised" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">LENDATA:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$04</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; Length of data to be initialised</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">As we need to add more data to initialise, we change the value of this constant.</p>



<p class="has-medium-font-size wp-block-paragraph">In main.asm we locate the Loop tag and four lines before the line LD BC, $03. Replace this line with the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, LENDATA" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, LENDATA</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Compile, load into the emulator and check that everything still works.</p>



<p class="has-medium-font-size wp-block-paragraph">As we have mentioned, we will not always initialise all the data, so it is a good idea to implement a routine that can be passed the length of the data to be initialised, so that it can be called from different places with different values.</p>



<p class="has-medium-font-size wp-block-paragraph">The values we want to initialise are:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Points_p1</li>



<li class="has-medium-font-size">Points_p2</li>



<li class="has-medium-font-size">Points_tie</li>



<li class="has-medium-font-size">MoveCounter</li>



<li class="has-medium-font-size">PlayerMoves (only at the start of the game)</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">There are more values to initialise, but they are higher up, so either we initialise them in two parts, or we change the label. The label is Grid (each player&#8217;s moves) and we need to set all the values to zero at the start of each point. Let&#8217;s change the position of the label, just above Points_p1, like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Development of the game.
; -------------------------------------------------------------------
; Positions of the pieces on the board, 2 bytes per piece Y, X
GridPos:     db POS1_LEFT, POS1_TOP ; 1
             db POS2_LEFT, POS1_TOP ; 2
             db POS3_LEFT, POS1_TOP ; 3
             db POS1_LEFT, POS2_TOP ; 4
             db POS2_LEFT, POS2_TOP ; 5
             db POS3_LEFT, POS2_TOP ; 6
             db POS1_LEFT, POS3_TOP ; 7
             db POS2_LEFT, POS3_TOP ; 8
             db POS3_LEFT, POS3_TOP ; 9

; Board squares. One byte per square, from 1 to 9.
; Bit 0 to 1, square occupied by player 1.
; Bit 4 to 1, square occupied by player 2.
Grid:        db $00, $00, $00, $00, $00, $00, $00, $00, $00
Points_p1:   db $00        ; Points player 1
Points_p2:   db $00        ; Points player 2
Points_tie:  db $00        ; Points tables
MoveCounter: db $00        ; Move counter
PlayerMoves: db $00        ; Player moving" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Development of the game.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Positions of the pieces on the board, 2 bytes per piece Y, X</span></span>
<span class="line"><span style="color: #7EB233">GridPos:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 1</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 2</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 3</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 4</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 5</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 6</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 7</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 8</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 9</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Board squares. One byte per square, from 1 to 9.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 0 to 1, square occupied by player 1.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 4 to 1, square occupied by player 2.</span></span>
<span class="line"><span style="color: #7EB233">Grid:</span><span style="color: #002339">        </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">Points_p1:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Points player 1</span></span>
<span class="line"><span style="color: #7EB233">Points_p2:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Points player 2</span></span>
<span class="line"><span style="color: #7EB233">Points_tie:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Points tables</span></span>
<span class="line"><span style="color: #7EB233">MoveCounter:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Move counter</span></span>
<span class="line"><span style="color: #7EB233">PlayerMoves:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Player moving</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The value of LENDATA has to be changed to $0D.</p>



<p class="has-medium-font-size wp-block-paragraph">We implement the initialisation routine in game.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Initialises the values of the item/item.
;
; Input: BC -&gt; Length of the values to be initialised.
;
; Alters the value of the BC, DE and HL registers.
; -------------------------------------------------------------------
ResetValues:
ld   hl, Grid              ; HL = Grid address
ld   de, Grid+$01          ; DE = address of Grid+1
ld   (hl), $00             ; Resets first position to zero
ldir                       ; Resets the remainder (BC) to zero

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Initialises the values of the item/item.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: BC -&gt; Length of the values to be initialised.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the BC, DE and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">ResetValues:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = Grid address</span></span>
<span class="line"><span style="color: #002339">ld   de, Grid+</span><span style="color: #174781">$01</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; DE = address of Grid+1</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$00</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Resets first position to zero</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Resets the remainder (BC) to zero</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">ResetValues gets the number of bytes to clear in BC (must be one less than the total length, that&#8217;s why LENDATA is $0D instead of $0E), points HL and DE to the first and second grid positions, clears the first and then the rest.</p>



<p class="has-medium-font-size wp-block-paragraph">If you look closely, this routine should look very familiar, take a look at the CLS routine in screen.asm. They are almost identical!</p>



<p class="has-medium-font-size wp-block-paragraph">We could implement a routine with the lines LD (HL), $00, LDIR and RET, bearing in mind that before calling it we would have to load the necessary values in HL and BC, and remove CLS and ResetValues. I&#8217;ll leave it up to you, I&#8217;m going to leave it as it is now.</p>



<p class="has-medium-font-size wp-block-paragraph">We go back to main.asm, find Loop and a few lines above XOR A. From XOR A to CALL PrintPoint, we keep only the lines LD BC, LENDATA and CALL PrintPoints, the rest we delete.</p>



<p class="has-medium-font-size wp-block-paragraph">Between LD BC, LENDATA and CALL PrintPoints, we add the call to initialise the values, as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintInfo

ld   bc, LENDATA
call ResetValues
call PrintPoints

Loop:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintInfo</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   bc, LENDATA</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ResetValues</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintPoints</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">Loop</span><span style="color: #002339">:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">After loading the length of data to be cleaned into BC, we call the initialisation of the values and draw the points.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see that everything still works.</p>



<p class="has-medium-font-size wp-block-paragraph">To know if the board is full, we need to update MoveCounter with every move and check if it has reached nine.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate loop_cont and add the following lines just above it, after LD (PlayerMoves), A:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, MoveCounter
inc  (hl)
ld   a, $09
cp   (hl)
jr   nz, loop_cont
ld   bc, LENDATA-$01
call ResetValues
call PrintBoard" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, MoveCounter</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$09</span></span>
<span class="line"><span style="color: #002339">cp   (hl)</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_cont</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENDATA-</span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ResetValues</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBoard</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We increment the move counter, check if it has reached nine, in which case we load into BC the length of the data to be erased minus one (so as not to erase the moving player), initialise the data and draw the board empty.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile and load into the emulator. If we press the keys from one to nine until the board is full, the player whose turn it is to move has not been cleared and will move the player whose turn it was in the next turn.</p>



<h3 id="information-messages" class="wp-block-heading has-large-font-size">Information messages</h3>



<p class="has-medium-font-size wp-block-paragraph">During the course of the game there are several messages that need to be displayed to the players: if the move is wrong, which player is moving, who gets the point, if there is a draw and when the game is over.</p>



<p class="has-medium-font-size wp-block-paragraph">The messages have been defined at the beginning of the chapter, and in each of them the ink, brightness, flicker and location, so all we need to do is call PrintString with the address of the message in HL.</p>



<p class="has-medium-font-size wp-block-paragraph">There are two reasons why this cannot be done:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">The messages Turn to, Missed turn and Point to are not complete, they are completed with the name of the player who has the turn.</li>



<li class="has-medium-font-size">Before writing a message, the line must be cleared.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">We will implement two routines, one in game.asm and the other in screen.asm; these routines will act as a bridge when we call PrintString.We will start with the routine we have implemented in screen.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the messages
;
; Input: HL-&gt; message address
; -------------------------------------------------------------------
PrintMsg:
ld   b, INI_TOP-$15        ; B = coord Y line to be deleted (inverted)
call CLL                   ; Deletes the line
jp   PrintString           ; Paint the message" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the messages</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: HL-&gt; message address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintMsg:</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$15</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y line to be deleted (inverted)</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLL                   </span><span style="color: #357B42; font-style: italic">; Deletes the line</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintString           </span><span style="color: #357B42; font-style: italic">; Paint the message</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintMsg receives the address of the message in HL, clears the message line and jumps to paint the message, where it exits. Clearing the line before painting a new message is the only use of this routine.</p>



<p class="has-medium-font-size wp-block-paragraph">We implement the game.asm routine, which completes the three messages that paint the player&#8217;s name.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Completes the messages in which the player's name is displayed.
;
; Input: HL -&gt; message address
;        DE -&gt; address where the name should go
;
; Alters the value of the AF, BC and DE registers.
; -------------------------------------------------------------------
DoMsg:
push hl                    ; Preserves HL
ld   hl, player1_name      ; HL = name player 1
ld   a, (PlayerMoves)      ; A = player
or   a                     ; Player 1?
ld   a, INKPLAYER1         ; A = ink player 1
jr   z, doMsg_cont         ; Player 1, skip
ld   hl, player2_name      ; HL = name player 2
ld   a, INKPLAYER2         ; A = ink player 2
doMsg_cont:
call INK                   ; Change ink
ld   bc, LENNAME           ; BC = length name
ldir                       ; Pass player name to message
pop  hl                    ; Retrieve HL
jp   PrintMsg              ; Paint message" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Completes the messages in which the player&#39;s name is displayed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: HL -&gt; message address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;        DE -&gt; address where the name should go</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and DE registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">DoMsg:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl                    </span><span style="color: #357B42; font-style: italic">; Preserves HL</span></span>
<span class="line"><span style="color: #002339">ld   hl, player1_name      </span><span style="color: #357B42; font-style: italic">; HL = name player 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER1         </span><span style="color: #357B42; font-style: italic">; A = ink player 1</span></span>
<span class="line"><span style="color: #002339">jr   z, doMsg_cont         </span><span style="color: #357B42; font-style: italic">; Player 1, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, player2_name      </span><span style="color: #357B42; font-style: italic">; HL = name player 2</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER2         </span><span style="color: #357B42; font-style: italic">; A = ink player 2</span></span>
<span class="line"><span style="color: #7EB233">doMsg_cont:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME           </span><span style="color: #357B42; font-style: italic">; BC = length name</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Pass player name to message</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; Retrieve HL</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintMsg              </span><span style="color: #357B42; font-style: italic">; Paint message</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">DoMsg receives the address of the message to be painted in HL and the address where the player&#8217;s name is to be placed in DE. He checks which player&#8217;s turn it is, places one or the other name and the player&#8217;s ink. He paints the message and leaves.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to main.asm and paint all the messages we can, starting with who has the turn.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate Loop and add the following lines just below it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   b, $19
loop_wait:
halt
djnz loop_wait
ld   hl, TitleTurn
ld   de, TitleTurn_name
call DoMsg
loop_key:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$19</span></span>
<span class="line"><span style="color: #7EB233">loop_wait:</span></span>
<span class="line"><span style="color: #002339">halt</span></span>
<span class="line"><span style="color: #002339">djnz loop_wait</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleTurn</span></span>
<span class="line"><span style="color: #002339">ld   de, TitleTurn_name</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg</span></span>
<span class="line"><span style="color: #7EB233">loop_key:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Each time we go through the loop, we paint the player whose turn it is to move. Before we do this, we pause for about half a second to allow time to read the messages.</p>



<p class="has-medium-font-size wp-block-paragraph">Now find the line CP KEY0 and just below it in the line JR Z, loop_cont, change loop_cont to loop_key.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile, load it into the emulator and you will see that we can see which player has the turn to move.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa319450&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa319450" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="700" height="606" data-attachment-id="4517" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/tictactoe-s02-03/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-03.png?fit=700%2C606&amp;ssl=1" data-orig-size="700,606" data-comments-opened="1" data-image-title="TicTacToe-S02-03" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-03.png?fit=700%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-03.png?resize=700%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4517" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-03.png?w=700&amp;ssl=1 700w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-03.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 700px) 100vw, 700px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Let&#8217;s also draw the busy box error message. Find the CALL ToMove line, delete the following line, JR NZ, loop_cont, and add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   z, loop_print
ld   hl, TitleError
call PrintMsg
jr   Loop
loop_print:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   z, loop_print</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleError</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg</span></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">Loop</span></span>
<span class="line"><span style="color: #7EB233">loop_print:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If it returns from the ToMove routine with the Z flag active, this means that the movement is correct and we jump to loop_print, the label we added before CALL PrintOXO. If not, the move is incorrect and we print the error message and go back to the start of the loop.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile, load into the emulator and try to move to an occupied square. The error message should be painted, even if it&#8217;s only visible for a short time; don&#8217;t worry, we&#8217;ll remove the pause at the beginning of the loop later and use the sound effects for timing.</p>



<h3 id="table-check" class="wp-block-heading has-large-font-size">Table check</h3>



<p class="has-medium-font-size wp-block-paragraph">The draw check is simple; if there are nine moves and no three in a row, there is a draw.</p>



<p class="has-medium-font-size wp-block-paragraph">Find the loop_cont tag and four lines above it, between JR NZ, loop_cont and LD BC, LENDATA-$01, add the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Points_tie
inc  (hl)
ld   hl, TitleTie
call PrintMsg" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Points_tie</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleTie</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">With these lines, if nine moves are reached without three in a row, one move is added to the draw score.</p>



<p class="has-medium-font-size wp-block-paragraph">Go to the loop_key tag and add the following line above it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintPoints" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintPoints</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Now, in each iteration of the loop, we draw the notes. The CALL PrintPoints just above the Loop tag can be removed.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile, load in the emulator and you will see that not everything works as it should, the table marker is not updated.</p>



<p class="has-medium-font-size wp-block-paragraph">Actually everything works as it should, the problem is that ResetValues is resetting the scores, which is easy to fix, but we will fix it later.</p>



<p class="has-medium-font-size wp-block-paragraph">We also notice that when repainting the board the flashing is active because the message Tables leaves it that way.</p>



<p class="has-medium-font-size wp-block-paragraph">To solve this we could implement a routine to remove the flickering, but we decided to solve it in the board definition. We go to var.asm and just below the Board_1 tag we add the following line to disable the flickering and glowing when the board is painted.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="db $12, $00, $13, $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Compile, load into the emulator, move until the board is full and check that there is no flickering when repainting.</p>



<h3 id="tictactoe-check" class="wp-block-heading has-large-font-size">Tic-tac-toe check</h3>



<p class="has-medium-font-size wp-block-paragraph">The three in a row check should not be made until at least three moves have been made. In principle, there should be at least five moves before there are three in a row, but since we are going to implement the possibility of the player losing the turn if it takes longer than the set time, it is possible that there will be three in a row with three moves. However, we are going to perform the operation each time a piece is moved, so it will always take the same (approximate) time for each iteration of the loop.</p>



<p class="has-medium-font-size wp-block-paragraph">In game.asm we add the routine that checks for tic-tac-toe.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Check if there are three in a row.
;
; Return: A -&gt; tic-tac-toe line
;         Z if there are three in a row, NZ otherwise.   
; 
; Alters the value of the AF, B and IX registers.
; -------------------------------------------------------------------
CheckWinner:
ld   ix, Grid-$01          ; IX = grid address - 1
ld   b, $03                ; B = sum cells player 1
ld   a, (PlayerMoves)      ; A = player
or   a                     ; Player 1?
jr   z, CheckWinner_check  ; Player 1, skip
ld   b, $30                ; B = sum cells player 2

CheckWinner_check:
ld   a, (ix+1)             ; A = cell 1
add  a, (ix+2)             ; A+= cell 2
add  a, (ix+3)             ; A+= cell 3
cp   b                     ; Three in a row?
ld   a, WINNERLINE123      ; A = flag line 123
ret  z                     ; Tic-tac-toe, exits

ld   a, (ix+4)             ; A = cell 4
add  a, (ix+5)             ; A+= cell 5
add  a, (ix+6)             ; A+= cell 6
cp   b                     ; Three in a row?
ld   a, WINNERLINE456      ; A = indicator line 456
ret  z                     ; Tic-tac-toe, exits

ld   a, (ix+7)             ; A = cell 7
add  a, (ix+8)             ; A+= cell 8
add  a, (ix+9)             ; A+= cell 9
cp   b                     ; Three in a row?
ld   a, WINNERLINE789      ; A = line indicator line 789
ret  z                     ; Tic-tac-toe, exits

ld   a, (ix+1)             ; A = cell 1
add  a, (ix+4)             ; A+= cell 4
add  a, (ix+7)             ; A+= cell 7
cp   b                     ; Three in a row?
ld   a, WINNERLINE147      ; A = indicator line 147
ret  z                     ; Tic-tac-toe, exits

ld   a, (ix+2)             ; A = cell 2
add  a, (ix+5)             ; A+= cell 5
add  a, (ix+8)             ; A+= cell 8
cp   b                     ; Three in a row?
ld   a, WINNERLINE258      ; A = indicator line 258
ret  z                     ; Tic-tac-toe, exits

ld   a, (ix+3)             ; A = cell 3
add  a, (ix+6)             ; A+= cell 6
add  a, (ix+9)             ; A+= cell 9
cp   b                     ; Three in a row?
ld   a, WINNERLINE369      ; A = indicator line 369
ret  z                     ; Tic-tac-toe, exits

ld   a, (ix+1)             ; A = cell 1
add  a, (ix+5)             ; A+= cell 5
add  a, (ix+9)             ; A+= cell 9
cp   b                     ; Three in a row?
ld   a, WINNERLINE159      ; A = indicator line 159
ret  z                     ; Tic-tac-toe, exits

ld   a, (ix+3)             ; A = cell 3
add  a, (ix+5)             ; A+= cell 5
add  a, (ix+7)             ; A+= cell 7
cp   b                     ; Three in a row?
ld   a, WINNERLINE357      ; A = line indicator 357
ret                        ; Last condition, always comes out" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Check if there are three in a row.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Return: A -&gt; tic-tac-toe line</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;         Z if there are three in a row, NZ otherwise.   </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, B and IX registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">CheckWinner:</span></span>
<span class="line"><span style="color: #002339">ld   ix, Grid-</span><span style="color: #174781">$01</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; IX = grid address - 1</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$03</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = sum cells player 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, CheckWinner_check  </span><span style="color: #357B42; font-style: italic">; Player 1, skip</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$30</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = sum cells player 2</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">CheckWinner_check:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">1</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">2</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">3</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 3</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE123      </span><span style="color: #357B42; font-style: italic">; A = flag line 123</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Tic-tac-toe, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">4</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">5</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">6</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE456      </span><span style="color: #357B42; font-style: italic">; A = indicator line 456</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Tic-tac-toe, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">7</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">8</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">9</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE789      </span><span style="color: #357B42; font-style: italic">; A = line indicator line 789</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Tic-tac-toe, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">1</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">4</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">7</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE147      </span><span style="color: #357B42; font-style: italic">; A = indicator line 147</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Tic-tac-toe, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">2</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">5</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">8</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 8</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE258      </span><span style="color: #357B42; font-style: italic">; A = indicator line 258</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Tic-tac-toe, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">3</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">6</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">9</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE369      </span><span style="color: #357B42; font-style: italic">; A = indicator line 369</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Tic-tac-toe, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">1</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">5</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">9</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE159      </span><span style="color: #357B42; font-style: italic">; A = indicator line 159</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Tic-tac-toe, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">3</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">5</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">7</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Three in a row?</span></span>
<span class="line"><span style="color: #002339">ld   a, WINNERLINE357      </span><span style="color: #357B42; font-style: italic">; A = line indicator 357</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Last condition, always comes out</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In CheckWinner we point IX to the address above the grid, in B we load the value that the cells should add if one or the other player wins: three for player one and thirty for player two.</p>



<p class="has-medium-font-size wp-block-paragraph">Then we check the possible combinations of three in a row that exist and leave if there were any, with the winning combination in A and the Z flag activated. To check if there were three in a row, we add up the values of the cells in A and check with B.</p>



<p class="has-medium-font-size wp-block-paragraph">In the final check, if there are no three in a row, the Z flag is deactivated (NZ).</p>



<p class="has-medium-font-size wp-block-paragraph">In main.asm we will add the lines to do the three in a row check and to act on whether it is successful or not. We add the following lines just below CALL PrintOXO.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="loop_checkWinner:
call CheckWinner
jr   nz, loop_tie
ld   hl, TitlePointFor
ld   de, TitlePointName
call DoMsg
ld   hl, Points_p1
ld   a, (PlayerMoves)
or   a
jr   z, loop_win
inc  hl
loop_win:
inc  (hl)
jr   loop_reset 
loop_tie:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">loop_checkWinner:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CheckWinner</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_tie</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePointFor</span></span>
<span class="line"><span style="color: #002339">ld   de, TitlePointName</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_p1</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_win</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl</span></span>
<span class="line"><span style="color: #7EB233">loop_win:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)</span></span>
<span class="line"><span style="color: #002339">jr   loop_reset </span></span>
<span class="line"><span style="color: #7EB233">loop_tie:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We call the tic-tac-toe check and skip if there are no tic-tac-toes. If there are three in a row, we draw the point-to message, find out which player made it and increase their score.</p>



<p class="has-medium-font-size wp-block-paragraph">Just below loop_tie are these lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (PlayerMoves)
xor  $01
ld   (PlayerMoves), a" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (PlayerMoves)</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">ld   (PlayerMoves), a</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We remove it and put it under the loop_cont tag, otherwise the next point will be started by the same player who won the previous one.</p>



<p class="has-medium-font-size wp-block-paragraph">Finally, we add the loop_reset tag before LD BC, LENDATA-$01.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and everything seems to work, except for the bookmarks, which still don&#8217;t update.</p>



<p class="has-medium-font-size wp-block-paragraph">We will visually mark where the three in a row have occurred by drawing a diagonal across the three tiles. We implement this routine in screen.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Print the winning line.
;
; Entry: A -&gt; Winning line
;
; Alters the value of the AF, BC, DE and HL registers.
; -------------------------------------------------------------------
PrintWinnerLine:
ld   hl, COORDX            ; HL = coord X
ld   bc, $6c6c             ; BC = displacement
ld   de, $01ff             ; DE = orientation
cp   WINNERLINE159         ; Does line 159 win?
jr   z, printWinnerLine_159; Yes, skip
ld   e, $01                ; DE = orientation
cp   WINNERLINE357         ; Does line 357 win?
jr   z, printWinnerLine_357; Yes, skip

ld   c, $00                ; Displacement
cp   WINNERLINE147         ; Do you win line 147?
jr   z, printWinnerLine_147; Yes, skip
cp   WINNERLINE258         ; Does line 258 win?
jr   z, printWinnerLine_258; Yes, skip
cp   WINNERLINE369         ; Does line 369 win?
jr   z, printWinnerLine_369; Yes, skip

ld   bc, $006c             ; Displacement
ld   (hl), $48             ; Coord X
inc  hl                    ; Aim HL at coord Y
cp   WINNERLINE123         ; Do you win line 123?
jr   z, printWinnerLine_123; Yes, skip
cp   WINNERLINE456         ; Do you win line 456?
jr   z, printWinnerLine_456; Yes, skip
cp   WINNERLINE789         ; Does line 789 win?
jr   z, printWinnerLine_789; If so, jump to painting it.

printWinnerLine_159:
ld   (hl), $b7             ; Coord X
jr   printWinnerLine_Y

printWinnerLine_357:
ld   (hl), $48             ; Coord X
jr   printWinnerLine_Y

printWinnerLine_147:
ld   (hl), $58             ; Coord X
jr   printWinnerLine_Y

printWinnerLine_258:
ld   (hl), $80             ; Coord X
jr   printWinnerLine_Y

printWinnerLine_369:
ld  (hl), $a8              ; Coord X

printWinnerLine_Y:
inc  hl                    ; Aim HL at coord Y
ld   (hl), $10             ; Coord Y
jr   printWinnerLine_end   ; Paint the line

printWinnerLine_123:
ld   (hl), $70             ; Coord Y
jr   printWinnerLine_end   ; Paint the line

printWinnerLine_456:
ld   (hl), $47             ; Coord Y
jr   printWinnerLine_end   ; Paint the line

printWinnerLine_789:
ld   (hl), $20             ; Coord Y

printWinnerLine_end:
jp   DRAW                  ; Paint the line" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Print the winning line.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entry: A -&gt; Winning line</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC, DE and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintWinnerLine:</span></span>
<span class="line"><span style="color: #002339">ld   hl, COORDX            </span><span style="color: #357B42; font-style: italic">; HL = coord X</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$6c6c</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; BC = displacement</span></span>
<span class="line"><span style="color: #002339">ld   de, </span><span style="color: #174781">$01ff</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; DE = orientation</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE159         </span><span style="color: #357B42; font-style: italic">; Does line 159 win?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_159</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   e, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; DE = orientation</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE357         </span><span style="color: #357B42; font-style: italic">; Does line 357 win?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_357</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   c, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; Displacement</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE147         </span><span style="color: #357B42; font-style: italic">; Do you win line 147?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_147</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE258         </span><span style="color: #357B42; font-style: italic">; Does line 258 win?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_258</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE369         </span><span style="color: #357B42; font-style: italic">; Does line 369 win?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_369</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$006c</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Displacement</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$48</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord X</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; Aim HL at coord Y</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE123         </span><span style="color: #357B42; font-style: italic">; Do you win line 123?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_123</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE456         </span><span style="color: #357B42; font-style: italic">; Do you win line 456?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_456</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">cp   WINNERLINE789         </span><span style="color: #357B42; font-style: italic">; Does line 789 win?</span></span>
<span class="line"><span style="color: #002339">jr   z, printWinnerLine_789</span><span style="color: #357B42; font-style: italic">; If so, jump to painting it.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_159:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), $b7             </span><span style="color: #357B42; font-style: italic">; Coord X</span></span>
<span class="line"><span style="color: #002339">jr   printWinnerLine_Y</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_357:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$48</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord X</span></span>
<span class="line"><span style="color: #002339">jr   printWinnerLine_Y</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_147:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$58</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord X</span></span>
<span class="line"><span style="color: #002339">jr   printWinnerLine_Y</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_258:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$80</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord X</span></span>
<span class="line"><span style="color: #002339">jr   printWinnerLine_Y</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_369:</span></span>
<span class="line"><span style="color: #002339">ld  (hl), $a8              </span><span style="color: #357B42; font-style: italic">; Coord X</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_Y:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; Aim HL at coord Y</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$10</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord Y</span></span>
<span class="line"><span style="color: #002339">jr   printWinnerLine_end   </span><span style="color: #357B42; font-style: italic">; Paint the line</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_123:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$70</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord Y</span></span>
<span class="line"><span style="color: #002339">jr   printWinnerLine_end   </span><span style="color: #357B42; font-style: italic">; Paint the line</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_456:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$47</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord Y</span></span>
<span class="line"><span style="color: #002339">jr   printWinnerLine_end   </span><span style="color: #357B42; font-style: italic">; Paint the line</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_789:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$20</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Coord Y</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printWinnerLine_end:</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   DRAW                  </span><span style="color: #357B42; font-style: italic">; Paint the line</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintWinnerLine gets the winning line in A. First we evaluate which is the line to jump to one part of the routine or another. As the different lines to be drawn share common data, we only change the data that differs and call DRAW to draw the line and exit (see the DRAW comments in ROM.asm for more information).</p>



<p class="has-medium-font-size wp-block-paragraph">In main.asm, locate the loop_checkWinner tag and add the following line under JR NZ, loop_tie:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintWinnerLine" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintWinnerLine</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load in the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa31b077&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa31b077" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="703" height="607" data-attachment-id="4518" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/tictactoe-s02-04/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-04.png?fit=703%2C607&amp;ssl=1" data-orig-size="703,607" data-comments-opened="1" data-image-title="TicTacToe-S02-04" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-04.png?fit=703%2C607&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-04.png?resize=703%2C607&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4518" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-04.png?w=703&amp;ssl=1 703w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-04.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 703px) 100vw, 703px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">As we can see, a line is drawn by marking the three in a row, although it is so fast that we can hardly see it. In the next chapter we will use sound for timing.</p>



<p class="has-medium-font-size wp-block-paragraph">However, if you want to check that the line is drawn, you can put these two lines below CALL PrintWinnerLine:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="tmp:
jr   tmp" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">tmp:</span></span>
<span class="line"><span style="color: #002339">jr   tmp</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Don&#8217;t forget to remove them afterwards.</p>



<h3 id="scoreboard-update" class="wp-block-heading has-large-font-size">Scoreboard update</h3>



<p class="has-medium-font-size wp-block-paragraph">We are going to fix the long-standing bug that causes the scoreboard not to be updated.</p>



<p class="has-medium-font-size wp-block-paragraph">In var.asm we locate the Grid tag and see that below it are the Points_p1, Points_p2 and Point_tie tags. We move these three tags to just below MoveCounter.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Development of the game.
; -------------------------------------------------------------------
; Positions of the pieces on the board, 2 bytes per piece Y, X
GridPos:     db POS1_LEFT, POS1_TOP ; 1
             db POS2_LEFT, POS1_TOP ; 2
             db POS3_LEFT, POS1_TOP ; 3
             db POS1_LEFT, POS2_TOP ; 4
             db POS2_LEFT, POS2_TOP ; 5
             db POS3_LEFT, POS2_TOP ; 6
             db POS1_LEFT, POS3_TOP ; 7
             db POS2_LEFT, POS3_TOP ; 8
             db POS3_LEFT, POS3_TOP ; 9

; Board squares. One byte per square, from 1 to 9.
; Bit 0 to 1, square occupied by player 1.
; Bit 4 to 1, square occupied by player 2.
Grid:        db $00, $00, $00, $00, $00, $00, $00, $00, $00
MoveCounter: db $00        ; Move counter
Points_p1:   db $00        ; Points player 1
Points_p2:   db $00        ; Points player 2
Points_tie:  db $00        ; Points tables
PlayerMoves: db $00        ; Player moving" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Development of the game.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Positions of the pieces on the board, 2 bytes per piece Y, X</span></span>
<span class="line"><span style="color: #7EB233">GridPos:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 1</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 2</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS1_TOP </span><span style="color: #357B42; font-style: italic">; 3</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 4</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 5</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS2_TOP </span><span style="color: #357B42; font-style: italic">; 6</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS1_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 7</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS2_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 8</span></span>
<span class="line"><span style="color: #002339">             </span><span style="color: #0991B6">db</span><span style="color: #002339"> POS3_LEFT, POS3_TOP </span><span style="color: #357B42; font-style: italic">; 9</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Board squares. One byte per square, from 1 to 9.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 0 to 1, square occupied by player 1.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 4 to 1, square occupied by player 2.</span></span>
<span class="line"><span style="color: #7EB233">Grid:</span><span style="color: #002339">        </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">MoveCounter:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Move counter</span></span>
<span class="line"><span style="color: #7EB233">Points_p1:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Points player 1</span></span>
<span class="line"><span style="color: #7EB233">Points_p2:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Points player 2</span></span>
<span class="line"><span style="color: #7EB233">Points_tie:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Points tables</span></span>
<span class="line"><span style="color: #7EB233">PlayerMoves:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Player moving</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In main.asm, find the loop_reset tag, and on the bottom line, LD BC, LENDATA-$01, replace $01 with $04.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile, load into the emulator, and now the bookmarks are updated.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa31be00&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa31be00" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="701" height="605" data-attachment-id="4519" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/tictactoe-s02-05/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-05.png?fit=701%2C605&amp;ssl=1" data-orig-size="701,605" data-comments-opened="1" data-image-title="TicTacToe-S02-05" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-05.png?fit=701%2C605&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-05.png?resize=701%2C605&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4519" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-05.png?w=701&amp;ssl=1 701w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S02-05.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 701px) 100vw, 701px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">In the next chapter of ZX Spectrum Assembly, we will implement different sounds and use them for timing by pausing between events.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/2brbm95l" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x02-scoreboards/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4510</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x01 Game board</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Mon, 02 Oct 2023 05:23:33 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac_toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4497</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we begin the development of another game, in this case Tic-Tac-Toe. Introduction Tic-tac-toe was born]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we begin the development of another game, in this case Tic-Tac-Toe.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#introduction">Introduction</a>
</li>
<li><a href="#game-board">Game board</a>
</li>
<li><a href="#sprites">Sprites</a>
</li>
<li><a href="#rom-routines-and-variables">ROM routines and variables</a>
</li>
<li><a href="#variables">Variables</a>
</li>
<li><a href="#screen">Screen</a>
</li>
<li><a href="#main">Main</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul></div>


<h2 id="introduction" class="wp-block-heading has-large-font-size">Introduction</h2>



<p class="has-medium-font-size wp-block-paragraph">Tic-tac-toe was born from reading the book «ZX Spectrum Game Programming Club» by Gary Plowman. When we finish typing the Basic list in the book, he suggests adding the single player mode as an exercise.</p>



<p class="has-medium-font-size wp-block-paragraph">Having concluded that Basic was not my thing, I decided to develop the game from scratch in Assembly, with the following options:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">One and two player modes.</li>



<li class="has-medium-font-size">Possibility to specify the names of the players.</li>



<li class="has-medium-font-size">Points to be earned to finish the game.</li>



<li class="has-medium-font-size">Maximum time per move.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">In Space Battle we saw the use of the ROM routine for keyboard handling, but it is in Tic Tac Toe where it is used most intensively.</p>



<p class="has-medium-font-size wp-block-paragraph">We will also use interrupts to control the maximum time per move and give the user a countdown of the seconds left to move; he will lose his turn if he uses them up without having made the move. </p>



<p class="has-medium-font-size wp-block-paragraph">At this point you should already have a certain level, so we will not explain the instructions one by one; we will explain what the routine does as a whole, which together with the comments should be sufficient for your understanding.</p>



<h2 id="game-board" class="wp-block-heading has-large-font-size">Game board</h2>



<p class="has-medium-font-size wp-block-paragraph">The first thing we are going to do is to design and paint the game board, which consists of a grid of nine cells.</p>



<p class="has-medium-font-size wp-block-paragraph">We create the folder TicTacToe and the files:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Main.asm</li>



<li class="has-medium-font-size">Rom.asm</li>



<li class="has-medium-font-size">Screen.asm</li>



<li class="has-medium-font-size">Sprite.asm</li>



<li class="has-medium-font-size">Var.asm</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">This time I won&#8217;t create folders for each step, but if you prefer you can continue this way.</p>



<h2 id="sprites" class="wp-block-heading has-large-font-size">Sprites</h2>



<p class="has-medium-font-size wp-block-paragraph">We define the graphics in sprite.asm: the X for player one, the O for player two, the board crosshairs and the vertical and horizontal lines. As in Space Battle, we will use the UDGs.</p>



<p class="has-medium-font-size wp-block-paragraph">The appearance of the graphics in sprite.asm is as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; File: sprite.asm
;
; Definition of graphs.
; -------------------------------------------------------------------
; Sprite player 1
Sprite_P1:
db $c0, $e0, $70, $38, $1c, $0e, $07, $03 ; $90
db $03, $07, $0e, $1c, $38, $70, $e0, $c0 ; $91

; Sprite player 2
Sprite_P2:
db $03, $0f, $1c, $30, $60, $60, $c0, $c0 ; $92 Top/Left
db $c0, $f0, $38, $0c, $06, $06, $03, $03 ; $93 Up/right
db $c0, $c0, $60, $60, $30, $1c, $0f, $03 ; $94 Down/Left
db $03, $03, $06, $06, $0c, $38, $f0, $c0 ; $95 Down/Right

; Sprite crosshead
Sprite_CROSS:
db $18, $18, $18, $ff, $ff, $18, $18, $18 ; $96

; Sprite vertical line
Sprite_SLASH:
db $18, $18, $18, $18, $18, $18, $18, $18 ; $97

; Sprite horizontal line
Sprite_MINUS:
db $00, $00, $00, $ff, $ff, $00, $00, $00 ; $98" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; File: sprite.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Definition of graphs.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite player 1</span></span>
<span class="line"><span style="color: #7EB233">Sprite_P1:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> $c0, $e0, </span><span style="color: #174781">$70</span><span style="color: #002339">, </span><span style="color: #174781">$38</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $90</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$38</span><span style="color: #002339">, </span><span style="color: #174781">$70</span><span style="color: #002339">, $e0, $c0 </span><span style="color: #357B42; font-style: italic">; $91</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite player 2</span></span>
<span class="line"><span style="color: #7EB233">Sprite_P2:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$0f</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$30</span><span style="color: #002339">, </span><span style="color: #174781">$60</span><span style="color: #002339">, </span><span style="color: #174781">$60</span><span style="color: #002339">, $c0, $c0 </span><span style="color: #357B42; font-style: italic">; $92 Top/Left</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> $c0, $f0, </span><span style="color: #174781">$38</span><span style="color: #002339">, </span><span style="color: #174781">$0c</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $93 Up/right</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> $c0, $c0, </span><span style="color: #174781">$60</span><span style="color: #002339">, </span><span style="color: #174781">$60</span><span style="color: #002339">, </span><span style="color: #174781">$30</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$0f</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $94 Down/Left</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$0c</span><span style="color: #002339">, </span><span style="color: #174781">$38</span><span style="color: #002339">, $f0, $c0 </span><span style="color: #357B42; font-style: italic">; $95 Down/Right</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite crosshead</span></span>
<span class="line"><span style="color: #7EB233">Sprite_CROSS:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, $ff, $ff, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $96</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite vertical line</span></span>
<span class="line"><span style="color: #7EB233">Sprite_SLASH:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $97</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite horizontal line</span></span>
<span class="line"><span style="color: #7EB233">Sprite_MINUS:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, $ff, $ff, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $98</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">With the practice you got in Space Battle, you should be able to draw the sprites on paper to see how they look.</p>



<p class="has-medium-font-size wp-block-paragraph">The next step is to draw the board. Constants are needed to reference the ROM, define positions, data to be painted and routines to paint it.</p>



<h2 id="rom-routines-and-variables" class="wp-block-heading has-large-font-size">ROM routines and variables</h2>



<p class="has-medium-font-size wp-block-paragraph">The ROM constants are defined in rom.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; File: rom.asm
;
; ROM routines and system variables.
; -------------------------------------------------------------------

; Permanent attributes of screen 2, main screen.
ATTR_S: equ $5c8d

; Current display attributes 2.
ATTR_T: equ $5c8f

; Edge attribute and screen 1. Used by BEEPER.
BORDCR: equ $5c48

; Address of starting coordinates X and Y (PLOT)
COORDX: equ $5c7d
COORDY: equ $5c7e

;--------------------------------------------------------------------
; Address where the keypad status flags are located when 
; the interruptions are not active.
;
; Bit 3 = 1 input in L mode, 0 input in K mode.
; Bit 5 = 1 a key has been pressed, 0 has not been pressed.
; Bit 6 = 1 numeric character, 0 alphanumeric.
;--------------------------------------------------------------------
FLAGS_KEY: equ $5c3b

;--------------------------------------------------------------------
; Address where the last key was pressed
; when interrupts are not active.
;--------------------------------------------------------------------
LAST_KEY: equ $5c08

; Direction of user-defined graphics.
UDG: equ $5c7b

; Start address of the graphics area of the VideoRAM.
VIDEORAM: equ $4000

; Length of the graphics area of the VideoRAM.
VIDEORAM_L: equ $1800

; Start address of the VideoRAM attributes area.
VIDEOATT: equ $5800

; Length of the VideoRAM attribute area.
VIDEOATT_L: equ $0300

;--------------------------------------------------------------------
; ROM beeper routine.
;
; Input: HL -&gt; Note.
;        DE -&gt; Duration.
;
; Alters the value of the AF, BC, DE, HL and IX registers.
;--------------------------------------------------------------------
BEEPER: equ $03b5

;--------------------------------------------------------------------
; Draw a line from the COORDS coordinates.
;
; Input: B -&gt; Vertical displacement of the line.
;        C -&gt; Horizontal displacement of the line.
;        D -&gt; Vertical orientation of the line:
;               $01 = Up, $FF = Down.
;        E -&gt; Horizontal orientation of the line:
;               $01 = Left, $FF = Right.
;
; Alters the value of the AF, BC and HL registers.
;--------------------------------------------------------------------
DRAW: equ $24ba

;--------------------------------------------------------------------
; ROM AT routine. Position the cursor.
;
; Input: B -&gt; Y-coordinate.
;        C -&gt; X-coordinate.
;
; The top left corner of the screen is 24, 33.
; (0-21) (0-31)
;--------------------------------------------------------------------
LOCATE: equ $0dd9

;--------------------------------------------------------------------
; ROM routine that opens the output channel.
;
; Input: A -&gt; Channel (1 = lower display, 2 = upper display).
;--------------------------------------------------------------------
OPENCHAN: equ $1601" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; File: rom.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ROM routines and system variables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Permanent attributes of screen 2, main screen.</span></span>
<span class="line"><span style="color: #7EB233">ATTR_S:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c8d</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Current display attributes 2.</span></span>
<span class="line"><span style="color: #7EB233">ATTR_T:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c8f</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Edge attribute and screen 1. Used by BEEPER.</span></span>
<span class="line"><span style="color: #7EB233">BORDCR:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c48</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Address of starting coordinates X and Y (PLOT)</span></span>
<span class="line"><span style="color: #7EB233">COORDX:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c7d</span></span>
<span class="line"><span style="color: #7EB233">COORDY:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c7e</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Address where the keypad status flags are located when </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; the interruptions are not active.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 3 = 1 input in L mode, 0 input in K mode.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 5 = 1 a key has been pressed, 0 has not been pressed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bit 6 = 1 numeric character, 0 alphanumeric.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">FLAGS_KEY:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c3b</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Address where the last key was pressed</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; when interrupts are not active.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">LAST_KEY:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c08</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Direction of user-defined graphics.</span></span>
<span class="line"><span style="color: #7EB233">UDG:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c7b</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Start address of the graphics area of the VideoRAM.</span></span>
<span class="line"><span style="color: #7EB233">VIDEORAM:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$4000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Length of the graphics area of the VideoRAM.</span></span>
<span class="line"><span style="color: #7EB233">VIDEORAM_L:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$1800</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Start address of the VideoRAM attributes area.</span></span>
<span class="line"><span style="color: #7EB233">VIDEOATT:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5800</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Length of the VideoRAM attribute area.</span></span>
<span class="line"><span style="color: #7EB233">VIDEOATT_L:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$0300</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ROM beeper routine.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: HL -&gt; Note.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;        DE -&gt; Duration.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC, DE, HL and IX registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">BEEPER:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$03b5</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Draw a line from the COORDS coordinates.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: B -&gt; Vertical displacement of the line.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;        C -&gt; Horizontal displacement of the line.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;        D -&gt; Vertical orientation of the line:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               $01 = Up, $FF = Down.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;        E -&gt; Horizontal orientation of the line:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               $01 = Left, $FF = Right.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">DRAW:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$24ba</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ROM AT routine. Position the cursor.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: B -&gt; Y-coordinate.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;        C -&gt; X-coordinate.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; The top left corner of the screen is 24, 33.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; (0-21) (0-31)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">LOCATE:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$0dd9</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ROM routine that opens the output channel.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: A -&gt; Channel (1 = lower display, 2 = upper display).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;--------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">OPENCHAN:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$1601</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">As you will see, we have added variables and routines that we have not seen before. Don&#8217;t worry, we&#8217;ll see them as we go along.</p>



<h2 id="variables" class="wp-block-heading has-large-font-size">Variables</h2>



<p class="has-medium-font-size wp-block-paragraph">In var.asm we add the necessary data to paint the board.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; File: var.asm
;
; Variable and constant declarations.
; -------------------------------------------------------------------

; Board ink
INKBOARD: equ $04

; Position Y = 0 for LOCATE.
INI_TOP:  equ $18
; Position X = 0 for LOCATE.
INI_LEFT: equ $21
; Positions for painting OXO elements.
POS1_TOP: equ INI_TOP  - $07
POS2_TOP: equ POS1_TOP - $05
POS3_TOP: equ POS2_TOP - $05

POS1_LEFT: equ INI_LEFT  - $0a
POS2_LEFT: equ POS1_LEFT - $05
POS3_LEFT: equ POS2_LEFT - $05
; -------------------------------------------------------------------
; Graphics.
; -------------------------------------------------------------------
; Vertical lines of the board.
Board_1:		
db $20, $20, $20, $20, $97, $20, $20, $20, $20, $97, $20, $20, $20
db $20, $ff

; Horizontal lines of the board.
Board_2:		
db $98, $98, $98, $98, $96, $98, $98, $98, $98, $96, $98, $98, $98
db $98, $ff

; -------------------------------------------------------------------
; Parts of the screen.
; -------------------------------------------------------------------
; Guide numbers so that the player knows which key to press
; to place the tokens (O X O)
Board_Helper:
; Magenta ink
db $10, $03 
; AT,Y,X,number
db $16, $08, $0a, &quot;1&quot;, $16, $08, $10, &quot;2&quot;, $16, $08, $15, &quot;3&quot;
db $16, $0d, $0a, &quot;4&quot;, $16, $0d, $10, &quot;5&quot;, $16, $0d, $15, &quot;6&quot;
db $16, $12, $0a, &quot;7&quot;, $16, $12, $10, &quot;8&quot;, $16, $12, $15, &quot;9&quot;
db $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; File: var.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Variable and constant declarations.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Board ink</span></span>
<span class="line"><span style="color: #7EB233">INKBOARD:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$04</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Position Y = 0 for LOCATE.</span></span>
<span class="line"><span style="color: #7EB233">INI_TOP:</span><span style="color: #002339">  </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$18</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Position X = 0 for LOCATE.</span></span>
<span class="line"><span style="color: #7EB233">INI_LEFT:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$21</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Positions for painting OXO elements.</span></span>
<span class="line"><span style="color: #7EB233">POS1_TOP:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> INI_TOP  - </span><span style="color: #174781">$07</span></span>
<span class="line"><span style="color: #7EB233">POS2_TOP:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> POS1_TOP - </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #7EB233">POS3_TOP:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> POS2_TOP - </span><span style="color: #174781">$05</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">POS1_LEFT:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> INI_LEFT  - </span><span style="color: #174781">$0a</span></span>
<span class="line"><span style="color: #7EB233">POS2_LEFT:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> POS1_LEFT - </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #7EB233">POS3_LEFT:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> POS2_LEFT - </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Graphics.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Vertical lines of the board.</span></span>
<span class="line"><span style="color: #7EB233">Board_1:</span><span style="color: #002339">		</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$97</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$97</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">, $ff</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Horizontal lines of the board.</span></span>
<span class="line"><span style="color: #7EB233">Board_2:</span><span style="color: #002339">		</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span><span style="color: #002339">, </span><span style="color: #174781">$98</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$98</span><span style="color: #002339">, $ff</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Parts of the screen.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Guide numbers so that the player knows which key to press</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; to place the tokens (O X O)</span></span>
<span class="line"><span style="color: #7EB233">Board_Helper:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Magenta ink</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339"> </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; AT,Y,X,number</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, &quot;</span><span style="color: #174781">1</span><span style="color: #002339">&quot;, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, &quot;</span><span style="color: #174781">2</span><span style="color: #002339">&quot;, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, &quot;</span><span style="color: #174781">3</span><span style="color: #002339">&quot;</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, &quot;</span><span style="color: #174781">4</span><span style="color: #002339">&quot;, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, &quot;</span><span style="color: #174781">5</span><span style="color: #002339">&quot;, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, &quot;</span><span style="color: #174781">6</span><span style="color: #002339">&quot;</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, &quot;</span><span style="color: #174781">7</span><span style="color: #002339">&quot;, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, &quot;</span><span style="color: #174781">8</span><span style="color: #002339">&quot;, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$15</span><span style="color: #002339">, &quot;</span><span style="color: #174781">9</span><span style="color: #002339">&quot;</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> $ff</span></span></code></pre></div>



<h2 id="screen" class="wp-block-heading has-large-font-size">Screen</h2>



<p class="has-medium-font-size wp-block-paragraph">In screen.asm we will implement the necessary routines to draw the elements on the screen.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Position the cursor. The upper corner is at 24.33.
;
; Input: B -&gt; Y.
;        C -&gt; X.
; -------------------------------------------------------------------
AT:
push af
push bc
push de
push hl                    ; Preserve records

call LOCATE                ; Position cursor

pop  hl
pop  de
pop  bc
pop  af                    ; Retrieves records
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Position the cursor. The upper corner is at 24.33.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: B -&gt; Y.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;        C -&gt; X.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">AT:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> af</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl                    </span><span style="color: #357B42; font-style: italic">; Preserve records</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> LOCATE                </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  af                    </span><span style="color: #357B42; font-style: italic">; Retrieves records</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The AT routine receives the Y and X coordinates simultaneously in the B and C registers (they are inverted coordinates). It calls the ROM routine that positions the cursor.</p>



<p class="has-medium-font-size wp-block-paragraph">We will manage the different colour attributes in a differentiated way, with one routine for each attribute.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Change the border colour.
;
; Input: A -&gt; Colour for the border.
;
; Alters the value of the AF registers.
; -------------------------------------------------------------------
BORDER:
push bc                    ; Preserves BC
and  $07                   ; A = colour
out  ($fe), a              ; Change border colour

rlca
rlca
rlca                       ; Rotate three bits to the left to set
                           ; colour in paper/border bits
ld   b, a                  ; B = A
ld   a, (BORDCR)           ; A = system variable BORDCR
and  $c7                   ; Remove the paper/border bits
or   b                     ; Adds the paper/border 	colour

ld   (BORDCR), a           ; BORDCR = A, so that BEEPER does not change
pop  bc                    ; Retrieves BC

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Change the border colour.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: A -&gt; Colour for the border.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">BORDER:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserves BC</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$07</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = colour</span></span>
<span class="line"><span style="color: #7B30D0">out</span><span style="color: #002339">  ($fe), a              </span><span style="color: #357B42; font-style: italic">; Change border colour</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca                       </span><span style="color: #357B42; font-style: italic">; Rotate three bits to the left to set</span></span>
<span class="line"><span style="color: #002339">                           </span><span style="color: #357B42; font-style: italic">; colour in paper/border bits</span></span>
<span class="line"><span style="color: #002339">ld   b, a                  </span><span style="color: #357B42; font-style: italic">; B = A</span></span>
<span class="line"><span style="color: #002339">ld   a, (BORDCR)           </span><span style="color: #357B42; font-style: italic">; A = system variable BORDCR</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $c7                   </span><span style="color: #357B42; font-style: italic">; Remove the paper/border bits</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   b                     </span><span style="color: #357B42; font-style: italic">; Adds the paper/border 	colour</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   (BORDCR), a           </span><span style="color: #357B42; font-style: italic">; BORDCR = A, so that BEEPER does not change</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Retrieves BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The BORDER routine receives in A the colour to be assigned to the border. To make sure it is the right colour, we keep bits 0, 1 and 2. We change the border colour and then set the value in the system variable BORDCR so that BEEPER does not change it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Assigns the colour of the ink.
;
; Input: A -&gt; Ink colour.
;             FBPPPIII
;
; Alters the value of the AF registers.
; -------------------------------------------------------------------
INK:
push bc                    ; Preserves BC
and  $07                   ; A = INK
ld   b, a                  ; B = A

ld   a, (ATTR_T)           ; A = current attribute
and  $f8                   ; A = FLASH, BRIGHT and PAPER
or   b                     ; Add INK

ld   (ATTR_T), a           ; Update current attribute
ld   (ATTR_S), a           ; Update permanent attribute
pop  bc                    ; Retrieves BC

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Assigns the colour of the ink.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: A -&gt; Ink colour.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;             FBPPPIII</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">INK:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserves BC</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$07</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = INK</span></span>
<span class="line"><span style="color: #002339">ld   b, a                  </span><span style="color: #357B42; font-style: italic">; B = A</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (ATTR_T)           </span><span style="color: #357B42; font-style: italic">; A = current attribute</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $f8                   </span><span style="color: #357B42; font-style: italic">; A = FLASH, BRIGHT and PAPER</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   b                     </span><span style="color: #357B42; font-style: italic">; Add INK</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   (ATTR_T), a           </span><span style="color: #357B42; font-style: italic">; Update current attribute</span></span>
<span class="line"><span style="color: #002339">ld   (ATTR_S), a           </span><span style="color: #357B42; font-style: italic">; Update permanent attribute</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Retrieves BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The INK routine gets the ink colour in A. We make sure to keep only the ink colour, get the current attributes, leave the flicker, brightness and background and add the ink. We update the system variables queried by RST $10 and by changing the channel to apply the colour attributes.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Assigns the background colour.
;
; Input: A -&gt; Background colour.
;
; Alters the value of the AF registers.
; -------------------------------------------------------------------
PAPER:
push bc                    ; Preserves BC
and  $07                   ; A = colour
rlca
rlca
rlca                       ; Rotate three bits to the left to set 
                           ; colour in paper/border bits
ld   b, a                  ; B = A
ld   a, (ATTR_T)           ; A = current attribute
and  $c7                   ; Remove background

or   b                     ; Add background
ld   (ATTR_T), a           ; Update current attribute
ld   (ATTR_S), a           ; Update permanent attribute
pop  bc                    ; Retrieves BC

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Assigns the background colour.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: A -&gt; Background colour.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PAPER:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserves BC</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$07</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = colour</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca                       </span><span style="color: #357B42; font-style: italic">; Rotate three bits to the left to set </span></span>
<span class="line"><span style="color: #002339">                           </span><span style="color: #357B42; font-style: italic">; colour in paper/border bits</span></span>
<span class="line"><span style="color: #002339">ld   b, a                  </span><span style="color: #357B42; font-style: italic">; B = A</span></span>
<span class="line"><span style="color: #002339">ld   a, (ATTR_T)           </span><span style="color: #357B42; font-style: italic">; A = current attribute</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $c7                   </span><span style="color: #357B42; font-style: italic">; Remove background</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   b                     </span><span style="color: #357B42; font-style: italic">; Add background</span></span>
<span class="line"><span style="color: #002339">ld   (ATTR_T), a           </span><span style="color: #357B42; font-style: italic">; Update current attribute</span></span>
<span class="line"><span style="color: #002339">ld   (ATTR_S), a           </span><span style="color: #357B42; font-style: italic">; Update permanent attribute</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Retrieves BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The PAPER routine gets the colour in A. We keep the colour, rotate it three bits to the left to put it in the background bits, and store it in B. We get the current attributes, remove the background, and add the one that came in A.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paints strings ending in $FF.
;
; Input: HL -&gt; Address of the string.
;
; Alters the value of the AF and HL registers.
; -------------------------------------------------------------------
PrintString:
ld   a, (hl)               ; A = character to paint
cp   $ff                   ; Is it $FF?
ret  z                     ; Yes, exit
rst  $10                   ; Paints character
inc  hl                    ; HL = next character
jr   PrintString           ; Loop to end of string" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paints strings ending in $FF.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: HL -&gt; Address of the string.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintString:</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = character to paint</span></span>
<span class="line"><span style="color: #002339">cp   $ff                   </span><span style="color: #357B42; font-style: italic">; Is it $FF?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Yes, exit</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints character</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = next character</span></span>
<span class="line"><span style="color: #002339">jr   PrintString           </span><span style="color: #357B42; font-style: italic">; Loop to end of string</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The PrintString routine has already been seen in Space Battle, so it needs no explanation.</p>



<p class="has-medium-font-size wp-block-paragraph">We will implement routines to clean up the whole screen, a line and the attributes.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Changes the attributes of the VideoRAM with the specified attribute.
;
; Input: A -&gt; Specified attribute (FBPPPIII)
;             Bits 0-2 Ink colour (0-7).
;             Bits 3-5 Paper colour (0-7).
;             Bit  6   Brightness (0/1).
;             Bit  7   Blink (0/1).
;
; Alters the value of the BC, DE and HL registers.
; -------------------------------------------------------------------
CLA:
ld   hl, VIDEOATT          ; HL = start attributes
ld   (hl), a               ; Change attribute
ld   de, VIDEOATT+1        ; DE = 2nd address attributes VideoRAM
ld   bc, VIDEOATT_L-1      ; BC = length attributes - 1
ldir                       ; Changes the attributes

ld   (ATTR_T), a           ; Updates system variable
ld   (ATTR_S), a           ; current attribute, permanent
ld   (BORDCR), a           ; and border
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Changes the attributes of the VideoRAM with the specified attribute.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: A -&gt; Specified attribute (FBPPPIII)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;             Bits 0-2 Ink colour (0-7).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;             Bits 3-5 Paper colour (0-7).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;             Bit  6   Brightness (0/1).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;             Bit  7   Blink (0/1).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the BC, DE and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">CLA:</span></span>
<span class="line"><span style="color: #002339">ld   hl, VIDEOATT          </span><span style="color: #357B42; font-style: italic">; HL = start attributes</span></span>
<span class="line"><span style="color: #002339">ld   (hl), a               </span><span style="color: #357B42; font-style: italic">; Change attribute</span></span>
<span class="line"><span style="color: #002339">ld   de, VIDEOATT+</span><span style="color: #174781">1</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; DE = 2nd address attributes VideoRAM</span></span>
<span class="line"><span style="color: #002339">ld   bc, VIDEOATT_L-</span><span style="color: #174781">1</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; BC = length attributes - 1</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Changes the attributes</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   (ATTR_T), a           </span><span style="color: #357B42; font-style: italic">; Updates system variable</span></span>
<span class="line"><span style="color: #002339">ld   (ATTR_S), a           </span><span style="color: #357B42; font-style: italic">; current attribute, permanent</span></span>
<span class="line"><span style="color: #002339">ld   (BORDCR), a           </span><span style="color: #357B42; font-style: italic">; and border</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The CLA routine receives in A the attributes to be assigned to the screen, in the format already seen above. It modifies them and updates the current, permanent and border attribute system variables.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Deletes the specified screen line.
;
; Input: B -&gt; Screen line to be deleted.
;             Inverted coordinates.
;
; Alters the value of the AF registers.
; -------------------------------------------------------------------
CLL:
ld   a, (ATTR_T)           ; A = current attributes
and  $3f                   ; A = PAPER + INK
ld   (ATTR_T), a           ; Update in memory

push bc                    ; Preserves BC

ld   c, INI_LEFT           ; C = 1st column
call AT                    ; Position cursor
ld   b, $20                ; B = 32 columns*line
CLL_loop:
ld   a, $20                ; A = space
rst  $10                   ; Prints it out
djnz CLL_loop              ; Loop while B &gt; 0

pop  bc                    ; Recover BC

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Deletes the specified screen line.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: B -&gt; Screen line to be deleted.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;             Inverted coordinates.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">CLL:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ATTR_T)           </span><span style="color: #357B42; font-style: italic">; A = current attributes</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$3f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = PAPER + INK</span></span>
<span class="line"><span style="color: #002339">ld   (ATTR_T), a           </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserves BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT           </span><span style="color: #357B42; font-style: italic">; C = 1st column</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$20</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = 32 columns*line</span></span>
<span class="line"><span style="color: #7EB233">CLL_loop:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$20</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = space</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Prints it out</span></span>
<span class="line"><span style="color: #002339">djnz CLL_loop              </span><span style="color: #357B42; font-style: italic">; Loop while B &gt; 0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Recover BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The CLL routine receives the line to be deleted in A (with inverted coordinates) and deletes it. Before it does this, it removes the brightness and blinking of the current attributes.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Deletes the graphics from the VideoRAM.
;
; Alters the value of the BC, DE and HL registers.
; -------------------------------------------------------------------
CLS:
ld   hl, VIDEORAM          ; HL = VideoRAM address
ld   de, VIDEORAM+1        ; DE = 2nd address VideoRAM
ld   bc, VIDEORAM_L-1      ; BC = length VideoRAM - 1
ld   (hl), $00             ; Clean 1st position
ldir                       ; Clean up the rest

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Deletes the graphics from the VideoRAM.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the BC, DE and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">CLS:</span></span>
<span class="line"><span style="color: #002339">ld   hl, VIDEORAM          </span><span style="color: #357B42; font-style: italic">; HL = VideoRAM address</span></span>
<span class="line"><span style="color: #002339">ld   de, VIDEORAM+</span><span style="color: #174781">1</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; DE = 2nd address VideoRAM</span></span>
<span class="line"><span style="color: #002339">ld   bc, VIDEORAM_L-</span><span style="color: #174781">1</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; BC = length VideoRAM - 1</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$00</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Clean 1st position</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Clean up the rest</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The CLS routine erases the graphic area of the screen, the pixels.</p>



<p class="has-medium-font-size wp-block-paragraph">As in Space Battle, we will draw some numerical data in BDC format, but this time, because the range of numbers will be between zero and ten, we will check if the ten is zero to draw a space instead.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paints the value of BCD numbers.
;
; It only paints numbers from 0 to 99.
;
; Input: HL -&gt; Pointer to the number to be painted.
;
; Alters the value of the AF register.
; -------------------------------------------------------------------
PrintBCD:
ld   a, (hl)               ; A = number to paint
and  $f0                   ; A = tens
rrca
rrca
rrca
rrca                       ; Tens to bits from 0 to 3
or   a                     ; Tens = 0?
jr   nz, PrintBCD_ascii    ; No, skip
ld   a, ' '                ; Tens = 0
jr   PrintBCD_continue     ; Paint space

PrintBCD_ascii:
add  a, '0'                ; A = A + Ascii code of 0

PrintBCD_continue:
rst  $10                   ; Paint 1st digit
ld   a, (hl)               ; A = number to paint
and  $0f                   ; Keeps the units
add  a, '0'                ; A = A + Ascii code of 0
rst  $10                   ; Print 2nd digit
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paints the value of BCD numbers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; It only paints numbers from 0 to 99.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: HL -&gt; Pointer to the number to be painted.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF register.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintBCD:</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = number to paint</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $f0                   </span><span style="color: #357B42; font-style: italic">; A = tens</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca                       </span><span style="color: #357B42; font-style: italic">; Tens to bits from 0 to 3</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Tens = 0?</span></span>
<span class="line"><span style="color: #002339">jr   nz, PrintBCD_ascii    </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, &#39; &#39;                </span><span style="color: #357B42; font-style: italic">; Tens = 0</span></span>
<span class="line"><span style="color: #002339">jr   PrintBCD_continue     </span><span style="color: #357B42; font-style: italic">; Paint space</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PrintBCD_ascii:</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; A = A + Ascii code of 0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PrintBCD_continue:</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paint 1st digit</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = number to paint</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Keeps the units</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; A = A + Ascii code of 0</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Print 2nd digit</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintBCD gets the HL pointer to the number to paint, loads it into A, takes the tens and if they are zero, paints a space. The rest of the routine is the same as in Space Battle.</p>



<p class="has-medium-font-size wp-block-paragraph">We have almost everything ready to paint the board, we just need the routine to do it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the board.
;
; Alters the value of the AF, BC, D and HL registers.
; -------------------------------------------------------------------
PrintBoard:
ld   a, INKBOARD           ; A = ink
call INK                   ; Change ink

; Initial coordinates of the board
ld   b, INI_TOP-$06        ; B = coord Y
ld   c, INI_LEFT-$09       ; C = coord X
ld   d, $0e                ; D = number of rows board

printBoard_1:
call AT                    ; Position cursor
ld   hl, Board_1           ; HL = vertical line
call PrintString           ; Paints vertical line characters
dec  b                     ; B-=1, next line
dec  d                     ; D-=1
jr  nz, printBoard_1       ; Loop while D &gt; 0

printBoard_2:
; Coordinates of the first horizontal line
ld   b, INI_TOP-$0a        ; B = coord Y
ld   c, INI_LEFT-$09       ; C = coord X
call AT                    ; Position cursor
ld   hl, Board_2           ; HL = horizontal line
call PrintString           ; Paint horizontal line

; Coordinates of the second horizontal line, 
; X-coordinate does not change.
ld   b, INI_TOP-$0f        ; B = coord Y
call AT                    ; Position cursor
ld   hl, Board_2
call PrintString           ; Paint horizontal line

printBoard_3:
; Paints the guide numbers so that the user knows which key to press
; To place the tokens (O X O)
ld   hl, Board_Helper      ; HL = helper
jp   PrintString           ; Paints helper and exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the board.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC, D and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintBoard:</span></span>
<span class="line"><span style="color: #002339">ld   a, INKBOARD           </span><span style="color: #357B42; font-style: italic">; A = ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Initial coordinates of the board</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$06</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$09</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #002339">ld   d, </span><span style="color: #174781">$0e</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; D = number of rows board</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printBoard_1:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Board_1           </span><span style="color: #357B42; font-style: italic">; HL = vertical line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString           </span><span style="color: #357B42; font-style: italic">; Paints vertical line characters</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; B-=1, next line</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  d                     </span><span style="color: #357B42; font-style: italic">; D-=1</span></span>
<span class="line"><span style="color: #002339">jr  nz, printBoard_1       </span><span style="color: #357B42; font-style: italic">; Loop while D &gt; 0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printBoard_2:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Coordinates of the first horizontal line</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0a</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$09</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Board_2           </span><span style="color: #357B42; font-style: italic">; HL = horizontal line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString           </span><span style="color: #357B42; font-style: italic">; Paint horizontal line</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Coordinates of the second horizontal line, </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; X-coordinate does not change.</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0f</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Board_2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString           </span><span style="color: #357B42; font-style: italic">; Paint horizontal line</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printBoard_3:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paints the guide numbers so that the user knows which key to press</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; To place the tokens (O X O)</span></span>
<span class="line"><span style="color: #002339">ld   hl, Board_Helper      </span><span style="color: #357B42; font-style: italic">; HL = helper</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintString           </span><span style="color: #357B42; font-style: italic">; Paints helper and exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintBoard paints the board, changes the ink and prepares everything for painting the vertical line, which is painted on printBoard_1. The horizontal line is painted on printBoard_2 and the guide for the player to know which keys to press is painted on printBoard_3.</p>



<p class="has-medium-font-size wp-block-paragraph">When the vertical and horizontal lines are drawn, the vertical line is erased where it crosses. Look closely at the definition of board2 and you will see that the same character is not painted over the whole board.</p>



<h2 id="main" class="wp-block-heading has-large-font-size">Main</h2>



<p class="has-medium-font-size wp-block-paragraph">To see if everything works, we implement the first version of the main.asm file.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org  $5e88

Main:
ld   hl, Sprite_P1
ld   (UDG), hl
ld   a, $02   
call OPENCHAN

xor  a
call BORDER
call CLA
call CLS

Init:
call PrintBoard

Loop:
jr   Loop

include &quot;rom.asm&quot;.
include &quot;screen.asm&quot;
include &quot;sprite.asm&quot;
include &quot;var.asm&quot;

end  Main" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$5e88</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Main:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Sprite_P1</span></span>
<span class="line"><span style="color: #002339">ld   (UDG), hl</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$02</span><span style="color: #002339">   </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> OPENCHAN</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  a</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> BORDER</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLA</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLS</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Init:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBoard</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">Loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">Loop</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">include &quot;rom.asm&quot;.</span></span>
<span class="line"><span style="color: #002339">include &quot;screen.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;sprite.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;var.asm&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end  Main</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We set the start address, bearing in mind that we will be making a custom loader. We show where the UDGs are, open channel two, change the border, attributes, clear the screen and paint the board. We stay in an infinite loop, which will be the main loop.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa33cfa6&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa33cfa6" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="702" height="606" data-attachment-id="4507" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/tictactoe-s01-01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S01-01.png?fit=702%2C606&amp;ssl=1" data-orig-size="702,606" data-comments-opened="1" data-image-title="TicTacToe-S01-01" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S01-01.png?fit=702%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S01-01.png?resize=702%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4507" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S01-01.png?w=702&amp;ssl=1 702w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S01-01.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 702px) 100vw, 702px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">We already have the board on which the game will be played.</p>



<p class="has-medium-font-size wp-block-paragraph">In the next chapter of ZX Spectrum Assembly, will be to implement the scoreboards and the two-player game, which will form a large part of the programme.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/22oz5y2m" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4497</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Space Battle &#8211; 0x0F ZEsarUX</title>
		<link>https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Sun, 01 Oct 2023 06:42:48 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Space Battle]]></category>
		<category><![CDATA[Z80]]></category>
		<category><![CDATA[ZEsarUX]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4459</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we will go deeper into the use of ZEsarUX. ZEsarUX Throughout this tutorial I have]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we will go deeper into the use of ZEsarUX.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<div role="navigation" aria-label="Tabla de contenidos" class="simpletoc wp-block-simpletoc-toc"><h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#zesarux">ZEsarUX</a>
</li>
<li><a href="#personalisation">Personalisation</a>
</li>
<li><a href="#debugging">Debugging</a>
</li>
<li><a href="#zx-spectrum-assembly-space-battle">ZX Spectrum Assembly, Space Battle</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul></div>


<h2 id="zesarux" class="wp-block-heading has-large-font-size">ZEsarUX</h2>



<p class="has-medium-font-size wp-block-paragraph">Throughout this tutorial I have used ZEsarUX, an emulator developed by César Hernández Bañó, which you can get for free at https://github.com/chernandezba/zesarux. César is constantly adding new features and making improvements.</p>



<p class="has-medium-font-size wp-block-paragraph">We have used ZEsarUX as a ZX Spectrum emulator, but it is capable of emulating many machines and doing many other things.</p>



<p class="has-medium-font-size wp-block-paragraph">In this chapter we will not be creating folders, copying files or generating code.</p>



<h2 id="personalisation" class="wp-block-heading has-large-font-size">Personalisation</h2>



<p class="has-medium-font-size wp-block-paragraph">The first step is to set up ZEsarUX to suit your needs. ZEsarUX has many options that can be configured, but we are going to focus on the ones that I think are most interesting for the use we are going to make of it in this book. I encourage you to do your own research.</p>



<p class="has-medium-font-size wp-block-paragraph">To customise ZEsarUX, press F5 to access the menu and then click on Preferences.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa359d5a&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa359d5a" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="390" height="242" data-attachment-id="4461" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-01.png?fit=390%2C242&amp;ssl=1" data-orig-size="390,242" data-comments-opened="1" data-image-title="SpaceBattle-S15-01" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-01.png?fit=390%2C242&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-01.png?resize=390%2C242&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4461" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-01.png?w=390&amp;ssl=1 390w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-01.png?resize=300%2C186&amp;ssl=1 300w" sizes="auto, (max-width: 390px) 100vw, 390px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">The first thing we need to do is configure a function key to restart the machine and another to enter debugging. Go to Settings and select the Function Keys option.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa35aaea&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa35aaea" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="351" height="352" data-attachment-id="4462" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-02/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-02.png?fit=351%2C352&amp;ssl=1" data-orig-size="351,352" data-comments-opened="1" data-image-title="SpaceBattle-S15-02" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-02.png?fit=351%2C352&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-02.png?resize=351%2C352&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4462" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-02.png?w=351&amp;ssl=1 351w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-02.png?resize=300%2C300&amp;ssl=1 300w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-02.png?resize=150%2C150&amp;ssl=1 150w" sizes="auto, (max-width: 351px) 100vw, 351px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Once in the Function Keys, select the F11 key by either clicking or pressing Enter.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa35b7df&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa35b7df" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="433" height="304" data-attachment-id="4463" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-03/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-03.png?fit=433%2C304&amp;ssl=1" data-orig-size="433,304" data-comments-opened="1" data-image-title="SpaceBattle-S15-03" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-03.png?fit=433%2C304&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-03.png?resize=433%2C304&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4463" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-03.png?w=433&amp;ssl=1 433w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-03.png?resize=300%2C211&amp;ssl=1 300w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-03.png?resize=130%2C90&amp;ssl=1 130w" sizes="auto, (max-width: 433px) 100vw, 433px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Once the key has been selected, we are shown the options available to us; we choose DebugCPU. We repeat the operation for the F12 key, but in this case we select Reset.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa35c432&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa35c432" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="464" height="320" data-attachment-id="4464" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-04/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-04.png?fit=464%2C320&amp;ssl=1" data-orig-size="464,320" data-comments-opened="1" data-image-title="SpaceBattle-S15-04" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-04.png?fit=464%2C320&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-04.png?resize=464%2C320&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4464" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-04.png?w=464&amp;ssl=1 464w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-04.png?resize=300%2C207&amp;ssl=1 300w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-04.png?resize=130%2C90&amp;ssl=1 130w" sizes="auto, (max-width: 464px) 100vw, 464px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">We need to test the buttons. Leave the menu by pressing Esc until it closes completely, or press F5 to go to the main menu and then Esc.</p>



<p class="has-medium-font-size wp-block-paragraph">Once the menu is closed, check that pressing F11 opens the debug window and that pressing F12 restarts the machine.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa35d137&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa35d137" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="708" height="686" data-attachment-id="4465" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-05/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-05.png?fit=708%2C686&amp;ssl=1" data-orig-size="708,686" data-comments-opened="1" data-image-title="SpaceBattle-S15-05" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-05.png?fit=708%2C686&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-05.png?resize=708%2C686&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4465" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-05.png?w=708&amp;ssl=1 708w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-05.png?resize=300%2C291&amp;ssl=1 300w" sizes="auto, (max-width: 708px) 100vw, 708px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">There are many functions that can be associated with a function key, in fact I have defined several keys.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa35dd72&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa35dd72" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="447" height="303" data-attachment-id="4466" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-06/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-06.png?fit=447%2C303&amp;ssl=1" data-orig-size="447,303" data-comments-opened="1" data-image-title="SpaceBattle-S15-06" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-06.png?fit=447%2C303&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-06.png?resize=447%2C303&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4466" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-06.png?w=447&amp;ssl=1 447w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-06.png?resize=300%2C203&amp;ssl=1 300w" sizes="auto, (max-width: 447px) 100vw, 447px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">I do not define F5 or F9 as one opens the menu and the other gives direct access to file upload.</p>



<p class="has-medium-font-size wp-block-paragraph">When debugging, I keep the debug window and the memory window visible, so it can be inferred that I am running out of space.</p>



<p class="has-medium-font-size wp-block-paragraph">Next we go to Settings, select ZX Desktop and then the first option, <strong>Enabled</strong>. When you select it, more options are displayed, the first of which we are interested in, <strong>width</strong>. Click on it until the value is 512 (or another value that suits you). We can also set the value directly if we select <strong>custom Width</strong>.</p>



<p class="has-medium-font-size wp-block-paragraph">To open the menu on the desktop, select the <strong>Open Menu on ZX Desktop</strong> option. On the other hand, if you don&#8217;t want the menu to open when you click on it, I find it more convenient to go to Preferences/General and uncheck the <strong>Clicking mouse open menu</strong> option.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa35ec79&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa35ec79" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="800" height="319" data-attachment-id="4467" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-07/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-07.png?fit=1024%2C408&amp;ssl=1" data-orig-size="1024,408" data-comments-opened="1" data-image-title="SpaceBattle-S15-07" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-07.png?fit=800%2C319&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-07.png?resize=800%2C319&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4467" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-07.png?w=1024&amp;ssl=1 1024w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-07.png?resize=300%2C120&amp;ssl=1 300w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-07.png?resize=768%2C306&amp;ssl=1 768w" sizes="auto, (max-width: 800px) 100vw, 800px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Now we have space to open other windows so that they don&#8217;t share space with the screen of our beloved ZX Spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">As I mentioned before, I like to have both the debug and memory windows visible while debugging. We need to change some options to be able to see how the memory is being updated while the program is running, and to be able to stop execution when the menu is open.</p>



<p class="has-medium-font-size wp-block-paragraph">Go to Settings/ZX Vision and almost at the end, select the options <strong>Stop emulation on menu</strong> and <strong>Background Windows</strong>. Once the second option is activated, you will see another option to activate <strong>Even when menu closed</strong>.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa35f9ad&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa35f9ad" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="562" height="544" data-attachment-id="4468" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-08/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-08.png?fit=562%2C544&amp;ssl=1" data-orig-size="562,544" data-comments-opened="1" data-image-title="SpaceBattle-S15-08" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-08.png?fit=562%2C544&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-08.png?resize=562%2C544&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4468" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-08.png?w=562&amp;ssl=1 562w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-08.png?resize=300%2C290&amp;ssl=1 300w" sizes="auto, (max-width: 562px) 100vw, 562px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">We have already set up the environment. All we need to do is open the debug windows and the hexadecimal editor from the Debug menu and place them where we want them; press F11 to open the debug window.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa3606f0&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa3606f0" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="800" height="317" data-attachment-id="4469" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-09/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-09.png?fit=1024%2C406&amp;ssl=1" data-orig-size="1024,406" data-comments-opened="1" data-image-title="SpaceBattle-S15-09" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-09.png?fit=800%2C317&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-09.png?resize=800%2C317&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4469" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-09.png?w=1024&amp;ssl=1 1024w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-09.png?resize=300%2C119&amp;ssl=1 300w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-09.png?resize=768%2C305&amp;ssl=1 768w" sizes="auto, (max-width: 800px) 100vw, 800px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Under Settings/General we can change the language, although at the time of writing not all terms have been translated.</p>



<p class="has-medium-font-size wp-block-paragraph">If at any time the windows do not respond, press F5.</p>



<h2 id="debugging" class="wp-block-heading has-large-font-size">Debugging</h2>



<p class="has-medium-font-size wp-block-paragraph">Let&#8217;s say we have a program and we load it at address $8000; make sure you don&#8217;t have the 16K machine selected or it won&#8217;t work.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org  $8000

Start:
ld   hl, $4000
ld   de, $8100
ld   a, $ff

Loop:
ld   (hl), a
ld   (de), a
dec  a
jr   nz, Loop

Exit:
jr   Start

end  $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Start:</span></span>
<span class="line"><span style="color: #002339">ld   hl, </span><span style="color: #174781">$4000</span></span>
<span class="line"><span style="color: #002339">ld   de, </span><span style="color: #174781">$8100</span></span>
<span class="line"><span style="color: #002339">ld   a, $ff</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">Loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), a</span></span>
<span class="line"><span style="color: #002339">ld   (de), a</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  a</span></span>
<span class="line"><span style="color: #002339">jr   nz, </span><span style="color: #7B30D0">Loop</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Exit:</span></span>
<span class="line"><span style="color: #002339">jr   Start</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end  </span><span style="color: #174781">$8000</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In this programme we point HL to the start of the VideoRAM, DE to position $8100 and load 255 ($FF) into A. Then we go through a loop in which we load the value of A into the positions pointed to by HL and DE and decrement A. At the end of the loop we go back to the start and continue in an infinite loop.</p>



<p class="has-medium-font-size wp-block-paragraph">The programme is very simple, but it is enough to show what I want to show you.</p>



<p class="has-medium-font-size wp-block-paragraph">The first thing we do is go into <strong>Debug</strong> and set a breakpoint at memory address $8000 so that execution stops at the start of the programme.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa361817&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa361817" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="591" height="384" data-attachment-id="4470" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-10/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-10.png?fit=591%2C384&amp;ssl=1" data-orig-size="591,384" data-comments-opened="1" data-image-title="SpaceBattle-S15-10" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-10.png?fit=591%2C384&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-10.png?resize=591%2C384&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4470" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-10.png?w=591&amp;ssl=1 591w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-10.png?resize=300%2C195&amp;ssl=1 300w" sizes="auto, (max-width: 591px) 100vw, 591px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">The different options are accessed by pressing the key corresponding to the highlighted letter.</p>



<p class="has-medium-font-size wp-block-paragraph">In the central part of the screen, on the left, we see the disassembled code and the address where each instruction is assembled. On the right, we see mainly the value of the different registers and the state of the flags. Just below this we see the values of the stack.</p>



<p class="has-medium-font-size wp-block-paragraph">At the top and bottom we see the different options; we will only see the ones we need to start debugging our programs.</p>



<p class="has-medium-font-size wp-block-paragraph">On the top right we see <strong>1-7</strong>: <strong>View</strong>. Clicking from one to seven will show different views of this window; by default view one is shown.</p>



<p class="has-medium-font-size wp-block-paragraph">At the top left we see <strong>m</strong>ptr. Pressing the M key opens a window where we can enter the address we want to go to. If we are entering hexadecimal values, the suffix H must be used.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa362799&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa362799" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="480" height="46" data-attachment-id="4471" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-11/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-11.png?fit=480%2C46&amp;ssl=1" data-orig-size="480,46" data-comments-opened="1" data-image-title="SpaceBattle-S15-11" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-11.png?fit=480%2C46&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-11.png?resize=480%2C46&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4471" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-11.png?w=480&amp;ssl=1 480w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-11.png?resize=300%2C29&amp;ssl=1 300w" sizes="auto, (max-width: 480px) 100vw, 480px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Once we have entered the address, we press Enter and the disassembly from that address is displayed.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa3633ec&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa3633ec" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="592" height="384" data-attachment-id="4472" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-12/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-12.png?fit=592%2C384&amp;ssl=1" data-orig-size="592,384" data-comments-opened="1" data-image-title="SpaceBattle-S15-12" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-12.png?fit=592%2C384&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-12.png?resize=592%2C384&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4472" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-12.png?w=592&amp;ssl=1 592w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-12.png?resize=300%2C195&amp;ssl=1 300w" sizes="auto, (max-width: 592px) 100vw, 592px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">As we haven&#8217;t loaded any programs yet, the only disassembly we see is NOP ($00).</p>



<p class="has-medium-font-size wp-block-paragraph">We set a breakpoint at address $8000. If we look at the fourth option in the second line of options, at the bottom of the window, we see Tog<strong>l</strong>; by pressing the L key we set or remove a breakpoint on the selected line.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa36406a&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa36406a" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="591" height="380" data-attachment-id="4473" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-13/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-13.png?fit=591%2C380&amp;ssl=1" data-orig-size="591,380" data-comments-opened="1" data-image-title="SpaceBattle-S15-13" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-13.png?fit=591%2C380&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-13.png?resize=591%2C380&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4473" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-13.png?w=591&amp;ssl=1 591w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-13.png?resize=300%2C193&amp;ssl=1 300w" sizes="auto, (max-width: 591px) 100vw, 591px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">We already have a breakpoint set at address $8000. When the program is loaded it will stop at this address and we will enter debugging mode. Press the N (Ru<strong>n</strong>) key to continue and return to Basic.</p>



<p class="has-medium-font-size wp-block-paragraph">ZEsarUX displays a window warning us that there is a breakpoint, we have to press a key to enter the <strong>Debug</strong> window. We can prevent this window from appearing in the debug properties.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa364d45&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa364d45" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="511" height="446" data-attachment-id="4474" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-14/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-14.png?fit=511%2C446&amp;ssl=1" data-orig-size="511,446" data-comments-opened="1" data-image-title="SpaceBattle-S15-14" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-14.png?fit=511%2C446&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-14.png?resize=511%2C446&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4474" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-14.png?w=511&amp;ssl=1 511w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-14.png?resize=300%2C262&amp;ssl=1 300w" sizes="auto, (max-width: 511px) 100vw, 511px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">We load the program and debug it.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa3659a6&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa3659a6" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="625" height="304" data-attachment-id="4475" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-15/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-15.png?fit=625%2C304&amp;ssl=1" data-orig-size="625,304" data-comments-opened="1" data-image-title="SpaceBattle-S15-15" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-15.png?fit=625%2C304&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-15.png?resize=625%2C304&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4475" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-15.png?w=625&amp;ssl=1 625w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-15.png?resize=300%2C146&amp;ssl=1 300w" sizes="auto, (max-width: 625px) 100vw, 625px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">To follow the programme, we can run it intwo ways: <strong>en</strong>: Stp and St<strong>o</strong>vr. In other environments, these are known as Step-by-instruction and Step-by-procedure.</p>



<p class="has-medium-font-size wp-block-paragraph">Step by step by instructions (Enter) executes the instructions one by one, and when we are faced with a <strong>CALL </strong>to a routine, either ours or from the ROM, we enter it, with the possibility of executing it step by step and seeing its code.</p>



<p class="has-medium-font-size wp-block-paragraph">In step by step procedures (O key), the execution is done instruction by instruction, but when we are faced with a <strong>CALL</strong> to a routine, either ours or from the ROM, it is executed and the PC (program counter) is passed to the instruction following the <strong>CALL</strong>; this must be taken into account in the loops.</p>



<p class="has-medium-font-size wp-block-paragraph">In our programme, when PC is at instruction <strong>JR NZ</strong>, <strong>8008</strong>, if we press Enter and A is not zero, it will jump to address $8008. Conversely, pressing O will execute the whole loop (all iterations) and point PC to the next instruction, <strong>JR 8000</strong>. Try it and see how it works.</p>



<p class="has-medium-font-size wp-block-paragraph">At each iteration of the loop, it loads the value of A into the addresses pointed to by HL and DE. We can only see the different values loaded at the address pointed to by HL because it points to VideoRAM.</p>



<p class="has-medium-font-size wp-block-paragraph">To see how the value of address $8100 changes, press F5, select the Hexadecimal Editor window, press M (memptr), type in address 8100H and press Enter.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa366c4f&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa366c4f" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="754" height="346" data-attachment-id="4476" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-16/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-16.png?fit=754%2C346&amp;ssl=1" data-orig-size="754,346" data-comments-opened="1" data-image-title="SpaceBattle-S15-16" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-16.png?fit=754%2C346&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-16.png?resize=754%2C346&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4476" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-16.png?w=754&amp;ssl=1 754w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-16.png?resize=300%2C138&amp;ssl=1 300w" sizes="auto, (max-width: 754px) 100vw, 754px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">If we remove the breakpoint and press N (Ru<strong>n</strong>) we will see that the position $8100 is updated.</p>



<p class="has-medium-font-size wp-block-paragraph">With this you should be able to debug programs, although ZEsarUX offers much more power, but it&#8217;s up to you to discover it, but let&#8217;s talk about one last thing: adding or modifying code directly in the debugger.</p>



<p class="has-medium-font-size wp-block-paragraph">In the first line of options at the bottom, pressing the S (stM) key will display other options. Pressing the A (assemble) key allows us to modify the instruction at the selected address. To leave the Assemble window, press the Esc key.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa3679f9&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa3679f9" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="624" height="527" data-attachment-id="4477" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-17/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-17.png?fit=624%2C527&amp;ssl=1" data-orig-size="624,527" data-comments-opened="1" data-image-title="SpaceBattle-S15-17" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-17.png?fit=624%2C527&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-17.png?resize=624%2C527&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4477" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-17.png?w=624&amp;ssl=1 624w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-17.png?resize=300%2C253&amp;ssl=1 300w" sizes="auto, (max-width: 624px) 100vw, 624px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">In our case, we will change the VideoRAM address where we load the value of A to column ten.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6ada1fa3685f0&quot;}" data-wp-interactive="core/image" data-wp-key="6ada1fa3685f0" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="755" height="345" data-attachment-id="4478" data-permalink="https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/spacebattle-s15-18/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-18.png?fit=755%2C345&amp;ssl=1" data-orig-size="755,345" data-comments-opened="1" data-image-title="SpaceBattle-S15-18" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Space Battle&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-18.png?fit=755%2C345&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-18.png?resize=755%2C345&#038;ssl=1" alt="ZX Spectrum Assembly, Space Battle" class="wp-image-4478" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-18.png?w=755&amp;ssl=1 755w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/SpaceBattle-S15-18.png?resize=300%2C137&amp;ssl=1 300w" sizes="auto, (max-width: 755px) 100vw, 755px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Space Battle</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">As you can see in the picture, the value of A is painted in column ten.</p>



<h2 id="zx-spectrum-assembly-space-battle" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Space Battle</h2>



<p class="has-medium-font-size wp-block-paragraph">We have adapted ZEsarUX and acquired the necessary knowledge to debug our programs, but ZEsarUX is very powerful and there is still a lot to discover.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/27lvln2y" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yn8q6c88" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Space Battle by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309095289979" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309095289979/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-space-battle-0x0f-zesarux/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4459</post-id>	</item>
	</channel>
</rss>
