<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.temlib.org/AtariForumWiki/index.php?action=history&amp;feed=atom&amp;title=Earxtutchap5</id>
	<title>Earxtutchap5 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.temlib.org/AtariForumWiki/index.php?action=history&amp;feed=atom&amp;title=Earxtutchap5"/>
	<link rel="alternate" type="text/html" href="https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;action=history"/>
	<updated>2026-05-13T17:19:27Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.2</generator>
	<entry>
		<id>https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13022&amp;oldid=prev</id>
		<title>&gt;Wongck at 15:31, 12 October 2011</title>
		<link rel="alternate" type="text/html" href="https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13022&amp;oldid=prev"/>
		<updated>2011-10-12T15:31:44Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 11:31, 12 October 2011&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l224&quot;&gt;Line 224:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 224:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Back to [[ASM_Tutorial]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Back to [[ASM_Tutorial]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Programming&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Making optimized assembly code by Earx&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>&gt;Wongck</name></author>
	</entry>
	<entry>
		<id>https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13021&amp;oldid=prev</id>
		<title>&gt;Silver Surfer: Added category</title>
		<link rel="alternate" type="text/html" href="https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13021&amp;oldid=prev"/>
		<updated>2009-05-02T17:11:22Z</updated>

		<summary type="html">&lt;p&gt;Added category&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 13:11, 2 May 2009&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l224&quot;&gt;Line 224:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 224:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Back to [[ASM_Tutorial]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Back to [[ASM_Tutorial]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Programming]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>&gt;Silver Surfer</name></author>
	</entry>
	<entry>
		<id>https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13020&amp;oldid=prev</id>
		<title>&gt;Zorro 2 at 08:09, 9 October 2006</title>
		<link rel="alternate" type="text/html" href="https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13020&amp;oldid=prev"/>
		<updated>2006-10-09T08:09:03Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 04:09, 9 October 2006&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l222&quot;&gt;Line 222:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 222:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This brings us to the end of this chapter.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This brings us to the end of this chapter.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Back to [[ASM_Tutorial]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>&gt;Zorro 2</name></author>
	</entry>
	<entry>
		<id>https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13019&amp;oldid=prev</id>
		<title>&gt;Simonsunnyboy at 17:47, 6 October 2006</title>
		<link rel="alternate" type="text/html" href="https://www.temlib.org/AtariForumWiki/index.php?title=Earxtutchap5&amp;diff=13019&amp;oldid=prev"/>
		<updated>2006-10-06T17:47:24Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
                        .-&amp;lt;-.                       .-&amp;gt;-.&lt;br /&gt;
                        |   |                       |   |&lt;br /&gt;
                        v  CHAPTER 5 : EXTREME LOOPING  v&lt;br /&gt;
                        |   |                       |   |&lt;br /&gt;
                        '-&amp;gt;-'                       '-&amp;lt;-'&lt;br /&gt;
&lt;br /&gt;
If you've coded some good solid loops and think they are as fast as they can&lt;br /&gt;
possibly get, then you might be wrong. It's always amazing to see how awkward&lt;br /&gt;
structures can make your code more readable and maybe even accelerate your&lt;br /&gt;
code. A large category of these structures are special loops or replacements&lt;br /&gt;
for them. Some other structures or methods for structurising can be found in&lt;br /&gt;
chapter 8.&lt;br /&gt;
&lt;br /&gt;
Instruction cache:&lt;br /&gt;
&lt;br /&gt;
Since the 68020 arrived, the CPUs have had cache. This is a small piece of&lt;br /&gt;
very fast memory that buffers small parts of the RAM. The RAM-access if&lt;br /&gt;
ofcourse slow and also remember that instructions need to be fetched from&lt;br /&gt;
here!!&lt;br /&gt;
&lt;br /&gt;
If you could put a loop completely in cache you could eliminate all&lt;br /&gt;
waitstates for waiting for a words and happily go on. The 68020's cache&lt;br /&gt;
isn't that efficient and only the 68030 and on have efficient instruction&lt;br /&gt;
caching.&lt;br /&gt;
&lt;br /&gt;
The 68030 offers to put a small loop (max. 256 bytes) completely in it's&lt;br /&gt;
instruction cache! The 68040 and 68060 can do the same for max. 4KB!! (which&lt;br /&gt;
is always more than enough for assembler).&lt;br /&gt;
&lt;br /&gt;
So it was actually faster on ST to completely unroll the whole loop until&lt;br /&gt;
half your RAM was full, now it is faster to keep it under 256 bytes. This is&lt;br /&gt;
funny, because keeping loops small makes them more readable as well as&lt;br /&gt;
faster now! =)&lt;br /&gt;
&lt;br /&gt;
But beware: only doing small loop of under 10 bytes, say 3 or 4 times is&lt;br /&gt;
basicly slower than unrolling it on 68030/040/060!!!&lt;br /&gt;
&lt;br /&gt;
Effective dbra: (Is that one of those pushup Dbra's?? =))&lt;br /&gt;
&lt;br /&gt;
When working with bit-by-bit operations the dbra instruction can be more&lt;br /&gt;
than a convenience. We know that &amp;quot;dbra&amp;quot; uses one data-register as it's&lt;br /&gt;
counter and updates this every loopcycle. Now if we could use this counter&lt;br /&gt;
for other purposes we would do it. right?&lt;br /&gt;
&lt;br /&gt;
Ok ok, not always. We know that using the counter as a memoryindex is slow&lt;br /&gt;
compared to used post-/pre- incremented addressing ( (an)+ -(an) ). But when&lt;br /&gt;
working with bitfields in a data-register the counter does come in handy&lt;br /&gt;
indeed.&lt;br /&gt;
&lt;br /&gt;
A good example is looking for the highest bit that is 1 in a longword.&lt;br /&gt;
&lt;br /&gt;
* d0.l: 32bits long bitfield&lt;br /&gt;
        moveq   #32-1,d7                        * Set count to highest bit.&lt;br /&gt;
&lt;br /&gt;
loop:   btst    d7,d0                           * Test current bit.&lt;br /&gt;
        bne.s   found                           * If bit is 1 -&amp;gt; found!&lt;br /&gt;
        dbra    d7,loop                         * Until d7 is -1.&lt;br /&gt;
found:&lt;br /&gt;
&lt;br /&gt;
Dbcc loops:&lt;br /&gt;
&lt;br /&gt;
Only in some innerloops this can be very handy. When you want to terminate a&lt;br /&gt;
certain loop after you've found something interesting or else it loops a&lt;br /&gt;
maximum amount of times, this is a good solution. A good example is reading&lt;br /&gt;
from a string of characters, until you've done 256 or you've found the&lt;br /&gt;
NULL-character.&lt;br /&gt;
&lt;br /&gt;
A &amp;quot;dbcc&amp;quot; intruction is very similar to the &amp;quot;dbra&amp;quot; instruction. The only&lt;br /&gt;
difference being that it also &amp;quot;falls through&amp;quot; (i.e. doesn't reloop) when it the&lt;br /&gt;
conditioncode it expects is found.&lt;br /&gt;
&lt;br /&gt;
Let's adapt our previous &amp;quot;dbra&amp;quot; example to use a dbcc-instruction:&lt;br /&gt;
&lt;br /&gt;
* d0.l: 32bits long bitfield&lt;br /&gt;
        moveq   #32-1,d7                        * Set count to highest bit.&lt;br /&gt;
&lt;br /&gt;
loop:   btst    d7,d0                           * Test current bit.&lt;br /&gt;
        dbeq    d7,loop                         * Until bit is 1 or d7 is -1.&lt;br /&gt;
found:&lt;br /&gt;
&lt;br /&gt;
An example of getting the length of a string with dbeq:&lt;br /&gt;
&lt;br /&gt;
* INPUT: a0: startaddress of string&lt;br /&gt;
        move.w  #256-1,d7                       * Loop a maximum of 256 times.&lt;br /&gt;
loop:   tst.b   (a0)+                           * Test if this is a NULL.&lt;br /&gt;
        dbeq    d7,loop                         * Reloop until NULL max times.&lt;br /&gt;
&lt;br /&gt;
At the end of this loop you can actually test if the string was NULL terminated&lt;br /&gt;
or that it exceeded 256 characters. If d7.w contains -1 then maximum was&lt;br /&gt;
exceeded. Ofcourse you can also check how many characters are done, by&lt;br /&gt;
calculating the difference from the initial loopcounter like so:&lt;br /&gt;
&lt;br /&gt;
        subi.w  #256,d7                         * Get difference from origin.&lt;br /&gt;
        neg.w   d7                              * Make it positive.&lt;br /&gt;
&lt;br /&gt;
Jumptrees:&lt;br /&gt;
&lt;br /&gt;
A really good technique that is used to replace flexible-length innerloops with&lt;br /&gt;
&amp;quot;dbra&amp;quot;. It relies on unrolling the loop a couple of times. This is called the&lt;br /&gt;
&amp;quot;tree&amp;quot;. The trick is to jump in this tree at the right position, so that you&lt;br /&gt;
the good number of iterations are done.&lt;br /&gt;
&lt;br /&gt;
This is ofcourse much faster than the overhead of the dbra-instruction&lt;br /&gt;
everytime a loop is processed. On ST you can make these trees as big as you&lt;br /&gt;
want. And only one jump-instruction on a total of maybe 10 iterations is&lt;br /&gt;
already neglegible. On the 68030 you should keep in mind that the tree must&lt;br /&gt;
always be under 256 bytes if you want all this to be executed from the cache.&lt;br /&gt;
&lt;br /&gt;
There's one more drawback for both ST newer machines: The iteration must&lt;br /&gt;
preferably have a size that is equal to 2^x! Otherwise a somewhat more costly&lt;br /&gt;
multiply instuction must be done instead of a simple shift to index the&lt;br /&gt;
position in the tree.&lt;br /&gt;
&lt;br /&gt;
Let's have an example for drawing a variably sized horizontal line on the&lt;br /&gt;
ST. It's done per 16 pixels:&lt;br /&gt;
&lt;br /&gt;
* INPUT: d0.w: number of pixels in line.&lt;br /&gt;
*        a0: screenaddress&lt;br /&gt;
        moveq   #$ffffffff,d1                   * Put colorcode in d1.l.&lt;br /&gt;
        add.w   d0,d0                           * / Multiply d0.w&lt;br /&gt;
        add.w   d0,d0                           * \ with 4.&lt;br /&gt;
        neg.w   d0                              * d0.w = -d0.w&lt;br /&gt;
        jmp     endjumptree(pc,d0.w)            * Jump in the tree.&lt;br /&gt;
        REPT    20                              * Repeat code 20 times.&lt;br /&gt;
        move.l  d1,(a0)+                        * / Paint next 16 pixels&lt;br /&gt;
        move.l  d1,(a0)+                        * \ color 15.&lt;br /&gt;
        ENDR                                    * Indicate end of repeatcode.&lt;br /&gt;
endjumptree:&lt;br /&gt;
&lt;br /&gt;
The jumptree is great because once you've got your index into the tree you&lt;br /&gt;
don't have recalculate the index anymore or restore it like in &amp;quot;dbra&amp;quot;-loop.&lt;br /&gt;
&lt;br /&gt;
Jumptables:&lt;br /&gt;
&lt;br /&gt;
These are a replacement for a long line of compares on a code (for Pascal/C--&lt;br /&gt;
users: a &amp;quot;case&amp;quot; or &amp;quot;switch&amp;quot; statement). Especially stuff like handling the&lt;br /&gt;
keycodes from the keyboard is an important issue. Many other situations are&lt;br /&gt;
suited to implement as a jumptable.&lt;br /&gt;
&lt;br /&gt;
For instance we want to execute little subroutines for a few different&lt;br /&gt;
keycodes:&lt;br /&gt;
&lt;br /&gt;
* INPUT: d0.b: keycode&lt;br /&gt;
compare0:&lt;br /&gt;
        cmpi.b  #$39,d0&lt;br /&gt;
        bne.s   compare1&lt;br /&gt;
        bra     TERMINATE_PROGRAM&lt;br /&gt;
compare1:&lt;br /&gt;
        cmpi.b  #1,d0&lt;br /&gt;
        bne.s   compare2&lt;br /&gt;
        bra     TERMINATE_PROGRAM&lt;br /&gt;
compare2:&lt;br /&gt;
        cmpi.b  #$4e,d0&lt;br /&gt;
        bne.s   compare3&lt;br /&gt;
        bra     MOVE_PLAYERFORWARD&lt;br /&gt;
compare3:&lt;br /&gt;
        cmpi.b  #$4a,d0&lt;br /&gt;
        bne.s   compare4&lt;br /&gt;
        bra     MOVE_PLAYERBACKWARD&lt;br /&gt;
compare4:&lt;br /&gt;
        cmpi.b  #$4b,d0&lt;br /&gt;
        bne.s   compare5&lt;br /&gt;
        bra     MOVE_PLAYERLEFT&lt;br /&gt;
compare5:&lt;br /&gt;
        cmpi.b  #$4d,d0&lt;br /&gt;
        bne.s   compare6&lt;br /&gt;
        bra     MOVE_PLAYERRIGHT&lt;br /&gt;
compare6:&lt;br /&gt;
        cmpi.b  #$48,d0&lt;br /&gt;
        bne.s   compare7&lt;br /&gt;
        bra     INC_PLAYERSPEED&lt;br /&gt;
compare7:&lt;br /&gt;
        cmpi.b  #$50,d0&lt;br /&gt;
        beq     DEC_PLAYERSPEED&lt;br /&gt;
        rts&lt;br /&gt;
&lt;br /&gt;
The bestcase scenerio in this example is the code is identified with the&lt;br /&gt;
first compare. The worstcase is, it isn't identified at all and all 8&lt;br /&gt;
compares are done for nothing. And you can imagine you may want to identify&lt;br /&gt;
more keys as well...&lt;br /&gt;
&lt;br /&gt;
Ofcourse the code gets bigger and less readable and hard to upgrade&lt;br /&gt;
everytime as a code is added. The solution to this is the jumptable. But&lt;br /&gt;
it's only handy when over 6 codes need to be identified and the codes don't&lt;br /&gt;
have a big range (like keyboard codes).&lt;br /&gt;
&lt;br /&gt;
A jumptable is a table with &amp;quot;bra.w&amp;quot; instructions to your little subroutines.&lt;br /&gt;
These are 4 bytes in size. You jump into the jumptable with a &amp;quot;jsr&amp;quot;-&lt;br /&gt;
instruction at jumptable-startaddress + code*4.&lt;br /&gt;
&lt;br /&gt;
* INPUT: d0.b: keycode.&lt;br /&gt;
        moveq   #0,d1                           * / Calculate&lt;br /&gt;
        move.b  d0,d1                           * | offset&lt;br /&gt;
        add.l   d1,d1                           * | in&lt;br /&gt;
        add.l   d1,d1                           * \ jumptable.&lt;br /&gt;
        jsr     jumptable(pc,d1.l)              * Jump in the table.&lt;br /&gt;
&lt;br /&gt;
jumptable:&lt;br /&gt;
        bra.w   dummy                           * Code 0 points to a &amp;quot;rts&amp;quot;.&lt;br /&gt;
        bra.w   ..&lt;br /&gt;
        bra.w   ...&lt;br /&gt;
        bra.w   ..&lt;br /&gt;
        ...&lt;br /&gt;
&lt;br /&gt;
dummy:  rts&lt;br /&gt;
&lt;br /&gt;
Note that all entries must be branches to subroutines (i.e. they must always&lt;br /&gt;
be terminated with a &amp;quot;rts&amp;quot; instruction). Also note that every code that&lt;br /&gt;
doesn't need to be processed must be pointed to a dummy instruction!&lt;br /&gt;
&lt;br /&gt;
This solution is more readable and allows the coder to seperate code from&lt;br /&gt;
data. So this is easier to upgrade and quite alot faster. If tables get too&lt;br /&gt;
big you can always put in a few normal compared to choose between various&lt;br /&gt;
jumptables.&lt;br /&gt;
&lt;br /&gt;
A jumptable is an excellent solution to unreadable chunks of code and quite&lt;br /&gt;
fast at that too! But a jumptable will most definetely cause cache misses&lt;br /&gt;
and hence needs to be avoided when using a loop to process codes over and&lt;br /&gt;
over.&lt;br /&gt;
&lt;br /&gt;
This brings us to the end of this chapter.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>&gt;Simonsunnyboy</name></author>
	</entry>
</feed>