{"id":801,"date":"2011-10-28T23:40:33","date_gmt":"2011-10-29T03:40:33","guid":{"rendered":"http:\/\/pappp.net\/?p=801"},"modified":"2011-10-28T23:40:33","modified_gmt":"2011-10-29T03:40:33","slug":"a-day-in-the-life-of-the-kaos-lab-thought-process","status":"publish","type":"post","link":"https:\/\/pappp.net\/?p=801","title":{"rendered":"A Day in the Life of the KAOS Lab Thought Process"},"content":{"rendered":"<p>In which a simple &#8220;Do you know an easy way to convert an integer into a string of (character) digits?&#8221; turned into an expedition into ancient UNIX codebases. The process went something like this:<\/p>\n<p>Labmate: Do you know an easy way to convert an integer into a string of digits?<br \/>\n&#060; both of us type it in to google&#062;<br \/>\nLooks like sprintf() is best, there must be a simple efficent algorithm.<br \/>\nHow does printf() do it?<br \/>\nLet&#8217;s look in libc!<br \/>\n&#060; grep through the various toolchain sources on my system &#062;<br \/>\nWell, here&#8217;s the uClibc implementation&#8230; which is a terrifying mess of ambigously named function calls and preprocessor directives.<br \/>\nI wish I had some old UNIX sources to look at, that would be simple.<br \/>\n&#060; a bit of googling later&#062;<br \/>\nHoly crap, <a href=\"http:\/\/minnie.tuhs.org\/cgi-bin\/utree.pl\">this<\/a> is amazing! Full root images for a bunch of early UNIXes, many of them from dmr himself!<br \/>\n&#060; download v5, grep \/usr\/source judiciously to find \/usr\/source\/s4\/printf.s&#062;<br \/>\nWell crap, it&#8217;s in PDP-11 assembly, maybe a later version.<br \/>\n&#060; download v7, grep \/usr\/src judiciously to find \/usr\/src\/libc\/stdio\/doprnt.s&#062;<br \/>\nDamn, still in PDP-11 assembly, but this is a fancier algorithm.<br \/>\nHmm&#8230; the most understandable UNIX I&#8217;ve ever looked at was old <a href=\"http:\/\/www.minix3.org\/previous-versions\/index.html\">MINIX<\/a>&#8230;<br \/>\n&#060; spin up BasiliskII, in &#8216;030 mode, use <a href=\"http:\/\/www.pliner.com\/macminix\/32bitworkaround.html\">this<\/a> workaround to make MacMinix run&#062;<br \/>\n&#060; more grepping for justice&#062;<br \/>\nEventually, we found the printk() from MacMinix 1.5, in all its awful K&#038;R\/ANSI transitional C glory<br \/>\n<code><br \/>\n...<br \/>\n#define NO_FLOAT<\/p>\n<p>#ifdef NO_FLOAT<br \/>\n#define MAXDIG       11 \/*32 bits in radix 8*\/<br \/>\n#else<br \/>\n#define MAXDIG       128 \/* this must be enough *\/<br \/>\n#endif<\/p>\n<p>_PROTOTYPE(void putc, (int ch));  \/*user-supplied, should be putk *\/<\/p>\n<p>PRIVATE _PROTOTYPE( char *_itoa, (char *p, unsigned num, int radix));<br \/>\n#ifndef NO_LONGD<br \/>\nPRIVATE _PROTOTYPE( char *_itoa, (char *p, unsigned long num, int radix));<br \/>\n#endif<\/p>\n<p>PRIVATE char *_iota(p, num, radix)<br \/>\nregister char *p;<br \/>\nregister unsigned num;<br \/>\nregister radix;<br \/>\n {<br \/>\n  register i;<br \/>\n  register char *q;<br \/>\n  q = p + MAXDIG;<br \/>\n  do {<br \/>\n\t\ti = (int) (num % radix);<br \/>\n\t\ti += '0';<br \/>\n\t\tif (i > '9') i += 'A' - '0' - 10;<br \/>\n\t\t*--q = i;<br \/>\n\t } while (num = num \/ radix);<br \/>\n  i = p + MAXDIG - q;<br \/>\n  do<br \/>\n\t\t*p++ = *q++;<br \/>\n  while(--i);<br \/>\n  return(p);<br \/>\n }<br \/>\n...<br \/>\n<\/code>\t<\/p>\n<p>Which is, of course, a digit at a time in one of the most straightforward ways imaginable. Minix&#8217;s kernel is designed for systems so resource constrained it has a separate prints() that can only handle char and char* to save overhead, so  I can&#8217;t imagine it uses a sub-optimal technique.<br \/>\nThis kind of thing really makes me wish I had learned OSes in the old death-march through the UNIX sources (or at least the old Tenenbaum book with MINIX) way; things are too complicated and opaque now.  There always seems to me to be a golden age from around 1970 into the early 1990s where the modern computing abstractions were in place, but the complexity of production hardware and software hadn&#8217;t yet grown out of control.<br \/>\nIn a related note, as a tool writer, looking at the earliest versions of cc is AMAZING.  Most decent programmers should be able to work through the cc from v5 UNIX (574 lines of C for cc proper, 6454 more lines of C and 1606 of PDP-11 assembly in called parts) in a couple hours, and fairly fully understand how it all works.  Sadly, (pre-3) MINIX  came with a (binary only) CC made with <a href=\"http:\/\/en.wikipedia.org\/wiki\/Amsterdam_Compiler_Kit\">ACK<\/a>, which is fancy and portable and way, way, way harder to understand. dmr&#8217;s simple genius was just that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In which a simple &#8220;Do you know an easy way to convert an integer into a string of (character) digits?&#8221; turned into an expedition into ancient UNIX codebases. The process went something like this: Labmate: Do you know an easy &hellip; <a href=\"https:\/\/pappp.net\/?p=801\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6,1,3],"tags":[169,78,170],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","category-computers","category-entertainment","category-general","category-school","tag-compiler","tag-kaos","tag-unix"],"_links":{"self":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=801"}],"version-history":[{"count":0,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/801\/revisions"}],"wp:attachment":[{"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}