title: Optimizing DCPU-16 FizzBuzz
body: <p>I tried to implement FizzBuzz in DCPU-16. I use this web emulator: <a href="http://mappum.github.com/DCPU-16/" rel="nofollow">http://mappum.github.com/DCPU-16/</a> (repository: <a href="https://github.com/mappum/DCPU-16" rel="nofollow">https://github.com/mappum/DCPU-16</a>).</p>
  
  <p>It stops before the end of the loop. Why?</p>
  
  <p>How can I optimize it?
  I'm a high level language programmer who don't know anything about asm. Have you some <em>basic</em> resource on this?</p>
  
  <p>More infos on DCPU-16: official specs: <a href="http://0x10c.com/doc/dcpu-16.txt" rel="nofollow">http://0x10c.com/doc/dcpu-16.txt</a> - unofficial specs: <a href="http://0x10.cc/" rel="nofollow">http://0x10.cc/</a></p>
  
  <p>The code:</p>
  
  <pre><code>; FizzBuzz in DCPU-16
  
  SET Z, 0x8000
  SET I, 1
  
  
  :loop
      JSR print
      ADD I, 1
      IFN I, 100
          SET PC, loop
  
      BRK
  
  
  :print
      ADD Z, 1
      SET [Z], 10
  
      SET A, I
      MOD A, 3
      IFE A, 0
          JSR printFizz
  
      SET B, I
      MOD B, 5
      IFE B, 0
          JSR printBuzz
  
      SET C, A
      IFG B, A
          SET C, B
      ADD A, B
      IFG A, C
          JSR printNumber
  
      SET PC, POP
  
  
  :printNumber
      SET A, I
  
      IFG I, 9
          DIV A, 10
  
      JSR printDigit
  
      IFG 10, I
          SET PC, POP
  
      SET A, I
      MOD A, 10
  
      JSR printDigit
      SET PC, POP
  
  :printDigit
      ADD A, 48
      ADD Z, 1
      SET [Z], A
      SET PC, POP
  
  :printFizz
      ADD Z, 1
      SET [Z], 70
      ADD Z, 1
      SET [Z], 105
      ADD Z, 1
      SET [Z], 122
      ADD Z, 1
      SET [Z], 122
      SET PC, POP
  
  :printBuzz
      ADD Z, 1
      SET [Z], 66
      ADD Z, 1
      SET [Z], 117
      ADD Z, 1
      SET [Z], 122
      ADD Z, 1
      SET [Z], 122
      SET PC, POP
  </code></pre>