// Little Man Computer, for Rosetta Code. // Calkin-Wilf sequence: displays index of term entered by user. INP // get numerator from user BRZ exit // exit if 0 STA num STA a // initialize a := numerator INP // get denominator from user BRZ exit // exit if 0 STA den STA b // initialize b := denominator LDA c0 // initialize index := 0 STA index LDA c1 // initialize power of 2 := 1 STA pwr2 // Build binary digits of the index loop LDA a // is a = b yet? SUB b BRZ break // if yes, break out of loop BRP a_gt_b // jump if a > b // If a < b then b := b - a, binary digit is 0 LDA b SUB a STA b BRA double // If a > b then a := a - b, binary digit is 1 a_gt_b STA a LDA index ADD pwr2 STA index // In either case, on to next power of 2 double LDA pwr2 ADD pwr2 STA pwr2 BRA loop // Out of loop, add leading binary digit 1 break LDA index ADD pwr2 STA index // Output the result LDA num OTX 1 // non-standard: minimum width = 1, no new line LDA asc_sl OTC LDA den OTX 1 LDA asc_lt // write '<-' after fraction OTC LDA asc_hy OTC LDA index OTX 11 // non-standard: minimum width = 1, add new line exit HLT // Constants c0 DAT 0 c1 DAT 1 asc_sl DAT 47 asc_lt DAT 60 asc_hy DAT 45 // Variables num DAT den DAT a DAT b DAT pwr2 DAT index DAT // end