;========================================================== ; 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: ; ; quot = 0; ; cin >> divor; ; cin >> dend; ; while ( dend >= divor ) { ; dend = dend - divor; ; quot++; ; } ; cout << quot; ; cout << dend; ; ; LMC-style Pseudocode for the program, using "skip" and "jump": ; ; quota = 0 ; read input into divor ; read input into dend ; WHILE: calculate dend - divor (to set LMC lights) ; if ( positive light is on ) skip next instruction ; jump to ENDWH (jump over WHILE body) ; dend = dend - divor ; quot++ ; jump to WHILE ; ENDWH: output quot ; output dend ; halt ; ; If we create this LMC Input (Inbox) Queue for "IN" statements: ; 100 301 ; ; Program output (using 3-digit odometer math): ; 003 and 001 ; ; 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 ; } /* ENDWH */ ; 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 ; variable divor DEND DAT ; variable dend QUOT DAT ; variable quot ------------------------------------------------------------------------------- Assembly Language "Listing" file showing mailboxes (MB) and Machine Code: MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 123 : LDA ZERO ; initialize quotient subtraction counter 01 226 : STO QUOT 02 500 : IN ; read and store divisor (first number) 03 224 : STO DIVOR 04 500 : IN ; read and store dividend (second number) 05 225 : STO DEND 06 125 : WHILE LDA DEND ; see if dividend is >= than divisor 07 424 : SUB DIVOR 08 802 : SKP ; skip *into* loop if dividend >= divisor 09 917 : JMP ENDWH ; jump out of loop when dividend < divisor 10 125 : LDA DEND ; subtract divisor from dividend 11 424 : SUB DIVOR 12 225 : STO DEND 13 126 : LDA QUOT ; quot counts number of subtractions 14 322 : ADD ONE 15 226 : STO QUOT 16 906 : JMP WHILE ; WHILE loop: keep going until nothing left 17 126 : ENDWH LDA QUOT ; output quotient 18 600 : OUT 19 125 : LDA DEND ; output remainder 20 600 : OUT 21 700 : HLT 22 001 : ONE DAT 001 ; constant 1 23 000 : ZERO DAT 000 ; constant 0 24 000 : DIVOR DAT ; variable divor 25 000 : DEND DAT ; variable dend 26 000 : QUOT DAT ; variable quot Label Table (created during first pass through source code): Label Memory Location ------- --------------- WHILE at 06 ENDWH at 17 ONE at 22 ZERO at 23 DIVOR at 24 DEND at 25 QUOT at 26 -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/