====================================================== Assignment #08 - Bitwise, Boolean, Z+S+C+O flags, LMC Computer ====================================================== - Ian! D. Allen - idallen@idallen.ca - www.idallen.com Available online: Wednesday March 9, 2011 Upload due date: Upload answer file before 23:59 (midnight) on Wednesday March 16, 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 "assignment08.txt" Upload the file via the Assignment08 "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, showing the method or formula you used to get the answer. Some of the questions may already give you the answer - you must show the full method you use to generate that answer. Upload the file containing the question, methods, formulas, and answers before the due date. Some of the answers below will require reading the links published in the weekly course notes. Full marks are awarded only if you show your method, the same method you will have to use on tests and exams. ============================================================================== 0. What is the date for your Second Midterm Test? You may use a simple calculator on your Second Midterm Test. No phones or PDA devices. You will not need a calculator; the math will be simple powers of two. From the Course Home Page: Friday March 18 - Week 9 - Midterm #2 *** Bitwise Operators Section: AND OR XOR NOT *** 1. For eight-bit numbers a=6Ch and b=36h give answers in binary and hex: a) What is a&b? _________(2) = ___________h ? b) What is a|b? _________(2) = ___________h ? c) What is a^b? _________(2) = ___________h ? d) What is ~a? _________(2) = ___________h ? e) What is ~b? _________(2) = ___________h ? a=6C = 0110 1100 b=36 = 0011 0110 a) What is a&b? 0010 0100(2) = 24h b) What is a|b? 0111 1110(2) = 7Eh c) What is a^b? 0101 1010(2) = 5Ah d) What is ~a? 1001 0011(2) = 93h e) What is ~b? 1100 1001(2) = C9h 2. For any value a, what is the bit pattern that results from a^a ? "All bits off." In other words, always zero. 3. For any value a, what is the bit pattern that results from a | ~a ? "All bits on". e.g. in 4 bits if a=0101 then ~a=1010 and a|~a = 1111 4. For any values a and b, explain what happens after the following two sequential XOR operations, e.g. try a=5,b=10 (or other values) and see what happens to a after you do these two XOR operations in this order: a = a^b a = a^b How has a changed after each of the above two operations? Does this result depend on the values of a and b? No change in value of "a" after two operations; no dependence on the values of "a" and "b". The second XOR cancels the first one. XOR is a cheap and quick "file obscuring" operation to perform on the bytes of a file to obscure the contents of the file. 5. For any values a and b, explain what happens after the following three sequential XOR operations, e.g. try a=5,b=10 (or other values) and see what happens to a and b after you do these three XOR operations in this order: a = a^b b = a^b a = a^b How have a and b changed after the above three operations? Does this result depend on the values of a and b? The values of "a" and "b" trade places; no dependence on the values of "a" and "b". This is a (slow) way to exchange the values of two variables without requiring a third, temporary variable. 6. Java integers (including constants) are 32 bits. Give the values of "x" in 32-bit hexadecimal after each of these Java statements: ( Hint: 0 = 00000000h as 32-bit hexadecimal ) int x = 0xF | 0xF00; x = 00000F0Fh a) int x = ~0; x = _________________h ? b) int x = ~1; x = _________________h ? c) int x = ~0xF; x = _________________h ? d) int x = ~0xFFFF; x = _________________h ? e) int x = ~0xFFFFFFFF; x = _________________h ? f) int x = ~0x11111111; x = _________________h ? g) int x = ~0x80000000; x = _________________h ? int x = 0xF | 0xF00; x = 00000F0Fh a) int x = ~0; x = FFFFFFFFh b) int x = ~1; x = FFFFFFFEh c) int x = ~0xF; x = FFFFFFF0h d) int x = ~0xFFFF; x = FFFF0000h e) int x = ~0xFFFFFFFF; x = 00000000h f) int x = ~0x11111111; x = EEEEEEEEh g) int x = ~0x80000000; x = 7FFFFFFFh *** Boolean Section - see 03.ppt *** 7. What is the opposite (the inverse of, the "NOT" of) these conditions: if ( x == 0 ) ? opposite: if ( x != 0 ) a) if ( x < 0 ) ? opposite: _____________________ ? b) if ( x > 0 ) ? opposite: _____________________ ? if ( x == 0 ) ? opposite: if ( x != 0 ) a) if ( x < 0 ) ? opposite: if ( x >= 0 ) b) if ( x > 0 ) ? opposite: if ( x <= 0 ) NOTE! that the opposite of "<" is ">=". NOTE! that the opposite of ">" is "<=". 8. Using deMorgan, write a simplified expression for the Boolean complement of the logic function F(u,v,w) = u'(v + w') F(u,v,w) = u'(v + w') : original F'(u,v,w) = ( u'(v + w') )' : complement = u'' + (v + w')' : deMorgan = u + (v + w')' : identity = u + v'w'' : deMorgan = u + v'w : identity 9. Using deMorgan, write a simplified expression for the Boolean complement of the logic function F(u,v,w) = u' + (vw') F(u,v,w) = u' + (vw') : original F'(u,v,w) = ( u' + (vw') )' : complement = u''(vw')' : deMorgan = u(vw')' : identity = u(v' + w'') : deMorgan = u(v' + w) : identity = uv' + uw : distribute (optional) 10. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "Call the add routine unless: the cost is less than zero or the colour is 'red'." Note that "unless X" means "IF NOT X" (the logical complement). if ( ! ( cost < 0 || colour == red ) ) call add; : original if ( !(cost < 0) && !(colour == red) ) call add; : deMorgan if ( cost >= 0 && colour != "red" ) call add(); : complement 11. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "In MiddleEarth, you lose (cannot renew) your driving license if your age is over 40 or you have 32 or more demerit points. Call the renew() function if this is not true. (Write a simplified IF statement that calls renew() if you are allowed to renew.)" lose = age > 40 || demerit >= 32 renew = NOT lose if ( NOT lose ) call renew() : original if ( ! ( age > 40 || demerit >= 32 ) ) call renew(); : substitute if ( !(age > 40) && !(demerit >= 32) ) call renew(); : deMorgan if ( age <= 40 && demerit < 32 ) call renew(); : complement 12. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "In Gondor, you lose (cannot renew) your driving license if your age is 50 or more, or you have more than 11 demerit points. Call the renew() function if this is not true. (Write a simplified IF statement that calls renew() if you are allowed to renew.)" LOSE = AGE >= 50 || POINTS > 11 renew = NOT LOSE if ( NOT LOSE ) call renew() : original ! LOSE = ! ( AGE >= 50 || POINTS > 11 ) : substitute ! LOSE = !(AGE >= 50) && !(POINTS > 11) : deMorgan ! LOSE = AGE < 50 && POINTS <= 11 : complement if ( AGE < 50 && POINTS <= 11 ) call renew() : substitue 13. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "An order is classified as "overdue" if the cost is more than $10 and the time is more than 90 days. Call the inventory routine if the size is bigger than 10 litres and the order is not overdue." overdue = cost > 10 AND time > 90 NOT overdue = NOT ( cost > 10 AND time > 90 ) = cost <= 10 OR time <= 90 if ( size > 10 AND NOT overdue ) CALL inventory(); if ( size > 10 AND ( cost <= 10 OR time <= 90 ) ) CALL inventory(); - the parentheses are required, since AND has precedence 14. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "An order is classified as "priority" if the cost is more than $10 or the time is less than 90 days. Call the notify routine if the cost is more than $10 and the order is not priority." Find the bug in the above logic. What is wrong? As a programmer, what would you tell your boss about this specification? priority = cost > 10 OR time < 90 NOT priority = NOT ( cost > 10 OR time < 90 ) = cost <= 10 AND time >= 90 if ( cost > 10 AND NOT priority ) CALL notify(); if ( cost > 10 AND cost <= 10 AND time >= 90 ) CALL notify(); Observe that "cost > 10 AND cost <= 10" is a case of "A AND NOT A", which can never be true. The notify() routine will never execute. The specification is wrong. Go tell your boss you figured that out. 15. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "Call the DEL routine unless: the COST is not zero or the STATUS is not 'final'." Note that "unless X" means "IF NOT X" (the logical complement). if ! ( COST != 0 || STATUS != final ) call DEL : original if !(COST != 0) && !(STATUS != final) call DEL : deMorgan if COST == 0 && STATUS == final call DEL : complement 16. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "Call the UPDATE routine unless: the COST is not less than zero and the TYPE is not 'oval'." Note that "unless X" means "IF NOT X" (the logical complement). if ! ( !(COST < 0) && TYPE != oval ) call UPDATE : original if !!(COST < 0) || !(TYPE != oval) call UPDATE : deMorgan if COST < 0 || TYPE == oval call UPDATE : complement 17. Write the simplest IF statement (simplify the Boolean logic) for the following programming problem specification: "A STALE sale is one where the DATE is greater than or equal to four years old and the COST is bigger than or equal to zero. If the sale is NOT STALE, call the SEND routine." STALE = DATE >= 4 && COST >= 0 : original if ! STALE call SEND : original ! STALE = ! ( DATE >= 4 && COST >= 0 ) : substitute ! STALE = !(DATE >= 4) || !(COST >= 0) : deMorgan ! STALE = DATE < 4 || COST < 0 : complement if DATE < 4 || COST < 0 call SEND : substitute *** Miscellaneous Section *** 18. Define the term "word" as it is used in computer architecture. [See Wikipedia: Word (computing)] A word is the typical native "integer" type used in the CPU, i.e. the collection of bits used by a particular processor to represent its basic integer numeric form. 32-bit processors have 32-bit words (integers); 64-bit processors have 64-bit words (integers). Processors can still do mathematics on larger and smaller integers, but it may require more (often much more) CPU or extra instructions. 19. Put an "X" beside all the 32-bit sign/magnitude negative numbers: 2007A654h 600A3B65h 900000E2h AA0000EFh B9000037h F000765Ah E9900000h 2007A654h 600A3B65h X-900000E2h X-AA0000EFh X-B9000037h X-F000765Ah X-E9900000h 20. The IEEE 754 floating-point number 7EDCBA98h is positive. Without converting, give the hexadecimal for the same number, only negative. Turn on the sign bit: 7EDCBA98h --> FEDCBA98h 21. Without converting, put an "X" beside all the IEEE 754 negative numbers: 2007A654h 600A3B65h 900000E2h AA0000EFh B9000037h F000765Ah E9900000h 2007A654h 600A3B65h X-900000E2h X-AA0000EFh X-B9000037h X-F000765Ah X-E9900000h 22. The following hexadecimal memory dump contains two big-endian four-byte two's-complement integers, starting at address 101. What decimal values do these two big-endian four-byte integers have? ADDRESS: ---------- MEMORY BYTES ---------- 100: 00 01 DF 5E 87 FE 20 A1 7A 00 00 11 13 02 00 4F 3A F1 ... Integer 1: 01 DF 5E 87 = 01DF5E87 -> positive number = +31415943 decimal Integer 2: FE 20 A1 7A = FE20A17A -> negative number (PANIC) -> use hex flip table -> 01DF5E85 -> add 1 -> 01DF5E86 = 31415942 decimal -> put a minus sign in front -> -31415942 23. The following hexadecimal memory dump contains two little-endian four-byte two's-complement integers, starting at address 102. What decimal values do these two little-endian four-byte integers have? ADDRESS: ---------- MEMORY BYTES ---------- 100: FF 01 DE 5E 86 FE 21 A1 79 61 62 63 64 FF C4 F4 A3 1F ... Integer 1: DE 5E 86 FE -> reverse -> FE 86 5E DE = FE865EDE -> negative -> use hex flip table -> 0179A121 -> add 1 -> 0179A122 = 24748322 decimal -> put a minus sign in front -> -24748322 Integer 2: 21 A1 79 61 -> reverse -> 61 79 A1 21 = 6179A121 -> positive -> 6179A121 = 1635361057 decimal 24. The following is a partial hexadecimal memory dump of the boot sector of an MS-DOS disk (MS-DOS means an Intel-based PC - what byte order?): 0000: EB 3C 90 4D 53 44 4F 53 35 2E 30 00 02 04 01 00 0010: 02 00 04 00 00 F8 F6 00 13 00 20 00 11 00 00 00 Read the above dump and give the hexadecimal and decimal values of the following unsigned integer items of different widths (sizes in bytes). The size of the integer is to the right of the offset, separated by a slash. The name of the item is also given below. Offset/Size: type of item: value in hex, value in decimal ----------- ------------------------------------------------ 000Bh/2: bytes per sector: 00 02 -> 0200h = 512 decimal a) 000Dh/1: sectors per allocation unit (cluster): ____________? b) 0010h/1: number of copies of FAT: ____________? c) 0011h/2: number of root directory entries: ____________? d) 0016h/2: number of sectors per FAT: ____________? e) 0018h/2: number of sectors per track: ____________? f) 001Ah/2: number of heads: ____________? Offset/Size: type of item: value in hex, value in decimal ----------- ------------------------------------------------ 000Bh/2: bytes per sector: 00 02 -> 0200h = 512 decimal a) 000Dh/1: sectors per allocation unit (cluster): 04h = 4 decimal b) 0010h/1: number of copies of FAT: 02h = 2 decimal c) 0011h/2: number of root directory entries: 00 04 -> 0400h = 1024(10) d) 0016h/2: number of sectors per FAT: F6 00 -> 00F6h = 246 decimal e) 0018h/2: number of sectors per track: 13 00 -> 0013h = 19 decimal f) 001Ah/2: number of heads: 20 00 -> 0020h = 32 decimal 25. Looking at the previous question, note the size of each integer and answer these questions (all integers are unsigned): a) What is the maximum number of sectors per allocation unit possible? b) What is the maximum number of root directory entries possible? max sectors/alloc (1 byte) is (2**8)-1 = FFh = 255 decimal max root entries (2 bytes) is (2**16)-1 = FFFFh = 65535 decimal 26. The eight-character ASCII text string "12345678" is stored in memory starting at location zero. (These are eight ASCII characters.) See http://easycalculation.com/hex-converter.php a) Give the eight hexadecimal character bytes as stored in memory: 31h 32h 33h 34h 35h 36h 37h 38h b) Interpret these eight bytes as two 32-bit two's complement integers in little-endian form and give their two decimal values: Integer 1: 31 32 33 34 -> 34333231h = 875770417 decimal Integer 2: 35 36 37 38 -> 38373635h = 943142453 decimal c) Interpret these eight bytes as four 16-bit two's complement integers in big-endian form and give their four decimal values: Integer 1: 3132h = (positive) 12594 decimal Integer 2: 3334h = (positive) 13108 decimal Integer 3: 3536h = (positive) 13622 decimal Integer 4: 3738h = (positive) 14136 decimal d) Add two to each of the four big-endian integers from (c) and give the resulting changed ASCII text string (eight ASCII characters): 3132h + 1 = 3134h 3334h + 1 = 3336h 3536h + 1 = 3538h 3738h + 1 = 373Ah ASCII: 31 34 33 36 35 38 37 3A -> "1436587:" *** Computer Basics Section - see 01.ppt *** 27. What is the modern version of Moore's Law? "the density of silicon chips doubles every 18 months" 28. Why can't Moore's law continue indefinitely? At some point the wires and components get close to the size of molecules and stop working. 29. What is the "von Neumann bottleneck"? "The limited throughput (data transfer rate) between the CPU and memory compared to the amount of memory. In most modern computers, throughput is much smaller than the rate at which the CPU can work. This seriously limits the effective processing speed when the CPU is required to perform minimal processing on large amounts of data. The CPU is continuously forced to wait for needed data to be transferred to or from memory. Since CPU speed and memory size have increased much faster than the throughput between them, the bottleneck has become more of a problem, a problem whose severity increases with every newer generation of CPU." http://en.wikipedia.org/wiki/Von_Neumann_architecture 30. What are some ways to work around the "von Neumann bottleneck"? Use on-chip registers and cache memory, separate paths for instructions and data, multiple processors. *** Zero, Sign, Carry, Overflow Section - see 02.ppt *** Reference: 02.ppt slides 47-49, 53-54 - slide 54 has an error - change second-line 0100+0010 to be 0100+0110 - see the Week 07 notes for handling flags set by Subtraction. 31. Build a Truth Table for two's complement "overflow", based on the rule that the Overflow flag is set if the carry in to the sign bit does not equal the carry out. Use these headings on the truth table columns: ------------ ------------- A B CI R CO OV ------------ ------------- Columns A, B, and CI are possible inputs. R, CO, and OV are outputs. Columns A and B are the sign bits of the two numbers being added. Column CI is the possible carry in to the sign bit. CO (carry out), R (result), and OV (overflow flag) are outputs. The truth table must have eight rows, for all possible combinations of the three inputs A, B, and CI. R is the sign bit of the result (based on A, B, and CI). CO is the carry out (also based on A, B, and CI). OV is whether or not the Overflow flag is set (based on CI and CO). Fill in the whole 8x6 table. The table starts with every possible combination of the three input bits on the left, from 0 0 0 to 1 1 1. The three outputs are: ------------ ------------- A B CI R CO OV ------------ ------------- 0 0 0 0 0 No 0 0 1 1 0 Yes (because CI != CO) 0 1 0 1 0 No 0 1 1 0 1 No 1 0 0 1 0 No 1 0 1 0 1 No 1 1 0 0 1 Yes (because CI != CO) 1 1 1 1 1 No 32. How do you know that a two's-complement addition has set the Overflow flag? (There are at least two ways. Give one or both.) Method A: positive+positive=negative or negative+negative=positive -OR- Method B: carry-in to sign bit is not equal to carry-out from sign bit 33. Copy the left column of the table in 02.ppt slide 54. Fix the second sum to be 0100+0110. Perform the given two's complement additions. Without looking at the answers, fill in the remaining four columns based on the results. Add another column that states whether the result is correct if treated as unsigned math instead of two's complement. Your answer will have four rows and five columns: RESULT CARRY OVERFLOW SIGNOK UNSIGNOK 0100 + 0010 = 0110 No No Yes Yes 0100 + 0110 = 1010 No Yes No Yes 1100 + 1110 = 1010 Yes No Yes No 1100 + 1010 = 0110 Yes Yes No No Did you remember to fix the typing error in the second line of the slide? The SIGNOK is the logical complement of the OVERFLOW flag. The UNSIGNOK is the logical complement of the CARRY flag. 34. Perform the following four additions and subtractions in binary, assuming a 6 bit word. Show the Result value plus the values of the Zero, Sign, Carry, and Overflow flag values for each (five answers for each). The "Carry" flag indicates a "Borrow" when doing subtraction of a big number from a smaller number. CARRY: 1111 011010 011010 + 001111 - 001111 Result: 101001 Result: 001011 Zero: 0 Zero: 0 Sign: 1 Sign: 0 Carry: 0 Carry: 0 (no borrow needed) Ovflo: 1 Ovflo: 0 CARRY: 111111 010111 010110 + 101001 - 010110 Result: 000000 Result: 000000 Zero: 1 Zero: 1 Sign: 0 Sign: 0 Carry: 1 Carry: 0 (no borrow needed) Ovflo: 0 Ovflo: 0 In the above four examples, we have: a) pos+pos=neg is wrong = overflow b) pos-pos(same as pos+neg)=pos is fine = no overflow c) pos+neg=pos is fine = no overflow d) pos-pos(same as pos+neg)=pos is fine = no overflow Above, "pos-pos" is the same math as "pos+neg" (no overflow possible). "neg-neg" would be the same math as "neg+pos" (no overflow possible). 35. Perform the following four additions and subtractions in binary, assuming a 6 bit word. Show the Result value plus the values of the Zero, Sign, Carry, and Overflow flag values for each (five answers for each). 010101 100101 + 101011 - 100101 Result: 000000 Result: 000000 Zero: 1 Zero: 1 Sign: 0 Sign: 0 Carry: 1 Carry: 0 Ovflo: 0 Ovflo: 0 011010 011010 - 001111 + 001111 Result: 001011 Result: 101001 Zero: 0 Zero: 0 Sign: 0 Sign: 1 Carry: 0 Carry: 0 Ovflo: 0 Ovflo: 1 36. Perform the indicated arithmetic in hexadecimal, assuming a 12-bit word. Show the hexadecimal Result plus the states of the Zero, Sign, Carry and Overflow flags (five answers for each problem). The "Carry" flag indicates a "Borrow" when doing subtraction of a big number from a smaller number. CARRY: 111 111 D8A 948 C8B ACE +276 -35A +839 -BDF ------------------------------- Result: 000 5EE 4C4 EEF Zero: on off off off Sign: off off off on Carry: on off on on Ovflo: off on* on off (*) Subtracting a positive is the same as adding a negative, and adding two negatives must give a negative, not a positive. Or, consider that subtracting a positive from a negative must generate a more negative number, not a positive number. The overflow flag comes on when the answer is wrong for two's complement. The simple rule to remember is that overflow only happens when pos+pos=neg or neg+neg=pos. For subtraction, note that subtracting a positive is the same math as adding a negative, and subtracting a negative is the same math as adding a positive. In the above examples, we have: a) neg+pos=pos is fine = no overflow b) neg-pos=pos is wrong = overflow c) neg+neg=pos is wrong = overflow d) neg-neg=neg is fine = no overflow Above, "neg-pos" is the same math as "neg+neg" and overflow is possible, and "neg-neg" is the same math as "neg+pos" (no overflow possible). 37. Add 16-bit 4999h to 9321h and give the Result, Zero, Sign, Carry, and Overflow. Is the result correct for unsigned math? for signed math? 4999h + 9321h = DCBAh Result = DCBAh Zero = 0 Sign = 1 Carry = 0 Overflow = 0 (overflow not possible if signs differ) CORRECT for unsigned (carry is off) CORRECT for signed (overflow is off) 38. Add 16-bit 7BCDh to AFFFh and give the Result, Zero, Sign, Carry, and Overflow. Is the result correct for unsigned math? for signed math? CARRY: 1111 7BCDh + AFFFh ------- RESULT: 2BCCh Result = 2BCCh (must fit in 16 bits) Zero = 0 Sign = 0 Carry = 1 Overflow = 0 (overflow not possible if signs differ) NOT CORRECT for unsigned (carry is on) CORRECT for signed (overflow is off) 39. Add 16-bit 59F9h to 5321h and give the Result, Zero, Sign, Carry, and Overflow. Is the result correct for unsigned math? for signed math? CARRY: 1 59F9h + 5321h ------- RESULT: AD1Ah Result = AD1Ah Zero = 0 Sign = 1 Carry = 0 Overflow = 1 (positive plus positive cannot be negative) CORRECT for unsigned (carry is off) NOT CORRECT for signed (overflow is on) 40. Add 16-bit AA9Ch to 8BCDh and give the Result, Zero, Sign, Carry, and Overflow. Is the result correct for unsigned math? for signed math? CARRY: 1111 AA9Ch + 8BCDh ------- RESULT: 3669h Result = 3669h (must fit in 16 bits) Zero = 0 Sign = 0 Carry = 1 Overflow = 1 (negative plus negative cannot be positive) NOT CORRECT for unsigned (carry is on) NOT CORRECT for signed (overflow is on) 41. Add 18-bit AA9Ch to 8BCDh and give the Result, Zero, Sign, Carry, and Overflow. Is the result correct for unsigned math? for signed math? (NOTE: The 18 is not a mistake. These are 18-bit numbers.) In 18-bits, all the above numbers have the sign bit (top bit) *off*. ( AA9Ch written in 18 bits binary is: 00 1010 1010 1001 1100 ) All the numbers have zero in the sign bit; they are all positive numbers. CARRY: 1111 0AA9Ch + 08BCDh ------- RESULT: 13669h (18 bits) Result = 13669h (fits easily in 18 bits) Zero = 0 Sign = 0 (leftmost bit, of 18 bits, is zero) Carry = 0 (no carry out of leftmost bit) Overflow = 0 CORRECT for unsigned (carry is off) CORRECT for signed (overflow is off) Note that 18-bit 13669h = 01 0011 0110 0110 1001 --> a positive number The "carry" now fits in 18 bits - the carry flag does not come on. The sign bit (the leftmost bit) is off - the number is positive. *** Little-Man Computer [LMC] Section *** 42. List the Instruction Cycle activities for the Little Man Computer (LMC). 1. Read and remember the value in the mailbox located at 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 until HALT 43. With reference to the full extended LMC opcode table (LMC_opcodes.html), which LMC instruction(s) would not work "properly" if the LM incremented the counter *after* performing the instruction instead of before? JMP and CALL would not work properly. JMP would go to the wrong location (one after), and CALL would both save the wrong return address (one before) and then jump to the wrong location (one after). 44. What is the purpose of the LMC mnemonic pseudo-instruction DAT? DAT: tell the assembler to reserve and possibly initialize memory (a mailbox) at the current address - i.e. put values into mailboxes (memory) 45. Which LMC instructions set the values of the three LMC light flags? Only ADD and SUB. 46. What is the only way that the N (negative) flag can come on? Give an example. Subtract a larger number from the Calculator, e.g. subtract 4 from 3. Only SUB can do this. 47. Can the N flag and P (positive) flag ever both be on at the same time? Give an example. Never on at the same time. No examples. 48. Can the N flag and Z (zero) flag ever both be on at the same time? Give an example. Never on at the same time. No examples. 49. a) What value appears in the Calculator if you start with 999 and then add one? 50. b) What are the on/off states of the three lights N,Z,P afterward? 999+001=000 N:off Z:on P:on 51. a) What value appears in the Calculator if you start with zero and then subtract one? 52. b) What are the on/off states of the three lights N,Z,P afterward? 000-001=999 N:on Z:off P:off For each of the following nine actions, give the final value in the Calculator and indicate the on/off state of the three LMC indicator lights N, Z, and P. Assume that no lights are on to begin with. Note: no lights change for input or output, only for ADD and SUB 53. a) Input 400 into Calculator. ANSWER: 400 N:off Z:off P:off 54. b) 600 is subtracted. ANSWER: 800 N:on Z:off P:off 55. c) 600 is subtracted again. ANSWER: 200 N:off Z:off P:on 56. d) 600 is subtracted again. ANSWER: 600 N:on Z:off P:off 57. e) 600 is subtracted again. ANSWER: 000 N:off Z:on P:on 58. f) 600 is subtracted again. ANSWER: 400 N:on Z:off P:off 59. g) 600 is added. ANSWER: 000 N:off Z:on P:on 60. h) 600 is added again. ANSWER: 600 N:off Z:off P:on 61. i) 600 is added again. ANSWER: 200 N:off Z:off P:on Full marks are awarded only if you show your method, the same method you will have to use on tests and exams. -- | 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/