<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://learn.cemetech.net/index.php?action=history&amp;feed=atom&amp;title=Z80%3AAnswers_3</id>
	<title>Z80:Answers 3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://learn.cemetech.net/index.php?action=history&amp;feed=atom&amp;title=Z80%3AAnswers_3"/>
	<link rel="alternate" type="text/html" href="http://learn.cemetech.net/index.php?title=Z80:Answers_3&amp;action=history"/>
	<updated>2026-04-29T13:02:41Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>http://learn.cemetech.net/index.php?title=Z80:Answers_3&amp;diff=140&amp;oldid=prev</id>
		<title>KermMartian: Created page with &quot;1.      loop:      cp b      jr c,bigger      jr z,endLoop      inc c      inc a      jr loop     bigger:      dec c      dec a      jr loop     endLoop:      ...   == Identif...&quot;</title>
		<link rel="alternate" type="text/html" href="http://learn.cemetech.net/index.php?title=Z80:Answers_3&amp;diff=140&amp;oldid=prev"/>
		<updated>2016-02-05T07:34:19Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;1.      loop:      cp b      jr c,bigger      jr z,endLoop      inc c      inc a      jr loop     bigger:      dec c      dec a      jr loop     endLoop:      ...   == Identif...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;1.&lt;br /&gt;
&lt;br /&gt;
    loop:&lt;br /&gt;
     cp b&lt;br /&gt;
     jr c,bigger&lt;br /&gt;
     jr z,endLoop&lt;br /&gt;
     inc c&lt;br /&gt;
     inc a&lt;br /&gt;
     jr loop&lt;br /&gt;
    bigger:&lt;br /&gt;
     dec c&lt;br /&gt;
     dec a&lt;br /&gt;
     jr loop&lt;br /&gt;
    endLoop:&lt;br /&gt;
     ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Identify the Error(s) ==&lt;br /&gt;
&lt;br /&gt;
2. &lt;br /&gt;
&lt;br /&gt;
    inc b&lt;br /&gt;
&lt;br /&gt;
[[Z80:Opcodes:DJNZ|DJNZ]] uses B as the counter, so increasing B in the loop will result in an infinite loop&lt;br /&gt;
&lt;br /&gt;
3. &lt;br /&gt;
&lt;br /&gt;
    jp mUp		;assume mUp exists somewhere&lt;br /&gt;
     ...&lt;br /&gt;
     jp mDown		;assume mDown exists somewhere&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The error is not that mUp or mDown does not exist, it&amp;#039;s that there is no check on any flag. It will jump regardless, so in this case it will always jump to mUp.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Fixed code:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    jp z,mUp&lt;br /&gt;
    ...&lt;br /&gt;
    jp z,mDown&lt;br /&gt;
&lt;br /&gt;
4. Nothing wrong with the code.&lt;br /&gt;
&lt;br /&gt;
5. There&amp;#039;s nothing wrong with the code. We want it to repeat 256 times, and since [[Z80:Opcodes:DJNZ|DJNZ]] decrements first, by setting B to zero it will first change B to 255, then repeat 255 times more. 255+1=256. Note that this is &amp;#039;&amp;#039;&amp;#039;bad&amp;#039;&amp;#039;&amp;#039; coding practice, though. Generally if you want a counter larger than $FF (255), you&amp;#039;ll use BC as a 16 bit counter.&lt;br /&gt;
&lt;br /&gt;
{{lowercase}}&lt;br /&gt;
[[Category:Z80 Assembly]]&lt;br /&gt;
[[Category:Z80 Heaven]]&lt;/div&gt;</summary>
		<author><name>KermMartian</name></author>
	</entry>
</feed>