================================= Assignment #05 - LMC Intermediate ================================= - Ian! D. Allen - idallen@idallen.ca - www.idallen.com 1. Develop an algorithm and then code a program using LMC mnemonic code ("LM-Assembly Language") that inputs two numbers and outputs the difference of the larger minus the smaller. Your program should not assume which number (larger or smaller) will be input first. Give the algorithm (pseudocode) followed by the LMC program here: input n1 input n2 load n2 subtract n1 if ( negative ) { load n1 subtract n2 } output stop IN STO NUM1 IN STO NUM2 LDA NUM2 ; redundant - it's already loaded SUB NUM1 SKN JMP POS LDA NUM1 SUB NUM2 POS OUT HLT NUM1 DAT NUM2 DAT 2. Convert the LM-Assembler program from the previous question into LM-Numeric machine code. (You may now optionally load and test the code in the LMC Simulator, if you like.) Give the machine code here: 0001 | 00 500 : IN 0002 | 01 211 : STO NUM1 0003 | 02 500 : IN 0004 | 03 212 : STO NUM2 0005 | 04 411 : SUB NUM1 0006 | 05 800 : SKN 0007 | 06 909 : JMP POS 0008 | 07 111 : LDA NUM1 0009 | 08 412 : SUB NUM2 0010 | 09 600 : POS OUT 0011 | 10 700 : HLT 0012 | 11 000 : NUM1 DAT 0013 | 12 000 : NUM2 DAT 3. Develop an algorithm and then code a program using LMC mnemonic code ("LM-Assembly Language") that outputs the numbers from 002 to 010 inclusive in steps of 2 using only one OUT instruction inside a loop. Give the algorithm (pseudocode) followed by the LMC program here: count = 2 limit = 10 while( count <= limit ) { output count count += 2 } stop ; LDA TWO ; optional initialization of COUNT and LIMIT ; STO COUNT ; optional ; LDA TEN ; optional ; STO LIMIT ; optional RPETE LDA LIMIT SUB COUNT SKP JMP DONE LDA COUNT OUT ADD STEP STO COUNT JMP RPETE DONE HLT LIMIT DAT 010 COUNT DAT 002 STEP DAT 002 4. Convert the LM-Assembler program from the previous question into LM-Numeric machine code. (You may now optionally load and test the code in the LMC Simulator, if you like.) Give the machine code here: 0001 | 00 110 : RPETE LDA LIMIT 0002 | 01 411 : SUB COUNT 0003 | 02 802 : SKP 0004 | 03 909 : JMP DONE 0005 | 04 111 : LDA COUNT 0006 | 05 600 : OUT 0007 | 06 312 : ADD STEP 0008 | 07 211 : STO COUNT 0009 | 08 900 : JMP RPETE 0010 | 09 700 : DONE HLT 0011 | 10 010 : LIMIT DAT 010 0012 | 11 002 : COUNT DAT 002 0013 | 12 002 : STEP DAT 002 5. Code an LM-Assembler main program and its subroutine. The main program should input two numbers, load the numbers into subroutine argument variables, then call a subroutine that returns in the calculator the difference of the larger minus the smaller. Your subroutine should not assume which number (larger or smaller) will be in which argument. Pass the two input values by copying them to argument/parameter mailboxes within the subroutine before you call the subroutine. Return the value from the subroutine via the Calculator. Give the algorithm (pseudocode) followed by the LMC program here: IN STO NUM1 IN STO NUM2 CALL MAX OUT HLT ; --- MAX Subroutine --- MAXRET DAT MAX LDA NUM2 SUB NUM1 SKN JMP POS LDA NUM1 SUB NUM2 POS JMP MAXRET NUM1 DAT NUM2 DAT 6. Convert the LM-Assembler program from the previous question into LM-Numeric machine code. (You may now optionally load and test the code in the LMC Simulator, if you like.) Give the machine code here: 0001 | 00 500 : IN 0002 | 01 215 : STO NUM1 0003 | 02 500 : IN 0004 | 03 216 : STO NUM2 0005 | 04 008 : CALL MAX 0006 | 05 600 : OUT 0007 | 06 700 : HLT 0008 | : ; --- MAX Subroutine --- 0009 | 07 000 : MAXRET DAT 0010 | 08 116 : MAX LDA NUM2 0011 | 09 415 : SUB NUM1 0012 | 10 800 : SKN 0013 | 11 914 : JMP POS 0014 | 12 115 : LDA NUM1 0015 | 13 416 : SUB NUM2 0016 | 14 907 : POS JMP MAXRET 0017 | 15 000 : NUM1 DAT 0018 | 16 000 : NUM2 DAT 7. Identify the components of an "Object" file and describe the purpose of each component. From 15LMCLink.htm : 0. machine code instructions (the program itself) 1. a public label table containing a list of all labels defined within the module (and their corresponding memory addresses) that should be made available to other modules. 2. an external reference table containing a list of all memory addresses that make references to "public" labels defined in other modules (plus the name of the actual label being referenced). 3. a relocation address table containing a list of all the addresses containing instructions that include address references to addresses within the module (based on the false assumption that the module would be loaded starting at address 00); all such memory address references will need to be adjusted to reflect the "true" memory address values once the "true" starting address of the module has been established. 8. Explain how a "linker" program uses the relocatable address list, the public (or "global") definition table, and the external reference table (all from within a collection of object files) to generate an "executable" version of a program. Version 1 (Ian!): The linker places all the machine code for all the modules into memory, noting the start location of each module in memory. The linker uses these individual start locations to "fix" addresses within each module. The linker "fixes" the location addresses in all the tables in all the modules loaded - three tables for each module. The linker also "fixes" all the relocatable addresses in the code within each module. The linker then matches up all external references with symbols in the public label tables, plugging in the actual (relocated) addresses of the external modules into the machine code. The resulting "linked" code is saved. Version 2 (Alan Pinck): All addresses in the relocatable address list contain instructions or data which refers to addresses within the object file (assuming the object file were to be loaded at address 0000); all such address references must be adjusted by the actual address where the object file will be loaded. The public/global definition table provides a list of labels which may be referenced by instructions in other object files, plus the address (within this object file) which corresponds to this label. The linker uses this information to replace references in other object files with the correct address within this object file. The external reference table provides a list of labels which are not defined within the current object file plus the location where the reference occurs (the same label may occur more than once in this table). The linker uses this table to determine which locations need to be modified with addresses from the public/global table.