{"id":5477,"date":"2014-03-30T22:29:29","date_gmt":"2014-03-30T22:29:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-one-implement-printf-like-functionality-in-assembler-collection-of-common-programming-errors\/"},"modified":"2014-03-30T22:29:29","modified_gmt":"2014-03-30T22:29:29","slug":"how-can-one-implement-printf-like-functionality-in-assembler-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-one-implement-printf-like-functionality-in-assembler-collection-of-common-programming-errors\/","title":{"rendered":"How can one implement printf-like functionality in assembler?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m very much just looking for feedback and guidance rather than a direct answer.<\/p>\n<p>Using the <code>.586<\/code> assembler instructions, how can you simulate printf-like functionality? For fixed width fields, I know I could do something like this:<\/p>\n<pre><code>.data\n  Format BYTE 'You are ## years old.',0\n<\/code><\/pre>\n<p>And then before printing the string, simply replace the ## values to the appropriate number, changing them back afterwards in case I need to use the format string multiple times with different values.<\/p>\n<p>This might not be the best way, but for now it works.<\/p>\n<p>What I can&#8217;t figure out is how to do it if you don&#8217;t know the number of digits ahead of time. The format option is okay if you know the maximum number of digits you want to use (above example won&#8217;t work pretty-like for people 100 years old and older, you lose the spaces between words).<\/p>\n<p>If you reserve too much space though, you get double spaces between your number and your worlds.<\/p>\n<pre><code>.data\n  Format BYTE 'You are ### years old.',0\n<\/code><\/pre>\n<p>Using my scheme with someone who is 12 would produce:<\/p>\n<pre><code>You are  12 years old.\n<\/code><\/pre>\n<p>My only thought was is there happened to be an ASCII character which printed nothing (aside from <code>\\0<\/code>), but it seems tacky.<\/p>\n<p>The whole problem arises because the instructor wants us to be able to print this type of formatted string using a single call to a PrintString procedure (which he provided us) which looks for <code>esi<\/code> to be set to a <code>\\0<\/code> terminated byte array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m very much just looking for feedback and guidance rather than a direct answer. Using the .586 assembler instructions, how can you simulate printf-like functionality? For fixed width fields, I know I could do something like this: .data Format BYTE &#8216;You are ## years old.&#8217;,0 And then before printing the string, simply replace the ## [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5477","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5477"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5477\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}