=========================================== Assignment #04 - LMC Basics =========================================== - Ian! D. Allen - idallen@idallen.ca - www.idallen.com 1. List the Instruction Cycle activities for the Little Man Computer (LMC). ANSWER: 1. Read and remember the value in the mailbox located at the the address shown on the counter. 2. Increment the counter. 3. Look up the remembered value on the chart and perform the indicated action. Repeat from step 1 2. For each of the following situations, assume that the Accumulator starts with the value 600. For each question, give the final value in the Accumulator and indicate which of the three indicator light(s) (Z, N, and P) is turned on: 2.1. 400 is added to the 600 in the accumulator. ANSWERS: 000 Z P 2.2. 700 is subtracted from the 600 in the accumulator and then 500 is subtracted from the accumulator. ANSWERS: 400 P 2.3. 800 is added to the 600 in the accumulator. ANSWERS: 400 P 2.4 601 is subtracted from the 600 in the accumulator. ANSWERS: 999 N 3. With reference to the full extended LMC opcode table (lmc_opcodes.htm), which LMC instruction(s) would not work "properly" if the LM incremented the counter *after* performing the instruction instead of before? ANSWER: JMP and CALL would not work properly. 4. List the output which would be generated if the following numeric code were entered into the LMC mailboxes starting at address 00, and then the LM was awakened. (You may want to translate this numeric program into more readable LMC mnemonics before understanding it!) 111 412 802 908 211 313 600 901 413 600 700 010 004 001 ANSWER: (Optional translation into more readable mnemonics:) MB code LAB MNE OPERAND == === ==== === ======= 00 111 LDA X 01 412 TOP SUB FOUR 02 802 SKP ; skip *into* loop if >= 4 03 908 JMP DONE ; exit loop if < 4 04 211 STO X 05 313 ADD ONE 06 600 OUT 07 901 JMP TOP ; keep looping 08 413 DONE SUB ONE ; after loop 09 600 OUT 10 700 HLT 11 010 X DAT 10 ; variable 12 004 FOUR DAT 4 ; constant 13 001 ONE DAT 1 ; constant Output: 7 4 1 996 5. What is the purpose of the LMC mnemonic pseudo-instructions: DAT and ORG? ANSWER: DAT: reserve and possibly initialize memory (a mailbox) at the current address ORG: tell the assembler program to start putting values into the given mailbox address, starting with the next instruction or data item in the source file 6. Translate the following LMC mnemonic code ("assembly language") into LMC numeric code ("machine language") starting in mailbox 00: LDA total SUB total STO total IN STO count rpete LDA count SUB one STO count SKP JMP done IN ADD total STO total JMP rpete done LDA total OUT HLT total DAT count DAT one DAT 001 ANSWER: Label Table: (optional) ------------ RPETE at 05 DONE at 14 TOTAL at 17 COUNT at 18 ONE at 19 Assembler Listing: ------------------ line | MB code: label mnem. operand ==== | == === : ===== ===== ======= 0001 | 00 117 : LDA total 0002 | 01 417 : SUB total 0003 | 02 217 : STO total 0004 | 03 500 : IN 0005 | 04 218 : STO count 0006 | 05 118 : rpete LDA count 0007 | 06 419 : SUB one 0008 | 07 218 : STO count 0009 | 08 802 : SKP 0010 | 09 914 : JMP done 0011 | 10 500 : IN 0012 | 11 317 : ADD total 0013 | 12 217 : STO total 0014 | 13 905 : JMP rpete 0015 | 14 117 : done LDA total 0016 | 15 600 : OUT 0017 | 16 700 : HLT 0018 | 17 000 : total DAT 0019 | 18 000 : count DAT 0020 | 19 001 : one DAT 001 7. Re-code the previous LMC mnemonic program into a Java or C++ program that performs, as closely as possible, the same opearations in the same order. Do not optimize your program or re-order the statments. ANSWER: /* First try: literal translation - not good style */ void main(void) { int total; int count; int temp; int one = 1; total = total - total; cin >> count; rpete: count = count - one; if ( count == 0 ) goto done; cin >> temp; total = temp + total; goto rpete; done: cout total; } /* Second try: better programming style */ void main(void) { int total; int count; int temp; total -= total; cin >> count; while (--count >= 0) { cin >> temp; total += temp; } cout total; }