=========================================================================== Assignment #11 - LMC control statements and subroutine calls =========================================================================== - Ian! D. Allen - idallen@idallen.ca - www.idallen.com Available online: Sunday April 3, 2011 Upload due date: Upload answer file before 23:59 (midnight) on Wednesday April 13, 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 "assignment11.txt" Upload the file via the Assignment11 "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. For all LMC programs below, provide the usual 3-column format, plus comments: LABEL MNEMONIC OPERAND ; COMMENTS ----- -------- ------- -------- You do not have to assemble the LMC program into machine code. If you want to test your LMC program in the LMC simulator, you can assemble it into machine code using this cut-and-paste web interface here: http://ian.idallen.ca/lmcasm.cgi The web interface will generate the machine code that you can download into a file to be loaded by the LMC simulator. ============================================================================== 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 11 notes page answers the other questions. See: 000_test_instructions.html - Instructions for Tests and Exams *** Flow Control Statements Section *** These Class Notes have the answers to these questions: 306_LMCProgramming.html - LMC Machine Level Programming 307_LMC_lights.txt - LMC Negative Flag Trickery 362_LMC_control.html - LMC Loop and Skip Condition Calculations 350_LMC_sample3.txt - input, add, and loop using a counter 350_LMC_sample4.txt - input, IF, output 350_LMC_sample5.txt - WHILE loop quotient and remainder 350_LMC_sample6.txt - negative flag and a FOR loop 1. Code the pseudocode below in LMC assembly language. (16 lines) Identify constants and variables. Use the pseudocode as comments to the right of the associated LMC assembly language, as done in LMC_sample6.txt and LMC_sample7.txt. (Reference: LMC_control.html) input x while ( x != 0 ) { output --x } IN ; input x STO X WHILE LDA X ; while ( x != 0 ) { SUB ZERO SKNZ JMP ENDWH LDA X ; --x SUB ONE STO X LDA X ; output x OUT JMP WHILE ENDWH HLT ZERO DAT 000 ; constant ONE DAT 001 ; constant X DAT ; variable 2. Code the pseudocode below in LMC assembly language. (15 lines) Identify constants and variables. Use the pseudocode as comments to the right of the associated LMC assembly language, as done in LMC_sample6.txt and LMC_sample7.txt. (Reference: LMC_control.html) input x do { output x-- } while ( x > 2 ) IN ; input x STO X DO LDA X ; output x OUT LDA X ; x-- SUB ONE STO X LDA TWO ; while ( x > 2 ) SUB X SKP JMP DO HLT ONE DAT 001 ; constant TWO DAT 002 ; constant X DAT ; variable 3. Code the pseudocode below in LMC assembly language. (28 lines) Identify constants and variables. Use the pseudocode as comments to the right of the associated LMC assembly language, as done in LMC_sample6.txt and LMC_sample7.txt. (Reference: LMC_control.html) for ( x = 20; x > 4; x -= 2 ) { y = x+5 output y+6 } output y+7 LDA TWENTY ; x = 20 STO X WHILE LDA FOUR ; while ( x > 4 ) SUB X SKN JMP ENDWH LDA X ; y = x + 5 ADD FIVE STO Y LDA Y ; output y+6 ADD SIX OUT LDA X ; x -= 2 SUB TWO STO X JMP WHILE ENDWH LDA Y ; output y+7 ADD SEVEN OUT HLT TWO DAT 002 ; constant FOUR DAT 004 ; constant FIVE DAT 005 ; constant SIX DAT 006 ; constant SEVEN DAT 007 ; constant TWENTY DAT 020 ; constant X DAT ; variable Y DAT ; variable 4. Code the pseudocode below in LMC assembly language. (27 lines) Identify constants and variables. Use the pseudocode as comments to the right of the associated LMC assembly language, as done in LMC_sample6.txt and LMC_sample7.txt. (Reference: LMC_control.html) input x input y if ( x != 0 && y != 0 ) { exchange x and y } output x output y REWRITE: as nested IF statements input x input y if ( x != 0 ) { if ( y != 0 ) { exchange x and y } } output x output y REWRITE: code the exchange input x input y if ( x != 0 ) { if ( y != 0 ) { tmp = x x = y y = tmp } } output x output y IN ; input x STO X IN ; input y STO Y LDA X ; if ( x != 0 ) { SUB ZERO SKNZ JMP ENDIF LDA Y ; if ( y != 0 ) { SUB ZERO SKNZ JMP ENDIF LDA X ; tmp = x STO TMP LDA Y ; x = y STO X LDA TMP ; y = tmp STO Y ENDIF LDA X ; output x OUT LDA Y ; output y OUT HLT ZERO DAT 000 ; constant X DAT ; variable Y DAT ; variable TMP DAT ; variable 5. Code the pseudocode below in LMC assembly language. (28 lines) Identify constants and variables. Use the pseudocode as comments to the right of the associated LMC assembly language, as done in LMC_sample6.txt and LMC_sample7.txt. (Reference: LMC_control.html) input x input y if ( x < 10 || y >= 20 ) { x = x + 10 y = y - 10 } output x+y REWRITE: Invert condition and jump to label: input x input y if ( !( x < 10 || y >= 20 ) ) { goto around } x = x + 10 y = y - 10 around: output x+y REWRITE: use deMorgan to turn || into && input x input y if ( x >= 10 && y < 20 ) { goto around } x = x + 10 y = y - 10 around: output x+y REWRITE: as nested IF statements input x input y if ( x >= 10 ) { if ( y < 20 ) { goto around } } x = x + 10 y = y - 10 around: output x+y IN ; input x STO X IN ; input y STO Y LDA X ; if ( x >= 10 ) { SUB TEN SKP JMP ENDIF LDA Y ; if ( y < 20 ) { SUB TWENTY SKN JMP ENDIF JMP AROUND ; goto around ENDIF LDA X ; x = x + 10 ADD TEN STO X LDA Y ; y = y - 10 SUB TEN STO Y AROUND LDA X ; output x+y ADD Y OUT HLT ZERO DAT 000 ; constant TEN DAT 010 ; constant TWENTY DAT 020 ; constant X DAT ; variable Y DAT ; variable *** Subroutine Call Section *** Class Notes References for the answers to these questions: 306_LMCProgramming.html - LMC Machine Level Programming 350_LMC_sample7.txt - subroutine call/return and linkage 6. Code an LMC-Assembler main program and one function for the following pseudocode for a program that inputs two numbers and outputs the difference between the largest and the smallest. Use the subroutine argument passing calling conventions demonstrated in class and documented in LMC_sample7.txt - "LMC Sample Program #7 - subroutine call/return and linkage": input a input b output a output b dif = maxdif(a,b) output dif stop function maxdif(a,b){ if ( a > b ) { return a-b } return b-a } Your main program must load inputs A and B into MAXDIF function argument variables (as was done in LMC_sample7), then call function MAXDIF that returns in the Calculator the difference of the largest number minus the smallest number. Your code should not assume which numbers (larger or smaller) will be in which argument. Pass the two input values by copying them to argument/parameter mailboxes within the MAXDIF function before you call the function, as was done in LMC_sample7. Return the value from MAXDIF by putting it in the LMC Calculator. (Note that the function argument variables cannot have the same name as the input variables. Choose new names.) Code the full LMC program (main plus function) using LMC assembly language in one source file. (20 lines and 13 lines) Identify constants and variables. Use the pseudocode as comments to the right of the associated LMC assembly language, as done in LMC_sample6.txt and LMC_sample7.txt. (Reference: LMC_control.html) WARNING: This is one source file. Make sure that all labels are unique! You cannot have two labels with the same name and different memory locations. ; all labels must be UNIQUE in this source file ; --- MAIN program ; IN ; input a STO A IN ; input b STO B LDA A ; output a OUT LDA B ; output b OUT LDA A ; dif = maxdif(a,b) STO AA ; copy values into function argument variables LDA B STO BB CALL MAXDIF STO DIF ; MAXDIF returns value in calculator; save it LDA DIF ; output dif OUT HLT ; stop A DAT ; variable B DAT ; variable DIF DAT ; variable ; --- Function MAXDIF(aa,bb) returns difference --- ; must use unique labels for two function arguments ; does *NOT* modify its input arguments ; returns the difference in the calculator AA DAT ; first subroutine argument BB DAT ; second subroutine argument MAXRET DAT ; reserved for CALL return address MAXDIF LDA BB ; if ( aa > bb ) SUB AA SKN JMP ENDIF LDA AA ; return AA-BB SUB BB JMP MAXRET ENDIF LDA BB ; return BB-AA SUB AA JMP MAXRET ; OPTIONAL: ; Here is an *OPTIMIZED* version of MAXDIF using less code ; Optimized code is harder to understand and harder to modify ; Do not optimize your code in this course AA DAT ; first subroutine argument BB DAT ; second subroutine argument MAXRET DAT ; reserved for CALL return address MAXDIF LDA BB ; if ( aa > bb ) SUB AA SKN JMP MAXRET ; already subtracted, just return BB-AA LDA AA SUB BB JMP MAXRET ; return AA-BB -- | 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/