=========================================================================== Assignment #10 - Little Man Computer [LMC], Assembly Language, Machine Code =========================================================================== - Ian! D. Allen - idallen@idallen.ca - www.idallen.com Available online: Thursday March 24, 2011 Upload due date: Upload answer file before 23:59 (midnight) on Friday April 1, 2011 Answers will be posted shortly after the due date/time and discussed in class, if there are any questions about the answers. Late assignments may or may not be marked. Submission method: Create a plain text file using the exact name "assignment10.txt" Upload the file via the Assignment10 "Upload File" facility in Blackboard. Use "Attach File" and "Submit" to upload your plain text file. No wordprocessor documents. Do not send email. Use only "Attach File". Use the file name given above. Upload only one single file of plain text, not HTML, not MSWord. No fonts, no word-processing. Plain text only. Did I mention that the format is plain text (VIM/Nano/Pico/Gedit or Notepad)? NO WORD PROCESSOR DOCUMENTS ACCEPTED. No marks are awarded for submitting under the wrong assignment number. Not all assignments will be marked. See the Week 1 Notes for details. Answers will be posted after the due date/time so that you can check your answers before coming to class and ask questions about the answers in class. Please check your answers (and my answers!). I go over each assignment in class if there are questions about the answers. No questions means no review - I'll presume you know the material. Questions similar to ones on these assignments will appear on your tests and exams. ============================================================================== DO THIS: Edit this file and answer the following questions underneath each question. Upload the file containing the questions and answers before the due date. Some of the answers below will require reading the links published in the weekly course notes. ============================================================================== 0. What is the date for your Final Exam? Can you use a calculator? Where do you write the Test Version number? Do you need to put your student number on the final exam mark-sense sheet? DAT2343 Home Page: Monday Apr 25 10h30 (10:30am to 1pm) - P214 The top of the Week 10 notes page answers the other questions. See: 000_test_instructions.html - Instructions for Tests and Exams *** Little-Man Computer [LMC] Section *** 1. What is the difference between a Symbol and a Label? (trick question) No difference. They are synonyms. A "Label Table" is often called a "Symbol Table". Symbols are Labels - they represent memory addresses. 2. What is the purpose of the LMC mnemonic pseudo-instruction ORG? (See the LMC_sample1.txt example.) 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. The Little Man does not see ORG; it is called a "pseudo-instruction" because it affects the assembly translation process but does not result in any generated code. *** Program 1 *** 3. a) Translate the following LMC mnemonic code ("assembly language") into LMC numeric code ("machine language") starting in mailbox 00. Your translated program should be written in five-column (plus comments) format, similar to the end of LMC_sample3.txt: MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- __ ___ : IN __ ___ : STO START __ ___ : IN __ ___ : STO END __ ___ : IN __ ___ : STO INCR __ ___ : LDA ZERO __ ___ : STO SUM __ ___ : LOOP LDA END __ ___ : SUB START __ ___ : SKP __ ___ : JMP ENDLP __ ___ : LDA SUM __ ___ : ADD START __ ___ : STO SUM __ ___ : LDA START __ ___ : ADD INCR __ ___ : STO START __ ___ : JMP LOOP __ ___ : ENDLP LDA START __ ___ : OUT __ ___ : LDA SUM __ ___ : OUT __ ___ : HLT __ ___ : START DAT ; variable __ ___ : END DAT ; variable __ ___ : INCR DAT ; variable __ ___ : SUM DAT ; variable __ ___ : ZERO DAT 000 ; constant MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 500 : IN 01 224 : STO START 02 500 : IN 03 225 : STO END 04 500 : IN 05 226 : STO INCR 06 128 : LDA ZERO 07 227 : STO SUM 08 125 : LOOP LDA END 09 424 : SUB START 10 802 : SKP 11 919 : JMP ENDLP 12 127 : LDA SUM 13 324 : ADD START 14 227 : STO SUM 15 124 : LDA START 16 326 : ADD INCR 17 224 : STO START 18 908 : JMP LOOP 19 124 : ENDLP LDA START 20 600 : OUT 21 127 : LDA SUM 22 600 : OUT 23 700 : HLT 24 000 : START DAT ; variable 25 000 : END DAT ; variable 26 000 : INCR DAT ; variable 27 000 : SUM DAT ; variable 28 000 : ZERO DAT 000 ; constant 4. b) Show the "Label Table" for the above program, linking labels with mailbox addresses. Reference: LMC_sample3.txt Label Table ----------- LOOP at 08 ENDLP at 19 START at 24 END at 25 INCR at 26 SUM at 27 ZERO at 28 5. c) Re-code the above program into a Java or C++ program or pseudocode that performs, as closely as possible, the same operations in the same order. Do not optimize your program or re-order the statements. Do a direct translation, using standard programming control structures such as IF/ELSE/WHILE/etc. Reference: LMC_control.html Here is just the algorithm: input start input end input incr sum = 0 while ( start <= end ) sum += start start += incr endwh output start output sum Here it is as comments on the assembly language (optional): MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 500 : IN ; input start 01 224 : STO START 02 500 : IN ; input end 03 225 : STO END 04 500 : IN ; input incr 05 226 : STO INCR 06 128 : LDA ZERO ; sum = 0 07 227 : STO SUM 08 125 : WHILE LDA END ; while ( start <= end ) 09 424 : SUB START 10 802 : SKP 11 919 : JMP ENDWH 12 127 : LDA SUM ; sum += start 13 324 : ADD START 14 227 : STO SUM 15 124 : LDA START ; start += incr 16 326 : ADD INCR 17 224 : STO START 18 908 : JMP WHILE ; endwh 19 124 : ENDWH LDA START ; output start 20 600 : OUT 21 127 : LDA SUM ; output sum 22 600 : OUT 23 700 : HLT 24 000 : START DAT ; variable 25 000 : END DAT ; variable 26 000 : INCR DAT ; variable 27 000 : SUM DAT ; variable 28 000 : ZERO DAT 000 ; constant 6. d) In simple English, what does the above program do? Calculate a SUM, starting at START, up to and including END, incrementing by INCR. Output the final value of START and the SUM. 7. e) Run the previous program "on paper". Given input values 004, 009, 003, what would be the program output? Trace: START = 4 END = 9 INCR = 3 SUM = 0 test and SKP: END - START --> 9 - 4 --> TRUE (skip) SUM = SUM + START --> SUM = 0 + 4 --> SUM = 4 START = START + INCR --> START = 4 + 3 --> START = 7 START = 7 END = 9 INCR = 3 SUM = 4 test and SKP: END - START --> 9 - 7 --> TRUE (skip) SUM = SUM + START --> SUM = 4 + 7 --> SUM = 11 START = START + INCR --> START = 7 + 3 --> START = 10 START = 10 END = 9 INCR = 3 SUM = 11 test and SKP: END - START --> 9 - 10 --> FALSE (no skip; jump out) output START and SUM --> output 10 and 11 Inputs 004, 009, 003 -> outputs: 010 011 f) Optional: After tracing the above program by hand, enter it into the LMC simulator and verify your answer. *** Program 2 *** 8. a) Turn the following pseudocode into LMC assembly language and then into LMC machine code starting at memory location 00. Your translated program should be written in five-column (plus comments) format, similar to the end of LMC_sample3.txt: LOCATION CODE LABEL MNEMONIC OPERAND ( ; COMMENTS ) Identify which labels are for variables and which are constants. Use the pseudocode as comments beside your assembly language. See Reference: LMC_control.html input x y = x + 500 if ( y < x ) z = 999 else z = y endif output z LMC assembly language and machine code: MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 500 : IN ; input x 01 217 : STO X 02 117 : LDA X ; y = x + 500 03 320 : ADD FIVEH 04 218 : STO Y 05 118 : LDA Y ; if ( y < x ) 06 417 : SUB X 07 800 : SKN 08 912 : JMP ELSE 09 121 : LDA NINEN ; z = 999 10 219 : STO Z 11 914 : JMP ENDIF 12 118 : ELSE LDA Y ; z = y 13 219 : STO Z 14 119 : ENDIF LDA Z ; output z 15 600 : OUT 16 700 : HLT ; stop 17 000 : X DAT ; variable 18 000 : Y DAT ; variable 19 000 : Z DAT ; variable 20 500 : FIVEH DAT 500 ; constant 21 999 : NINEN DAT 999 ; constant 9. b) Run the previous program "on paper". Given an input value of 700, what would be the program output? What is the output for an input value of 300? Input 700 -> output 999 Input 300 -> output 800 c) Optional: After tracing the above program by hand, enter it into the LMC simulator and verify your answer. *** Program 3 *** 10. a) Translate the following numeric program into LMC mnemonics and labels. (This is called "dis-assembling" code. You start with machine code and you end up with assembly language.) Read the mailboxes across each line below in left-to-right in memory dump format, starting at mailbox 00. 500 218 500 219 118 419 800 912 119 418 220 915 118 419 220 120 600 700 000 000 Your translated assembly language program should be written in standard five-column (plus comments) format, similar to the end of LMC_sample3.txt: LOCATION CODE LABEL MNEMONIC OPERAND ( ; COMMENTS ) Identify which labels are for variables and which are constants. Make your best guess as to what control structures are being used in the program and choose appropriate label names to reflect them. See Reference: LMC_control.html LMC assembly language and machine code: MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 500 : IN 01 218 : STO X 02 500 : IN 03 219 : STO Y 04 118 : LDA X 05 419 : SUB Y 06 800 : SKN 07 912 : JMP ELSE 08 119 : LDA Y 09 418 : SUB X 10 220 : STO Z 11 915 : JMP ENDIF 12 118 : ELSE LDA X 13 419 : SUB Y 14 220 : STO Z 15 120 : ENDIF LDA Z 16 600 : OUT 17 700 : HLT 18 000 : X DAT ; variable 19 000 : Y DAT ; variable 20 000 : Z DAT ; variable 11. b) Run the previous program "on paper". List all the outputs that would be generated if the input values were 500 and 300. 500-300=200 so output is "200" c) Optional: After tracing the above program by hand, enter it into the LMC simulator and verify your answer. 12. d) Reverse-engineer the high-level pseudocode (or Java code or C++ code) that could have generated the above assembly language. See Reference: LMC_control.html Here is just the algorithm: input x input y if ( x < y ) z = y - x else z = x - y endif output z Here it is as comments on the assembly language (optional): MB Code Label Mnemon. Operand Comments -- ---- ----- ------- ------- ------------------------- 00 500 : IN ; input x 01 218 : STO X 02 500 : IN ; input y 03 219 : STO Y 04 118 : LDA X ; if ( x < y ) 05 419 : SUB Y 06 800 : SKN 07 912 : JMP ELSE 08 119 : LDA Y ; z = y - x 09 418 : SUB X 10 220 : STO Z 11 915 : JMP ENDIF 12 118 : ELSE LDA X ; z = x - y 13 419 : SUB Y 14 220 : STO Z 15 120 : ENDIF LDA Z ; output z 16 600 : OUT 17 700 : HLT ; stop 18 000 : X DAT ; variable 19 000 : Y DAT ; variable 20 000 : Z DAT ; variable -- | 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/