;========================================== ; LMC Sample Program #4 - input, IF, output ;========================================== ; -Ian! D. Allen - idallen@idallen.ca - www.idallen.com ; ; Some simple random C++ code, translated to LMC assembly language. ; ; cin >> first; ; cin >> second; ; if ( second <= first ) { ; cout << ++first + second; ; } ; cout << second-- + first + 3; ; ; LMC-style Pseudocode for the program, using "skip" and "jump": ; ; read input into first ; read input into second ; calculate first - second (to set LMC lights) ; if ( positive light is on ) skip next instruction ; jump to ENDIF (jump over IF body) ; increment first ; output first + second ; ENDIF: output second + first + 3 ; decrement second ; halt ; ; If we create this LMC Input (Inbox) Queue for "IN" statements: ; 100 900 ; ; Program output (using 3-digit odometer math): ; 900+100+3=003 ; ; If we create this LMC Input (Inbox) Queue for "IN" statements: ; 900 100 ; ; Program output (using 3-digit odometer math): ; 901+100=001 ; 100+901+3=004 ; ; The LMC assembly-language source program below is written in ; three-column format (comments optional): ; ; LABEL MNEMONIC OPERAND ( ; COMMENTS ) ; ; I've put each line of C++ in as a comment: ; ;Label Mnem. Operand Comments............. ;----- ----- ------- --------------------- ; cin >> first; IN ; read and store first number STO FIRST ; cin >> second; IN ; read and store second number STO SECOND ; if ( second <= first ) { LDA FIRST ; if second is <= first SUB SECOND SKP ; skip *into* IF statement if second is <= first JMP ENDIF ; jump to ENDIF if second > first ; cout << ++first + second; LDA FIRST ; pre-increment of FIRST ADD ONE STO FIRST LDA FIRST ; addition of FIRST and SECOND and output ADD SECOND OUT ; } /* ENDIF */ ; cout << second-- + first + 3; ENDIF LDA SECOND ; addition of SECOND, FIRST, and 3 and output ADD FIRST ADD THREE OUT LDA SECOND ; post-decrement of SECOND SUB ONE STO SECOND HLT ; constants: ONE DAT 001 ; constant 1 THREE DAT 003 ; constant 3 ; variables: FIRST DAT ; variable first SECOND DAT ; variable second ------------------------------------------------------------------------------- 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 500 : IN ; read and store first number 01 224 : STO FIRST 02 500 : IN ; read and store second number 03 225 : STO SECOND 04 124 : LDA FIRST ; if second is <= first 05 425 : SUB SECOND 06 802 : SKP ; skip *into* IF statement if second is <= first 07 914 : JMP ENDIF ; jump to ENDIF if second > first 08 124 : LDA FIRST ; pre-increment of FIRST 09 322 : ADD ONE 10 224 : STO FIRST 11 124 : LDA FIRST ; addition of FIRST and SECOND and output 12 325 : ADD SECOND 13 600 : OUT 14 125 : ENDIF LDA SECOND ; addition of SECOND, FIRST, and 3 and output 15 324 : ADD FIRST 16 323 : ADD THREE 17 600 : OUT 18 125 : LDA SECOND ; post-decrement of SECOND 19 422 : SUB ONE 20 225 : STO SECOND 21 700 : HLT 22 001 : ONE DAT 001 ; constant 1 23 003 : THREE DAT 003 ; constant 3 24 000 : FIRST DAT ; variable first 25 000 : SECOND DAT ; variable second Label Table (created during first pass through source code): Label Memory Location ------- --------------- ENDIF at 14 ONE at 22 THREE at 23 FIRST at 24 SECOND at 25 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, "STO FIRST" becomes "224" because FIRST is at location 24. -- | 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/