;============================================= ; LMC Sample Program #3 - input, add, and loop ;============================================= ; - Ian! D. Allen - idallen@idallen.ca - www.idallen.com ; ; We will use this 6-column format for all LMC programs. ; (We won't use the Label column yet.) ; Start by filling in the Mnemonic and Operand columns, then go back ; and "hand-assemble" the mnemonics into actual numbers in mailboxes. ; ; The "DAT" (data) mnemonic is not an LMC instruction. It is used by ; assembly language programmers to indicate that the given operand is data ; to be placed in memory at the current memory location. ; ; Pseudocode: ; ; one = 1 ; this value is initialized in LMC memory ; sum = 0 ; this value is initialized in LMC memory ; counter = 3 ; this value is initialized in LMC memory ; loop: read tmp ; sum = sum + tmp ; counter = counter - one ; if ( zero light is on ) goto skip: ; goto loop ; skip: output sum ; ; Using more structured code: ; ; sum = 0; /* this value is initialized in LMC memory */ ; counter = 3; /* this value is initialized in LMC memory */ ; do { ; cin >> tmp; ; sum = sum + tmp; ; } while ( --counter != 0 ); ; cout << sum; ; ; LMC Input Queue (for "IN" statements): ; 324 ; 119 ; 041 ; ; Output: 324 + 119 + 041 = 484 (using 3-digit odometer math) ; ;Loc. Code Label Mnemon. Operand Comments ;---- ---- ----- ------- ------- ------------------------- 00 500 IN ; input a number 01 312 ADD 12 ; add sum to number 02 212 STO 12 ; store back into sum 03 113 LDA 13 ; load counter 04 411 SUB 11 ; subtract one 05 213 STO 13 ; store back in counter 06 801 SKZ ; exit loop if counter is zero 07 900 JMP 00 ; go to top of loop 08 112 LDA 12 ; load sum 09 600 OUT ; output sum 10 700 HLT ; put LMC to sleep (stop execution) 11 001 DAT 001 ; constant 1 12 000 DAT 000 ; sum variable 13 003 DAT 003 ; counter variable 14 500 DAT 500 ; unused memory (junk) ; Below is the same program written using labels instead of hard-coding ; all of the memory locations. Using labels makes writing the program ; easier, since we don't care where in memory things get put. Turning ; a program with labels into numeric machine code is harder, since we ; now have to make two passes through the code: The first pass writes ; down the operation codes (opcodes) for each instruction and the memory ; locations of all the labels; the second pass writes down ("back plugs") ; the actual memory addresses used by the opcodes that use labels: ; Label table: LOOP is at 00, ONE is at 11, SUM is at 12, COUNT is at 13 ; ;Loc. Code Label Mnemon. Operand Comments ;---- ---- ----- ------- ------- ------------------------- 00 500 LOOP IN ; input a number 01 312 ADD SUM ; add sum to number 02 212 STO SUM ; store back into sum 03 113 LDA COUNT ; load counter 04 411 SUB ONE ; subtract one 05 213 STO COUNT ; store back in counter 06 801 SKZ ; exit loop if counter is zero 07 900 JMP LOOP ; go to top of loop 08 112 LDA SUM ; load sum 09 600 OUT ; output sum 10 700 HLT ; put LMC to sleep (stop execution) 11 001 ONE DAT 001 ; constant 1 12 000 SUM DAT 000 ; sum variable 13 003 COUNT DAT 003 ; counter variable 14 500 DAT ? ; unused memory (junk) from here on