// 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