Você está na página 1de 7

Introduction to Computers and Programming Midterm (2009)

*Please answer the following questions by the order of question numbers on the answer sheets in Chinese or English. You may keep these question sheets after the examination. Total 110 pts 1. (8 pts) Consider the following circuit:

A. List all the possible input bit patterns and their corresponding outputs. B. What Boolean operation does the circuit compute in fact? A. 0, 0: 0 0, 1: 1 1, 0: 1 1, 1: 0 B. XOR operation 2. (8 pts) Express the following values in binary notation A. 96 B. 3 5/8 A. 1100000 B. 11.101

3. (8 pts) Perform the following additions in binary notation A. 1010.001 + 1.101 B. 111.11 + 10.01 A. 1011.110 B. 1010.00 4. (4 pts) How many cells can be in a computer s main memory if each cells address can be represented by two hexadecimal digits? 256 cells. Two-hexadecimal-digit address can represent addresses from 00 to FF, a total of 256 addresses, thus it support 256 memory cells. 5. (8 pts) We know the ASCII code for the character A is 41 (in hexadecimal notation), B is 42, C is 43, and so on. Encode the message NCTU in ASCII, using one byte per character. Show the message in both hexadecimal notation and binary notation.

hexadecimal notation: 4E 43 54 55, binary notation: 01001110 01000011 01010100 01010101 6. (8 pts) Encode each of the following commands in terms of the machine language described in the language description table. A. __________ JUMP to the instruction at address B2 if the content of register 2 equals that of register 0. B. __________ AND the contents of registers 5 and 6, leaving the result in register 4. A. B2B2 B. 8456(8465) 7. (8 pts) If Registers 0 and 1 contain the patterns B5 and F0, respectively, what will be in register 1 after executing each of the following instructions? (Refer to the language description table in Appendix.) A. A102 __________ B. 4001 __________ A. 3C B. B5 8. (8 pts) The following table shows a portion of a machine's memory containing a program written in the language described in the language description table (see Appendix). What will happen if the machine is started with its program counter containing 00? address content 00 21 01 02 03 04 05 B0 31 04 C0 00

The machine will change the last instruction to a jump instruction and continue to repeat the same routine over and over. 9. (8 pts) (A) Summarize briefly the booting process for a typical PC. You need to explain why ROM is needed in the booting process. (B) Today most of our smart phones have some sort of operating systems running. We know there is no hard drive in our smart phones, but the booting process is almost the same. Where the OS is most likely stored when a smart phones power is turned off? A. The machine begins by executing a program, called the bootstrap, at a predetermined location in the machines ROM. This program directs the machine to load a program (the operating system) from mass storage into main memory.

Once the operating system has been placed in main memory, the bootstrap tells the machine to transfer its attention to the program just loaded. ROM is needed to store the bootstrap program, since the main memory is volatile, and all its content will be lost when the power is turned off. B. Stored on a Flash ROM. It is a type of non-volatile memory that is used to store the OS and other built-in software. When the power is off, the data stored in ROM are still present, while all the data stored in RAM will be lost. 10. The state of a process is kept in a process table when the process is running. A. What minimum state information needs to be kept in the table for each process? B. How does the OS prevent normal programs from modifying the process table directly? A. Program counter, special-purpose registers, related portion of main memory B. By having CPU operate in one of two modes: privileged mode (level) and non-privileged mode (level). An attempt from a normal program (which must run in non-privileged mode) to execute a privileged instruction like modifying the process table causes an interrupt. This interrupt converts the CPU to privileged mode and transfers control to an interrupt handler within the operating system. The operating system can then take appropriate actions for it. 11. (4 pts) Summarize briefly the steps performed by the CPU when an interrupt occurs. The CPU completes its current machine cycle, saves the state of the current process, and sets its program counter to a predetermined value (which is the location of the interrupt handler). Thus the next instruction executed will be the first instruction within the interrupt handler. 12. (8 pts) If each time slice in a multiprogramming system is 100 milliseconds and each context switch requires at most a microsecond, does this means the machine can service at most about 10 processes in one second? Why or why not? No. Since if processes did not consume their entire time slices, its time-slice will be terminated and another process will be scheduled to run. Therefore the number of processes executed in one second could be much higher than 10 processes, but then the time required to perform a context switch might become more significant.

13. (10 pts) We know the equation 1 + 2 + + n = n(n + 1)/2. Assume you have designed a GUI that looks like:

, In the GUI the text box is named txtNum and the button named resultButton. Write a code segment associated with resultButton which when clicked, will (1) declare integer variables n, sum1, and sum2, (2) set n with the value from txtNum, (3) compute the right-hand-side expression of the equation above and put the result in sum1, (4) compute the left-hand-side expression using a for loop or while loop and put the result in sum2, (5) compare sum1 and sum2; if they are equal, set the text property of resultButton to Both are XXX where XXX is the value in sum1, or to Something wrong! otherwise. For example, the result of clicking the button in the above GUI may become:

