;=========================================== ; LMC Sample Program #2 - input, add, output ;=========================================== ; - Ian! D. Allen - idallen@idallen.ca - www.idallen.com ; ; We will use this 5-column (plus comments) format for all LMC programs. ; 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. ; ; This LMC program never accesses memory locations past 06. ; The constant value in location 06 ("num") is overwritten before being used. ; ; Input Queue (for "IN" statements): ; 800 ; 300 ; ; Output: 800 + 300 = 100 (using 3-digit odometer math; no negative numbers) ; ;Loc. Code Label Mnemon. Operand Comments ;---- ---- ----- ------- ------- ------------------------- 00 500 IN ; input first number 01 206 STO 06 ; save in memory 02 500 IN ; input second number 03 306 ADD 06 ; add first number 04 600 OUT ; output sum 05 700 HLT ; put LMC to sleep (stop execution) 06 942 DAT 942 ; initialize memory with junk ; Below is the same program rewritten to use labels instead of mailboxes. ; Using labels allows us to reference memory locations by symbolic names. ; This lets us insert and delete code without having to go back and change ; all the mailbox numbers in our source code: ; ;Loc. Code Label Mnemon. Operand Comments ;---- ---- ----- ------- ------- ------------------------- 00 500 IN ; input first number 01 206 STO num ; save in memory 02 500 IN ; input second number 03 306 ADD num ; add first number 04 600 OUT ; output sum 05 700 HLT ; put LMC to sleep (stop execution) 06 942 num DAT 942 ; initialize memory with junk ; Here is the same program written using DAT statements. ; This format is much harder to read since no helpful mnemonics are used: ; ;Loc. Code Label Mnemon. Operand Comments ;---- ---- ----- ------- ------- ------------------------- 00 500 DAT 500 ; input first number 01 206 DAT 206 ; save in memory 02 500 DAT 500 ; input second number 03 306 DAT 306 ; add first number 04 600 DAT 600 ; output sum 05 700 DAT 700 ; put LMC to sleep (stop execution) 06 942 DAT 942 ; initialize memory with junk -- | 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/