;============================================================= ; LMC Sample Program #3 - input, add, and loop using a counter ;============================================================= ; - Ian! D. Allen - idallen@idallen.ca - www.idallen.com ; ; Program purpose: Loop using a counter, adding numbers to a sum. ; ; Implementation using a structured C++-like code: ; ; sum = 0; ; counter = 3; ; do { ; cin >> tmp; ; sum = sum + tmp; ; } while ( --counter != 0 ); // pre-decrement used ; cout << sum; ; ; LMC-style Pseudocode for the program, using "skip": ; ; sum = 0 ; counter = 3 ; loop: read input into tmp ; sum = sum + tmp ; counter = counter - 1 ; if ( zero light is on ) skip next instruction ; jump to loop ; output sum ; halt ; ; If we create this LMC Input (Inbox) Queue for "IN" statements: ; 500 400 300 ; ; Program execution: 500 + 400 + 300 = 200 (using 3-digit odometer math) ; ; The LMC assembly-language source program below is written in ; three-column format (comments optional): ; ; LABEL MNEMONIC OPERAND ( ; COMMENTS ) ; ; Using labels instead of hard-coding all of the memory locations, makes ; writing the program easier, since we can give good variable names to ; the memory locations and we don't don't need to care where in memory ; things get put until we "assemble" the program into machine code. ; ; Many of the comments in this code are "Teacher Style" comments that ; comment on *HOW* the code works. You would not use Teacher-Style ; comments in your own code. Comments must say *WHY* the code works, ; not *HOW*. Do not use Teacher-Style comments in your own code. ;Label Mnemon. Operand Comments ;----- ------- ------- ------------------------- LDA ZERO ; initialize sum to 0 STO SUM LDA THREE ; initialize counter to 3 STO COUNTER LOOP IN ; input a number to be summed into calculator ADD SUM ; add sum to number STO SUM ; store back into sum LDA COUNTER ; load counter into calculator SUB ONE ; subtract one STO COUNTER ; store back in counter SKZ ; exit loop (skip the JMP) if counter is zero JMP LOOP ; go to top of loop LDA SUM ; load sum into calculator OUT ; output sum HLT ; put LMC to sleep (stop execution) ZERO DAT 000 ; constant 0 ONE DAT 001 ; constant 1 THREE DAT 003 ; constant 3 SUM DAT ; variable used to hold sum COUNTER DAT ; variable used to hold loop count ; 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. A "DAT" ; without an operand reserves memory but does not initialize it. ; Variables typically do not need initial values since the program will ; initialize them at execution time. Constants must have values. ; Turning an LMC assembly-language source program with labels into ; numeric machine code requires 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 (a "Label Table"); the ; second pass writes down ("back plugs") the actual memory addresses ; used by the opcodes that use labels. ; ; We will use a 5-column (plus comments) format for LMC program listings: ; LOCATION CODE LABEL MNEMONIC OPERAND ( ; COMMENTS ) ; Start by filling in the Mnemonic and Operand columns, then go back ; and "hand-assemble" the mnemonics into actual numbers in mailboxes. Assembly Language "Listing" file showing mailboxes (MB) and Machine Code: MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 115 : LDA ZERO ; initialize sum to 0 01 218 : STO SUM 02 117 : LDA THREE ; initialize counter to 3 03 219 : STO COUNTER 04 500 : LOOP IN ; input a number to be summed into calculator 05 318 : ADD SUM ; add sum to number 06 218 : STO SUM ; store back into sum 07 119 : LDA COUNTER ; load counter into calculator 08 416 : SUB ONE ; subtract one 09 219 : STO COUNTER ; store back in counter 10 801 : SKZ ; exit loop (skip the JMP) if counter is zero 11 904 : JMP LOOP ; go to top of loop 12 118 : LDA SUM ; load sum into caclulator 13 600 : OUT ; output sum 14 700 : HLT ; put LMC to sleep (stop execution) 15 000 : ZERO DAT 000 ; constant 0 16 001 : ONE DAT 001 ; constant 1 17 003 : THREE DAT 003 ; constant 3 18 000 : SUM DAT ; variable used to hold sum 19 000 : COUNTER DAT ; variable used to hold loop count Label Table (created during first pass through source code): Label Memory Location ------- --------------- LOOP at 04 ZERO at 15 ONE at 16 THREE at 17 SUM at 18 COUNTER at 19 On the second pass, the assembler program uses the Label Table to back-plug the addresses wherever labels were used in the source code. For example, "LDA ZERO" becomes "115" because ZERO is at location 15. -- | 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/