<?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>disassemble | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/disassemble/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Sat, 23 May 2020 09:08:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>disassemble | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>An interesting way to find a bug</title>
		<link>https://learncgames.com/an-interesting-way-to-find-a-bug/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=an-interesting-way-to-find-a-bug</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 29 May 2020 23:00:13 +0000</pubDate>
				<category><![CDATA[bugs]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[disassemble]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=706</guid>

					<description><![CDATA[<p>Here&#8217;s a bit of code with a very subtle bug. It wasn&#8217;t ever setting the size file (an int field in a struct). So I took a look at the assembly generated and spotted it. In retrospect it was a bit obvious! void DoRotateAndDie() { for (int i = 0; i &#60; 10; i++) { [&#8230;]</p>
The post <a href="https://learncgames.com/an-interesting-way-to-find-a-bug/">An interesting way to find a bug</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:703,&quot;href&quot;:&quot;https:\/\/pixabay.com\/photos\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=336507&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260129081545\/https:\/\/pixabay.com\/photos\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 17:44:07&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-14 05:26:57&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-20 06:21:21&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-24 05:17:33&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-28 21:45:30&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-04 08:58:06&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-08 13:20:41&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-12 03:15:15&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-11 01:53:49&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-11 01:53:49&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:704,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=336507&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 17:44:10&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-14 05:26:57&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-20 06:21:21&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-24 05:17:33&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-28 21:45:31&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-04 08:58:06&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-08 13:20:42&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-12 03:15:15&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-11 01:53:49&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-11 01:53:49&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_708" aria-describedby="caption-attachment-708" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-708" src="https://learncgames.com/wp-content/uploads/2020/05/disassembly-336507_640-300x197.jpg" alt="Disassembly" width="300" height="197" srcset="https://learncgames.com/wp-content/uploads/2020/05/disassembly-336507_640-300x197.jpg 300w, https://learncgames.com/wp-content/uploads/2020/05/disassembly-336507_640.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-708" class="wp-caption-text">Image by <a href="https://pixabay.com/photos/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=336507">Free-Photos</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=336507">Pixabay</a></figcaption></figure>
<p>Here&#8217;s a bit of code with a very subtle bug. It wasn&#8217;t ever setting the size file (an int field in a struct). So I took a look at the assembly generated and spotted it. In retrospect it was a bit obvious!</p>
<pre><code class="language-c" data-line="">void DoRotateAndDie() {
	for (int i = 0; i &lt; 10; i++) {
		while(1) {
			int x = Random(MAXBOARDWIDTH) - 1;
			int y = Random(MAXBOARDHEIGHT) - 1;
			pBoardPiece ppiece = board[y][x].ppiece;
			if (!ppiece) continue;
			if (ppiece-&gt;size != 0) continue; // Not this one
			ppiece-&gt;size == 64;
			break;
		}
	}
}</code></pre>
<p>It&#8217;s somewhat stupid. The line just before the break is meant to be an assignment but there&#8217;s double ==. Stranbgely enough the C compiler In Visual Studio didn&#8217;t generate a warning or error. When I put a break point on the line, it hit the break instead.</p>
<p>I was curious to see what code was generated. Here&#8217;s the disassembly.</p>
<pre><code class="language-markup" data-line="">
			if (ppiece-&gt;size != 0) continue; // Not this one
00124892  mov         eax,dword ptr [ebp-2Ch]  
00124895  cmp         dword ptr [eax+30h],0  
00124899  je          DoRotateAndDie+8Dh (012489Dh)  
0012489B  jmp         DoRotateAndDie+40h (0124850h)  
			ppiece-&gt;size == 64;
			break;
0012489D  jmp         DoRotateAndDie+91h (01248A1h) </code></pre>
<p>So it doesn&#8217;t generate any code at all for that assignment of 64, it&#8217;s just two jmps with no assignment! But fixing it and checking the code this time produces this:</p>
<pre><code class="language-markup" data-line="">			if (ppiece-&gt;size != 0) continue; // Not this one
00144895  cmp         dword ptr [eax+30h],0  
00144899  je          DoRotateAndDie+8Dh (014489Dh)  
0014489B  jmp         DoRotateAndDie+40h (0144850h)  
			ppiece-&gt;size = 64;
0014489D  mov         eax,dword ptr [ebp-2Ch]  
001448A0  mov         dword ptr [eax+30h],40h  </code></pre>
<p>Those last two lines assign 64 (40h in assembly).</p>
<p>Normally I pick up these type of bugs just by visual inspection. If it isn&#8217;t obvious then there are two other techniques to try. The first is get a colleague, or if one isn&#8217;t handy a teddy bear or toy duck will do. Now explain to the colleague/teddy bear/duck how the code works. Explicitly say it out loud, do not just think it. It&#8217;s amazing how often that works. The process of explaining it forces your brain to do a bit more work then if you just mentally walked the code.</p>
<p>The other method is to disassemble the code and look at it from a different point of view. If the compiler sees the code differently than how you think it should be, it might provide a clue. Here I found out that putting an expression in code instead of a statement, generates no code. Normally with =/== it&#8217;s the opposite, putting in an assignment instead of a comparison.</p>The post <a href="https://learncgames.com/an-interesting-way-to-find-a-bug/">An interesting way to find a bug</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">706</post-id>	</item>
	</channel>
</rss>
