<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: John Hardy</title>
    <description>The latest articles on DEV Community by John Hardy (@jhlagado).</description>
    <link>https://dev.to/jhlagado</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F80923%2Ffba96bbe-4ae2-4f98-9a6e-633a2c173712.jpg</url>
      <title>DEV Community: John Hardy</title>
      <link>https://dev.to/jhlagado</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhlagado"/>
    <language>en</language>
    <item>
      <title>Structured Programming in Z80 Assembly </title>
      <dc:creator>John Hardy</dc:creator>
      <pubDate>Wed, 18 Dec 2019 06:58:22 +0000</pubDate>
      <link>https://dev.to/jhlagado/structured-programming-in-z80-assembly-554d</link>
      <guid>https://dev.to/jhlagado/structured-programming-in-z80-assembly-554d</guid>
      <description>&lt;h1&gt;
  
  
  struct-z80 - Structured Programming in Z80 Assembly Language
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;title: Structured Programming in Z80 Assembly Language
published: true
description: Using assembly macros to write high level code
tags: Z80, macros, assembler, structured programming
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the great pains of writing assembly language for old-school microprocessors such as the Z80 is the complexity of implementing algorithms due to the lack of high-level control and looping structures. All you have are conditional jumps and labels and nothing to help you enforce the structure of your code.&lt;/p&gt;

&lt;p&gt;It's not exaggerating when someone claims that &lt;a href="https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf"&gt;GOTOs are considered harmful&lt;/a&gt; ...at least to your state of mental well-being! ;-)&lt;/p&gt;

&lt;p&gt;This lack of structure is what often ends up driving programmers in the direction of high-level languages such as C which people often think of as "low level", "assembler++" or "close to the metal". On 8-bit CPUs, however, this far from the truth and C adds a lot of overhead to your machine-cycle and memory-cell constrained code. This is why assembly language is still the tool of choice for 8-bit programming despite it also being a major source of frustration.&lt;/p&gt;

&lt;p&gt;Macros are a huge boon to writing assembly language. Recently I developed a set of macros that were inspired by a coding pattern invented by Garth Wilson and (quite separately by) Dave Keenan which enabled me to write structured programs in assembly language.&lt;/p&gt;

&lt;p&gt;See:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://wilsonminesco.com/StructureMacros/"&gt;Program Structures with Macros&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://dkeenan.com/AddingStructuredControlFlowToAnyAssembler.htm"&gt;Adding Structured Control Flow to Any Assembler&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both authors were heavily influenced by the &lt;a href="https://www.forth.com/forth/"&gt;Forth programming language&lt;/a&gt; and the way that it introduced high-level structured programming concepts to low level programming years before systems languages like C and Pascal became commonplace.&lt;/p&gt;

&lt;p&gt;The examples I'm giving here were written using the asm80 macro system but I'm sure they could be easily adapted to your own favourite assembly's macro syntax.&lt;/p&gt;

&lt;p&gt;See:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.asm80.com/"&gt;Asm80&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://maly.gitbooks.io/asm80/macros.html"&gt;Asm80: Macros&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Macro library contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/jhlagado/struct-z80/blob/master/struct.md"&gt;Control structures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jhlagado/struct-z80/blob/master/dloop.md"&gt;Looping structures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jhlagado/struct-z80/blob/master/stack.md"&gt;Structure stacks&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;A repo of all the macros discussed here can be found here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/jhlagado/struct-z80"&gt;struct-z80&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Include the following files in your project&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;_if&lt;/code&gt;, &lt;code&gt;_else&lt;/code&gt;, &lt;code&gt;_endif&lt;/code&gt;, &lt;code&gt;_switch&lt;/code&gt;, &lt;code&gt;_case,&lt;/code&gt; &lt;code&gt;_endcase&lt;/code&gt;, &lt;code&gt;_enddo&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.include "struct-macros.z80"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;_do&lt;/code&gt;, &lt;code&gt;_while&lt;/code&gt;, &lt;code&gt;_until&lt;/code&gt;, &lt;code&gt;_break&lt;/code&gt;, &lt;code&gt;_continue&lt;/code&gt;, &lt;code&gt;_endo&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.include "dloop-macros.z80"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Copyleft @ 2019 John Hardy, ALL WRONGS RESERVED&lt;/em&gt;&lt;br&gt;
This software is released under the GNU public license 3.0&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Contact me: &lt;a href="//mailto:jh@lagado.com"&gt;jh@lagado.com&lt;/a&gt; GitHub: &lt;a href="http://github.com/jhlagado"&gt;jhlagado&lt;/a&gt; Twitter: &lt;a href="https://twitter.com/jhlagado"&gt;@jhlagado&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
