Task

Create and display the sequence of binary digits for a given   non-negative integer.

   The decimal value      5   should produce an output of               101
   The decimal value     50   should produce an output of            110010
   The decimal value   9000   should produce an output of    10001100101000

The results can be achieved using built-in radix functions within the language   (if these are available),   or alternatively a user defined function can be used.

The output produced should consist just of the binary digits of each number followed by a   newline.

There should be no other whitespace, radix or sign markers in the produced output, and leading zeros should not appear in the results.




 
// Little Man Computer, for Rosetta Code.
// Read numbers from user and display them in binary.
// Exit when input = 0.
input    INP
         BRZ zero
         STA N
// Write number followed by '->'
         OUT
         LDA asc_hy
         OTC
         LDA asc_gt
         OTC
// Find greatest power of 2 not exceeding N,
//  and count how many digits will be output
         LDA c1
         STA pwr2
loop     STA nrDigits
         LDA N
         SUB pwr2
         SUB pwr2
         BRP double
         BRA part2    // jump out if next power of 2 would exceed N
double   LDA pwr2
         ADD pwr2
         STA pwr2
         LDA nrDigits
         ADD c1
         BRA loop
// Write the binary digits
part2    LDA N
         SUB pwr2
set_diff STA diff
         LDA asc_1     // first digit is always 1
wr_digit OTC           // write digit
         LDA nrDigits  // count down the number of digits
         SUB c1
         BRZ input     // if all digits done, loop for next number
         STA nrDigits
// We now want to compare diff with pwr2/2.
// Since division is awkward in LMC, we compare 2*diff with pwr2.
         LDA diff      // diff := diff * 2
         ADD diff
         STA diff      
         SUB pwr2      // is diff >= pwr2 ?
         BRP set_diff  // yes, update diff and write '1'
         LDA asc_0     // no, write '0'
         BRA wr_digit
zero     HLT           // stop if input = 0
// Constants
c1       DAT 1
asc_hy   DAT 45
asc_gt   DAT 62
asc_0    DAT 48
asc_1    DAT 49
// Variables
N        DAT
pwr2     DAT
nrDigits DAT
diff     DAT
 