{"id":37,"date":"2022-05-13T04:25:00","date_gmt":"2022-05-13T04:25:00","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/ettucode\/?p=37"},"modified":"2022-05-13T04:25:00","modified_gmt":"2022-05-13T04:25:00","slug":"the-trials-and-tribulations-of-debugging-code","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/ettucode\/2022\/05\/13\/the-trials-and-tribulations-of-debugging-code\/","title":{"rendered":"The Trials and Tribulations of Debugging Code"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/debate.protocommunications.com\/wp-content\/uploads\/2018\/03\/frustrated-meme.png\" alt=\"\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">It works. Kind of?<\/h3>\n\n\n\n<p>There comes a point in every programmer&#8217;s education\/career where, after spending weeks writing and testing code, one excitedly compiles and runs their code, expecting great things. Often, there seems to be a bug that is causing the program to behave unexpectedly and worse, in the worst case, it seems impossible to find or diagnose.<\/p>\n\n\n\n<p>As far as I&#8217;m aware, every programmer has to deal with this situation, regardless of how competent\/experienced they are. Perhaps there are people that don&#8217;t make mistakes, but that&#8217;s certainly not me.<\/p>\n\n\n\n<p>Our 8080 emulator has all the requisite instructions to run Space Invaders. We&#8217;ve fastidiously followed the advice and directions of those who have worked on or completed similar projects, and yet, things are not working as we would hope.<\/p>\n\n\n\n<p>Here is the output of a Space Invaders emulator that works, at the title screen:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"838\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-1024x838.png\" alt=\"\" class=\"wp-image-38\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-1024x838.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-300x246.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-768x629.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-1536x1258.png 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image.png 1617w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Our title screen looks similar, but the section under &#8220;Score Advance Table&#8221; is shifted to the right, so that only the left-half of the sprites is visible on the right- side of the screen. Bizarre.<\/p>\n\n\n\n<p>But what&#8217;s this? One of our instructions, RNZ, is not implemented correctly! What if we fix it and run the code again?<\/p>\n\n\n\n<p>Brilliant! Our emulator now displays the main menu and scoring table. All it took was inspecting the code visually. Unfortunately we haven&#8217;t found any other errors, and the program crashes once it attempts to enter &#8220;attract mode&#8221;, which is the game&#8217;s demo mode. The emulator is asking for yet another instruction to be implemented, but since other 8080 Space Invaders projects don&#8217;t emulate this instruction, let&#8217;s instead try running the program a few times to see how it behaves.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"80\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-1.png\" alt=\"\" class=\"wp-image-39\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-1.png 650w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5438\/files\/2022\/05\/image-1-300x37.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><figcaption>interesting&#8230;<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Power (sometimes) corrupts<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/farfarawayradio.com\/wp-content\/uploads\/2016\/08\/Palpatine-1024x436.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<p>Every programmer that&#8217;s ever been introduced to, or heard of C, might have heard a famous quote by Bjarne Stroustroup (creator of C++)  &#8220;C makes it easy to shoot yourself in the foot.&#8221; What he might mean by this statement, at least partially anyway, is that as a C programmer, you&#8217;re responsible for allocation\/de-allocation of memory in your own program. Often people new to C\/C++ are not familiar with the ins and outs of memory allocation, specifically the program stack and heap.<\/p>\n\n\n\n<p>The &#8220;corrupted size vs. prev_size&#8221; message we see above is a symptom of heap corruption, which is when a program attempts to write too many blocks of data to an area of memory with a limited capacity. It seems that we&#8217;re writing too much data to an area of memory, so we&#8217;ll likely have to visually inspect our code again to attempt and rectify this issue.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A last resort<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/media.cntraveler.com\/photos\/53da60a46dec627b149e66f4\/master\/pass\/hilton-moorea-lagoon-resort-spa-moorea-french-poly--110160-1.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<p>And if our team can&#8217;t seem to figure out how this heap corruption issue is occurring by visually inspecting the code?<\/p>\n\n\n\n<p>There might be a way out of that. Since we know that there are working Space Invaders projects, we can attempt a crude, but possibly effective technique for debugging our code. We can print the status of the CPU for our own project, say for the first 2000 instructions, and print them to a file. We do the same for a known working project. In particular, we print all the register, flag, and SP values. If we see a discrepancy between the two files, we can see where it occurs and inspect the surrounding instructions to ensure things are working as they should.<\/p>\n\n\n\n<p>The nice thing about this technique is that we might not even have to do a visual inspection of the code to find any discrepancies; there are command-line utilities that do a character-by-character analysis of two files, and can highlight differences between those two files. A crude technique possibly, but it might end up pointing our team in the right direction.<\/p>\n\n\n\n<p>Our goal for the following week is to continue debugging until our game works flawlessly in black\/white mode. Once we have that working, we&#8217;ll be adding color and audio to our game, which hopefully we can discuss next week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It works. Kind of? There comes a point in every programmer&#8217;s education\/career where, after spending weeks writing and testing code, one excitedly compiles and runs their code, expecting great things. Often, there seems to be a bug that is causing the program to behave unexpectedly and worse, in the worst case, it seems impossible to [&hellip;]<\/p>\n","protected":false},"author":12287,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/users\/12287"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/posts\/37\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/ettucode\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}