(1) Dim n As Integer = 0 Dim sum1 As Integer = 0 Dim sum2 As Integer = 0 (2) n = txtNum2.Text (3) sum1 = (n * (n + 1)) / 2 (4) For i As Integer = 1 To n sum2 += i Next (5) If sum1 = sum2 Then resultButton.Text = "Both are " & sum1 Else resultButton.Text = "Something wrong!" End If

14. (12 pts) A running VB program has its GUI looks like:

In the GUI, the name of the text box is changed to txtNum; the code associated with the Do It button (when it is clicked) is shown below: Dim num As Integer = txtNum.Text num = num \ 2 txtNum.Text = num In the code above, the \ operator returns the integer quotient of a division. For example, the expression 14 \ 4 evaluates to 3. A. What does the GUI show when the button is clicked 1, 2, 3, 4, and 5 times, respectively (assuming in the beginning the text box contains 9) B. There is a function called IsNumeric() which takes one argument and returns true if the argument is a valid numeric string, and false otherwise. Change the code so that if the user enters an invalid string the program will set the text box to 0 and continue working, otherwise the program works as before. C. VBs modulus operator is Mod. That is, it divides two numbers and returns the remainder. For example, (5 Mod 3) evaluates to 2 (divide 5 by 3 and the remainder is 2). One can use Mod to decide if a number is even or odd (e.g. 9 Mod 2 = 1 and 4 Mod 2 = 0). Modify the original program using while loop so that when the button is clicked, the decimal number in the text box will be converted into binary form (i.e. in binary notation) and shown in the same text box again. For example, using the GUI example above, after the button is clicked, the new GUI will look like:

Of course the user may enter a number other than 9, but let s assume the user only enters positive integers. Also note that you can add 0 to the beginning of a string variable txt with:

txt = 0 & txt and similarly add 1 with txt = 1 & txt A. 1: 4 2: 2 3: 1 4: 0 5: 0 B. Dim num As Integer = 0 If IsNumeric(txtNum.Text) Then num = txtNum.Text num = num \ 2 txtNum.Text = num Else txtNum.Text = 0 End If C. Dim number As Integer = txtNum.Text Dim str As String = "" Dim dividend As Integer = number Dim remainder As Integer = 0 If dividend = 0 Then str = dividend & str End If While dividend <> 0 remainder = dividend Mod 2 str = remainder & str dividend \= 2 End While txtNum.Text = str

Appendix (From Append C of the Text Book)


Opcode Operand
1

Description

RXY LOAD the register R with the bit pattern found in the memory cell whose address is XY. Example: 14A3 would cause the contents of the memory cell located at address A3 to be placed in register 4. RXY LOAD the register R with the bit pattern XY. Example: 20A3 would cause the value A3 to be placed in register 0. RXY STORE the bit pattern found in register R in the memory cell whose address is XY. Example: 35B1 would cause the contents of register 5 to be placed in the memory cell whose address is B1. 0RS MOVE the bit pattern found in register R to register S. Example: 40A4 would cause the contents of register A to be copied into register 4. RST ADD the bit patterns in registers S and T as though they were twos complement representations and leave the result in register R. Example: 5726 would cause the binary values in registers 2 and 6 to be added and the sum placed in register 7. RST ADD the bit patterns in registers S and T as though they represented values in floating-point notation and leave the floating-point result in register R. Example: 634E would cause the values in registers 4 and E to be added as floating-point values and the result to be placed in register 3. RST OR the bit patterns in registers S and T and place the result in register R. Example: 7CB4 would cause the result of ORing the contents of registers B and 4 to be placed in register C. RST AND the bit patterns in register S and T and place the result in register R. Example: 8045 would cause the result of ANDing the contents of registers 4 and 5 to be placed in register 0. RST EXCLUSIVE OR the bit patterns in registers S and T and place the result in register R. Example: 95F3 would cause the result of EXCLUSIVE ORing the contents of registers F and 3 to be placed in register 5. R0X ROTATE the bit pattern in register R one bit to the right X times. Each time place the bit that started at the low-order end at the high-order end. Example: A403 would cause the contents of register 4 to be rotated 3 bits to the right in a circular fashion. RXY JUMP to the instruction located in the memory cell at address XY if the bit pattern in register R is equal to the bit pattern in register number 0. Otherwise, continue with the normal sequence of execution. (The jump is implemented by copying XY into the program counter during the execute phase.) Example: B43C would first compare the contents of register 4 with the contents of register 0. If the two were equal, the pattern 3C would be placed in the program counter so that the next instruction executed would be the one located at that memory address. Otherwise, nothing would be done and program execution would continue in its normal sequence. 000 HALT execution. Example: C000 would cause program execution to stop.

2 3

4 5

Você também pode gostar