{"id":52,"date":"2008-02-13T19:12:03","date_gmt":"2008-02-13T21:12:03","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=52"},"modified":"2008-02-13T19:12:03","modified_gmt":"2008-02-13T21:12:03","slug":"converting-an-integer-to-decimal-assembly-style","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=52","title":{"rendered":"Converting An Integer To Decimal &#8211; Assembly Style"},"content":{"rendered":"<p>I know this is one of the most trivial things to implement in a high level language.  In C it goes like this:<\/p>\n<p>void print_integer(unsigned int x)<br \/>\n{<br \/>\nif ((x \/ 10) != 0) pi(x \/ 10);<br \/>\nputch((x % 10) + &#8216;0&#8217;);<br \/>\n}<\/p>\n<p>The annoying thing is that you have to div twice and do a modulo once. Which in reality can be merged into a single X86 instruction. Another thing is that if you want to be able to print a normal result for an input of 0 you will have to test the result of the division instead of checking simply x itself. The conversion is done from the least significant digit to the most. But when we display the result (or put it in a buffer) we have to reverse it. Therefore the recursion is so handy here. This is my go in 16bit, it&#8217;s a code that I wrote a few years ago, and I just decided I should put it here for a reference. I have to admit that I happened to use this same code for also 32bits or even different processors and since it&#8217;s so elegant it works so well and easy to port. But I leave it for you to judge ;)<\/p>\n<p>bits 16<br \/>\nmov ax, 12345<br \/>\ncall print_integer<br \/>\nxor ax, ax<br \/>\ncall print_integer<br \/>\nret<\/p>\n<p>print_integer:<br \/>\n; base 10<br \/>\npush byte 10<br \/>\npop bx<br \/>\n.next:; make a 32 bits division, remainder in dx, quotient in ax<br \/>\nxor dx, dx<br \/>\ndiv bx<br \/>\npush dx ; push remainder<br \/>\nor ax, ax ; if the result if 0, we will stop recursing<br \/>\njz .stop<br \/>\ncall .next ; now this is the coolest twist ever, the IP that is pushed onto the stack&#8230;<br \/>\n.stop:<br \/>\npop ax ; get the remainder (in reversed order)<br \/>\nadd al, 0x30 ; convert it to a character<br \/>\nint 0x29 ; use (what used to be an undocumented) interrupt to print al<br \/>\nret ; go back to &#8216;stop&#8217; and read the next digit&#8230;<\/p>\n<p>I urge you to compile the C code with full optimization and compare the codes for yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know this is one of the most trivial things to implement in a high level language. In C it goes like this: void print_integer(unsigned int x) { if ((x \/ 10) != 0) pi(x \/ 10); putch((x % 10) + &#8216;0&#8217;); } The annoying thing is that you have to div twice and do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[21,5],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-Q","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52"}],"version-history":[{"count":0,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}