;========================================= ; LMC Sample Program #1 - add and subtract ;========================================= ; - 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. It just puts ; data into memory. ; ; The "ORG" (origin) mnemonic is not an LMC instruction. It is used by ; assembly language programmers to indicate that the next instructions ; or data will be placed into memory starting at the given address. ; No code is generated by ORG; it sets the value of the next memory address. ; ; Note that the Negative light will come on after the subtraction. ; It will go off after the addition, because only subtracting a big ; number from a small number sets the Negative light. Addition can ; never set the Negative light. ; ; Note that this LMC program never accesses memory locations from 05 ; through 61 or from 65 to 99 (inclusive). ; ; Output: 7 - 15 + 2 = 994 (using 3-digit odometer math; no negative numbers) ; ;Loc. Code Label Mnemon. Operand Comments ;---- ---- ----- ------- ------- ------------------------- 00 162 LDA 62 ; load first number 01 463 SUB 63 ; subract second number 02 364 ADD 64 ; add third number 03 600 OUT ; output result 04 700 HLT ; put LMC to sleep (stop execution) ORG 62 ; place next data starting at box 62 62 007 DAT 007 ; first number (constant) 63 015 DAT 015 ; second number (constant) 64 002 DAT 002 ; third number (constant) ; 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 162 LDA first ; load first number 01 463 SUB second ; subract second number 02 364 ADD third ; add third number 03 600 OUT ; output result 04 700 HLT ; put LMC to sleep (stop execution) ORG 62 ; place next data starting at box 62 62 007 first DAT 007 ; first number (constant) 63 015 second DAT 015 ; second number (constant) 64 002 third DAT 002 ; third number (constant) ; 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 162 DAT 162 ; load first number 01 463 DAT 463 ; subract second number 02 364 DAT 364 ; add third number 03 600 DAT 600 ; output result 04 700 DAT 700 ; put LMC to sleep (stop execution) ORG 62 ; place next data starting at box 62 62 007 DAT 007 ; first number (constant) 63 015 DAT 015 ; second number (constant) 64 002 DAT 002 ; third number (constant) -- | 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/