Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computers - 4 th exam- 授課教授:李錫智. Hints: ADD [DR] [SR1] [SR2] ------- 1010000100 DR SR1 SR2 SUB [DR] [SR1] [SR2] ------- 1010001000 DR.

Similar presentations


Presentation on theme: "Introduction to Computers - 4 th exam- 授課教授:李錫智. Hints: ADD [DR] [SR1] [SR2] ------- 1010000100 DR SR1 SR2 SUB [DR] [SR1] [SR2] ------- 1010001000 DR."— Presentation transcript:

1 Introduction to Computers - 4 th exam- 授課教授:李錫智

2 Hints: ADD [DR] [SR1] [SR2] ------- 1010000100 DR SR1 SR2 SUB [DR] [SR1] [SR2] ------- 1010001000 DR SR1 SR2 LOAD [DR] [MEM] ------- 100000010 DR MEM STORE [MEM] [SR] ------- 100000100 MEM SR MOVE [DR] [SR] ------- 100100010000 DR SR HALT ------- 1111111111111111 where each of the DR, SR, SR1, and SR2 fields contains 2 bits, and each MEM field contains 5 bits.

3 1.What task would the following assembly-language program perform? LOAD R0 10 ADD R0 R0 R0 STORE 10 R0 HALT Ans: 將 MEM[10] 的值,乘以 4 ,再存回 MEM[10]

4 2. How many cycles are required to execute the instructions in Problem 1? Describe the settings for each cycle. Ans: 5 cycles. (1) 初始化;執行 Load R0 10 (2) ADD R0 R0 R0 (3) ADD R0 R0 R0 (4) STORE 10 R0 (5) HALT A AddressB AddressALU OpSwitch PosC AddressRW Address 00 10100001010 A AddressB AddressALU OpSwitch PosC AddressRW Address 00 10010000000 A AddressB AddressALU OpSwitch PosC AddressRW Address 00 10010000000 A AddressB AddressALU OpSwitch PosC AddressRW Address 00 0101010001010 A AddressB AddressALU OpSwitch PosC AddressRW Address 00 00000000000

5 3.Write a sequence of assembly-language instructions that would add the contents of memory locations 10 and 11 then store the result in memory location 12. Ans: LOAD R0 10 LOAD R1 11 ADD R2 R0 R1 STORE 12 R2 HALT

6 4. What sequence of machine-language instructions corresponds to the assembly-language instruction set you wrote in problem 3? Ans: LOAD R0 10  1000000100001010 LOAD R1 11  1000000100101011 ADD R2 R0 R1  1010000100100001 STORE 12 R2  1000001000110010 HALT  1111111111111111

7 5.How many cycles are required to execute the instructions in Problem 4? Describe the settings for each cycle. Ans:5 cycles (1) 初始化; LOAD R0 10 (2) LOAD R1 11 (3) ADD R2 R0 R1 (4) STORE 12 R2 (5) HALT A AddressB AddressALU OpSwitch PosC AddressRW Address 00 10100001010 A AddressB AddressALU OpSwitch PosC AddressRW Address 00 1010010101011 A AddressB AddressALU OpSwitch PosC AddressRW Address 000101 10011000000 A AddressB AddressALU OpSwitch PosC AddressRW Address 101010010101010001100 A AddressB AddressALU OpSwitch PosC AddressRW Address 00 00000000000

8 6.Consider the following program: Pig Latin function PL(word) { var index; index = word.search(/[aeiouAEIOU]/); if (index <= 0) { return word + "way"; } else { return word.substring(index, word.length) + word.substring(0,index) + "ay"; }

9 function PP(phrase) { var words, pig, index; words = phrase.split(/[,\n]+/); if (words.length == 0) { return ""; } else { pig = PL(words[0]); index = 1; while (index < words.length) { pig = pig + " " + PL(words[index]); index = index + 1; } return pig; }

10 Enter a phrase: <input type="button" value="Convert to PL" onclick="document.getElementById('pW').value = PP (document.getElementById('word').value);" /> In PL: What will happen if the user types in the string “Oh, how pretty and handsome you are” and hits the button? Ans: Ohway owhay ettypray andway andsomehay ouyay areway

11 7.Do the following: (a). Consider the following program: function nsysu(string) { var i, str; str = string; i = str.search(/[aeiou]/); while (i != -1) { str = str.substring(0, i) + "*" + str.substring(i+1,str.length); i = str.search(/[aeiou]/); } return str; } What will be returned for the function call nsysu(“I love nsysu”)? Ans:I l*v* nsys*

12 7.Do the following: (b). Consider the following program: function IsP(string) { var stripped, front, back; stripped = string; front = 0; back = stripped.length - 1; while (front < back) { if (stripped.charAt(front) != stripped.charAt(back)) { return false; } front = front + 1; back = back - 1; } return true; } What will be returned for the function call IsP(“usysu”)? Ans: True

13 8.Write a function named Reverse that takes a string as input and returns a copy of that string in reverse order. For example, the function call Reverse("abcd") should return the string "dcba". Ans: Number_8

14 function Reverse(str) { var len,restr; len=str.length; restr=""; i=len-1; while(i!=-1) { restr=restr+str.charAt(i); i=i-1; } document.getElementById('output').value=restr; } Enter a string: Reverse String:

15 9.Write a function named Remove that removes all spaces and commas from a given string. For example, the function call Remove(“Hey, you there”) should return the string “Heyyouthere”. Ans: Number_9

16 function Remove(phrase) { var words=phrase.split(/[,]+/); newphrase=""; index=0; while(index<words.length) { newphrase=newphrase+words[index]; index=index+1; } document.getElementById('output').value=newphrase } Enter a phrase: The result:

17 10.Consider the following circuit. A, B, and C are inputs. P and Q are outputs. Please list the input-output relationship in a truth table.

18 Ans: ABCPQ 00000 10010 01010 00110 11001 10101 01101 11111


Download ppt "Introduction to Computers - 4 th exam- 授課教授:李錫智. Hints: ADD [DR] [SR1] [SR2] ------- 1010000100 DR SR1 SR2 SUB [DR] [SR1] [SR2] ------- 1010001000 DR."

Similar presentations


Ads by Google