{"id":62,"date":"2013-04-01T00:35:12","date_gmt":"2013-04-01T00:35:12","guid":{"rendered":"https:\/\/www.softwareab.net\/wordpress\/?p=62"},"modified":"2013-04-09T11:26:56","modified_gmt":"2013-04-09T11:26:56","slug":"the-worlds-smallest-useful-program","status":"publish","type":"post","link":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/","title":{"rendered":"The World&#8217;s Smallest (Useful!) Program :)"},"content":{"rendered":"<p>Thought I&#8217;d share a funny little program with you from back in 1986. Here it is: to my knowledge the world&#8217;s smallest (useful) IBM PC computer program:<\/p>\n<pre>PUSH\u00c2\u00a0 AX\nPOP\u00c2\u00a0\u00c2\u00a0 DS\nXOR\u00c2\u00a0\u00c2\u00a0 BYTE PTR [16], 7\nRETF<\/pre>\n<p><!--more-->That computer program in x86 assembler is exactly 9 bytes long and actually does do something useful\u00e2\u20ac\u201d it toggles the print screen function (if it&#8217;s on, it turns it off and vice versa). It works based on some analysis of the IBM PC ROM BIOS listing \u00e2\u20ac\u201c at memory location <code>F000:FF53<\/code> there is an <code>IRET<\/code> instruction (return from interrupt). At <code>F000:FF54<\/code> there is the beginning of the PRTSC interrupt handler. So, think about that \u00e2\u20ac\u201c we have <code>FF53<\/code> and <code>FF54<\/code>; the last nibble of the octet is either 3 or 4. Furthermore, the interrupt table at the top of memory (<code>0000:0000<\/code>) contains at position 5 (hex positions <code>0014<\/code> to <code>0017<\/code>) the address of the PRTSC interrupt handler (<code>00 F0 54 FF<\/code> in little-endian notation). So to toggle print screen&#8230;if we could just change that \u00e2\u20ac\u015354\u00e2\u20ac\u009d to be \u00e2\u20ac\u015353\u00e2\u20ac\u009d (the address of the <code>IRET<\/code> function of the preceding interrupt vector) we&#8217;d have it done.<\/p>\n<p>The code above works by using lots of tricks: the <code>AX<\/code> &#8220;accumulator&#8221; register is always zero when a DOS command file (.COM extension) runs, and the <code>DS<\/code> data segment register is what you use to manipulate memory. So, to zero out the <code>DS<\/code> register (to access the top of memory where the PRTSC interrupt is) we push <code>AX<\/code> onto the stack and pop <code>DS<\/code> off. This is equivalent to assigning AX to DS directly\u00e2\u20ac\u201dbut (even though it is <b>two<\/b> instructions) it is smaller (one byte each for the <code>PUSH<\/code> \/ <code>POP<\/code> vs. <i>three<\/i> bytes for the equivalent <code>MOV DS, AX<\/code>). This was a revelation to me\u00e2\u20ac\u201dat a very basic level sometimes you really do choose size (the two instructions, that require four cycles) vs. speed (the single <code>MOV<\/code> command that uses three cycles but takes up three bytes on disk).<\/p>\n<p>Then we use the <code>XOR<\/code> command to modify the data byte at offset 0x16 (that&#8217;s hexadecimal; 22 decimal). We XOR whatever was in the data byte with a 7. Let&#8217;s look at what happens:<\/p>\n<pre>     3\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 0011\nXOR\u00c2\u00a0 7\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 0111\n    --\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 ----\n     4\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 0100<\/pre>\n<p>And, going the other way&#8230;<\/p>\n<pre>     4\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 0100\nXOR\u00c2\u00a0 7\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 0111\n    --\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0----\n     3\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 0011<\/pre>\n<p>By XOR&#8217;ing with a 7, we are essentially switching the values between 3 and 4.<\/p>\n<p>Finally, the program uses a little trick of MS-DOS. (Remember, MS-DOS was initially written for CP\/M compatibility, just do a search for INT 21 FUNC 09 for a view of how CP\/M allowed strings to be printed to the screen.) For badly-written programs (which ties back to Norwhich University&#8217;s Dr. Mich Kabay&#8217;s comments on the great unwashed horde in the 80s writing any old garbage and putting it onto a machine), it wasn&#8217;t unusual for programs in CP\/M simply to have a <code>RETF<\/code> (far return) without being wrapped in a <code>CALL<\/code> statement. Effectively, CP\/M translated this as &#8220;return me to the operating system NOW&#8221; which effectively terminates the running program. Bill Gates (really, Tim Paterson) put in the same provision for DOS so that instead of me writing the accepted <code>INT 20<\/code> (IBM PC ROM BIOS standard end program, took two bytes) or even more standards-based <code>MOV AH, 4E; INT 21<\/code> (the best-practices way to end a program; but at a cost of <i>4 bytes<\/i>; my gosh, did they think we all had 10MB hard disks??) all I had to do was a simple one-byte <code>RETF<\/code> and my program would end. I got to save at least a byte of space!<\/p>\n<p>Here&#8217;s top of memory prior to doing the run:<\/p>\n<pre>0000:0010\u00c2\u00a0 XX XX XX XX 00 F0 54 FF [ ... other interrupt vectors ... ]<\/pre>\n<p>After running it once:<\/p>\n<pre>0000:0010\u00c2\u00a0 XX XX XX XX 00 F0 53 FF [ ... other interrupt vectors ... ]<\/pre>\n<p>And after running it again:<\/p>\n<pre>0000:0010\u00c2\u00a0 XX XX XX XX 00 F0 54 FF [ ... other interrupt vectors ... ]<\/pre>\n<p>Very amusing; to quote Homer Simpson: &#8220;Print screen goes off, print screen goes on. Print screen goes off, print screen goes on.&#8221; And all in 8 bytes (not counting the <code>RETF<\/code>).<\/p>\n<p>If the machine wasn&#8217;t <i>strictly compatible<\/i> with IBM PC ROM BIOS then my little gem wouldn&#8217;t work and would in fact have a real possibility of crashing the system. Well, you can&#8217;t make an omelet without breaking a few eggs!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thought I&#8217;d share a funny little program with you from back in 1986. Here it is: to my knowledge the world&#8217;s smallest (useful) IBM PC computer program: PUSH\u00c2\u00a0 AX POP\u00c2\u00a0\u00c2\u00a0 DS XOR\u00c2\u00a0\u00c2\u00a0 BYTE PTR [16], 7 RETF<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The World&#039;s Smallest (Useful!) Program :) - softwareab<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The World&#039;s Smallest (Useful!) Program :) - softwareab\" \/>\n<meta property=\"og:description\" content=\"Thought I&#8217;d share a funny little program with you from back in 1986. Here it is: to my knowledge the world&#8217;s smallest (useful) IBM PC computer program: PUSH\u00c2\u00a0 AX POP\u00c2\u00a0\u00c2\u00a0 DS XOR\u00c2\u00a0\u00c2\u00a0 BYTE PTR [16], 7 RETF\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/\" \/>\n<meta property=\"og:site_name\" content=\"softwareab\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cloudraticsolutions\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/cloudraticsolutions\/\" \/>\n<meta property=\"article:published_time\" content=\"2013-04-01T00:35:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-04-09T11:26:56+00:00\" \/>\n<meta name=\"author\" content=\"Andrew Bruce\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@realcloudratics\" \/>\n<meta name=\"twitter:site\" content=\"@realcloudratics\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrew Bruce\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/\"},\"author\":{\"name\":\"Andrew Bruce\",\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600\"},\"headline\":\"The World&#8217;s Smallest (Useful!) Program :)\",\"datePublished\":\"2013-04-01T00:35:12+00:00\",\"dateModified\":\"2013-04-09T11:26:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/\"},\"wordCount\":621,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600\"},\"articleSection\":[\"Teknocratica\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/\",\"url\":\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/\",\"name\":\"The World's Smallest (Useful!) Program :) - softwareab\",\"isPartOf\":{\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#website\"},\"datePublished\":\"2013-04-01T00:35:12+00:00\",\"dateModified\":\"2013-04-09T11:26:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.softwareab.net\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The World&#8217;s Smallest (Useful!) Program :)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#website\",\"url\":\"https:\/\/www.softwareab.net\/wordpress\/\",\"name\":\"softwareab\",\"description\":\"Technocratica, Technopolitik, Technophobia\",\"publisher\":{\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.softwareab.net\/wordpress\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600\",\"name\":\"Andrew Bruce\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.softwareab.net\/wordpress\/wp-content\/uploads\/2024\/03\/andy-cartoon.jpg\",\"contentUrl\":\"https:\/\/www.softwareab.net\/wordpress\/wp-content\/uploads\/2024\/03\/andy-cartoon.jpg\",\"width\":400,\"height\":330,\"caption\":\"Andrew Bruce\"},\"logo\":{\"@id\":\"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/image\/\"},\"description\":\"Team-oriented systems mentor with deep knowledge of numerous software methodologies, technologies, languages, and operating systems. Excited about turning emerging technology into working production-ready systems. Focused on moving software teams to a higher level of world-class application development. Specialties:Software analysis and development...Product management through the entire lifecycle...Discrete product integration specialist!\",\"sameAs\":[\"http:\/\/cloudraticsolutions.net\/\",\"https:\/\/www.facebook.com\/cloudraticsolutions\/\",\"https:\/\/twitter.com\/realcloudratics\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The World's Smallest (Useful!) Program :) - softwareab","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/","og_locale":"en_US","og_type":"article","og_title":"The World's Smallest (Useful!) Program :) - softwareab","og_description":"Thought I&#8217;d share a funny little program with you from back in 1986. Here it is: to my knowledge the world&#8217;s smallest (useful) IBM PC computer program: PUSH\u00c2\u00a0 AX POP\u00c2\u00a0\u00c2\u00a0 DS XOR\u00c2\u00a0\u00c2\u00a0 BYTE PTR [16], 7 RETF","og_url":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/","og_site_name":"softwareab","article_publisher":"https:\/\/www.facebook.com\/cloudraticsolutions\/","article_author":"https:\/\/www.facebook.com\/cloudraticsolutions\/","article_published_time":"2013-04-01T00:35:12+00:00","article_modified_time":"2013-04-09T11:26:56+00:00","author":"Andrew Bruce","twitter_card":"summary_large_image","twitter_creator":"@realcloudratics","twitter_site":"@realcloudratics","twitter_misc":{"Written by":"Andrew Bruce","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#article","isPartOf":{"@id":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/"},"author":{"name":"Andrew Bruce","@id":"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600"},"headline":"The World&#8217;s Smallest (Useful!) Program :)","datePublished":"2013-04-01T00:35:12+00:00","dateModified":"2013-04-09T11:26:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/"},"wordCount":621,"commentCount":0,"publisher":{"@id":"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600"},"articleSection":["Teknocratica"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/","url":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/","name":"The World's Smallest (Useful!) Program :) - softwareab","isPartOf":{"@id":"https:\/\/www.softwareab.net\/wordpress\/#website"},"datePublished":"2013-04-01T00:35:12+00:00","dateModified":"2013-04-09T11:26:56+00:00","breadcrumb":{"@id":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.softwareab.net\/wordpress\/the-worlds-smallest-useful-program\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.softwareab.net\/wordpress\/"},{"@type":"ListItem","position":2,"name":"The World&#8217;s Smallest (Useful!) Program :)"}]},{"@type":"WebSite","@id":"https:\/\/www.softwareab.net\/wordpress\/#website","url":"https:\/\/www.softwareab.net\/wordpress\/","name":"softwareab","description":"Technocratica, Technopolitik, Technophobia","publisher":{"@id":"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.softwareab.net\/wordpress\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/1337443eaeb75104e0410b508e67f600","name":"Andrew Bruce","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/www.softwareab.net\/wordpress\/wp-content\/uploads\/2024\/03\/andy-cartoon.jpg","contentUrl":"https:\/\/www.softwareab.net\/wordpress\/wp-content\/uploads\/2024\/03\/andy-cartoon.jpg","width":400,"height":330,"caption":"Andrew Bruce"},"logo":{"@id":"https:\/\/www.softwareab.net\/wordpress\/#\/schema\/person\/image\/"},"description":"Team-oriented systems mentor with deep knowledge of numerous software methodologies, technologies, languages, and operating systems. Excited about turning emerging technology into working production-ready systems. Focused on moving software teams to a higher level of world-class application development. Specialties:Software analysis and development...Product management through the entire lifecycle...Discrete product integration specialist!","sameAs":["http:\/\/cloudraticsolutions.net\/","https:\/\/www.facebook.com\/cloudraticsolutions\/","https:\/\/twitter.com\/realcloudratics"]}]}},"_links":{"self":[{"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":4,"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/posts\/62\/revisions\/82"}],"wp:attachment":[{"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareab.net\/wordpress\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}