title: Convert Int to binary in Assembly (pep8) - An elegant approach
body: <p>I am writing a program for one of my classes in assembly(pep8).  The program is supposed to take in an integer between -32,768 and 32,767, and output its binary equivalent.  The code below accomplishes the task except it prints the binary number backwards.</p>
  
  <p>Is there an elegant way for me to print the binary number in the correct order (ie I DONT want to store every binary digit in a local variable and then print them in reverse order).</p>
  
  <pre><code>        BR main
  int:             .EQUATE 0                ;local variable int
  out:             .EQUATE 2                ;local variable out
  index:           .EQUATE 4                ;local variable index
  stack:           .EQUATE 6                ;local variable for dynamic memory allocation
  
  
  
  main:            SUBSP       stack,i      ;allocate memmory
                   LDA         0,i          
                   STA         index,s      ;initialize index
                   DECI        int,s        ;get user input and store it in num 
  while:           LDA         index,s      ;while (num &lt;= 16)
                   CPA         16,i         ;
                   BRGE        endWhile     ;
                   ADDA        1,i          ;index++
                   STA         index,s      ;
                   LDA         int,s        ;
                   ANDA        0x0001,i     ;modulus 2 "%2"
                   STA         out,s        ;
                   DECO        out,s        ;output results 
                   LDA         int,s        ;
                   ASRA                     ;devide num by 2
                   STA         int,s        ;
  
                   BR          while        ;
  endWhile:        ADDSP       stack,i      ;deallocate memmory
                   STOP
                   .END
  </code></pre>