;========================================================== ; LMC Sample Program #5 - WHILE loop quotient and remainder ;========================================================== ;- Ian! D. Allen - idallen@idallen.ca - www.idallen.com ; ; A program to output a quotient and a remainder, given a ; divisor and dividend read as input. ; ; C++ algorithm: ; ; int divor; ; int dend; ; int quot; ; quot = 0; ; cin >> divor; ; cin >> dend; ; while ( dend >= divor ) { ; dend = dend - divor; ; quot++; ; } ; cout << quot; ; cout << dend; ; ; Corresponding high-level statements precede each LMC code block: ; ;Label Mnem. Operand Comments............. ;----- ----- ------- --------------------- ; quot = 0; LDA ZERO ; initialize quotient subtraction counter STO QUOT ; cin >> divor; IN ; read and store divisor (first number) STO DIVOR ; cin >> dend; IN ; read and store dividend (second number) STO DEND ; while ( dend >= divor ) { WHILE LDA DEND ; see if dividend is >= than divisor SUB DIVOR SKP ; skip *into* loop if dividend >= divisor JMP ENDWH ; jump out of loop when dividend < divisor ; dend = dend - divor; LDA DEND ; subtract divisor from dividend SUB DIVOR STO DEND ; quot++; LDA QUOT ; quot counts number of subtractions ADD ONE STO QUOT JMP WHILE ; WHILE loop: keep going until nothing left ; } ; cout << quot; ENDWH LDA QUOT ; output quotient OUT ; cout << dend; LDA DEND ; output remainder OUT HLT ; constants used in this program: ONE DAT 001 ; constant 1 ZERO DAT 000 ; constant 0 ; variables used in this program: DIVOR DAT ? ; int divor; // divisor DEND DAT ? ; int dend; // dividend QUOT DAT ? ; int quot; // quotient