Introduction to Computers - 4 th exam- 授課教授:李錫智
Hints: ADD [DR] [SR1] [SR2] DR SR1 SR2 SUB [DR] [SR1] [SR2] DR SR1 SR2 LOAD [DR] [MEM] DR MEM STORE [MEM] [SR] MEM SR MOVE [DR] [SR] DR SR HALT where each of the DR, SR, SR1, and SR2 fields contains 2 bits, and each MEM field contains 5 bits.
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]
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 A AddressB AddressALU OpSwitch PosC AddressRW Address A AddressB AddressALU OpSwitch PosC AddressRW Address A AddressB AddressALU OpSwitch PosC AddressRW Address A AddressB AddressALU OpSwitch PosC AddressRW Address
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
4. What sequence of machine-language instructions corresponds to the assembly-language instruction set you wrote in problem 3? Ans: LOAD R0 10 LOAD R1 11 ADD R2 R0 R1 STORE 12 R2 HALT
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 A AddressB AddressALU OpSwitch PosC AddressRW Address A AddressB AddressALU OpSwitch PosC AddressRW Address A AddressB AddressALU OpSwitch PosC AddressRW Address A AddressB AddressALU OpSwitch PosC AddressRW Address
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"; }
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; }
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
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*
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
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
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:
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
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:
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.
Ans: ABCPQ