Introduction to Computer Science Homework 1

advertisement
Introduction to Computer Science
Homework 1
1. In each circuit below, the rectangles represent the same type of gate. Based
on the input and output information given, identify whether the gate
involved is an AND, OR, or XOR. (8 points)
a. AND
1
?
?
0
?
1
?
0
0
b. OR
0
?
1
c. XOR
1
?
1
1
?
2. If the input and output bit patterns in the circuit below are interpreted as
binary representations of numeric values, what operation does the circuit
perform? (8 points) (You would get only part of grade if your answer is
not complete.)
ANS: The circuit subtracts one (except for the case of the input being 000).
3. Solve each of the following problems by translating the values into two’s
two
complement notation (using patterns of five bits), converting any
subtraction problem to an equivalent addition problem, and performing that
addition. (8 points)
Example:
a.
b.
c.
d.
e.
4. Which of the following values cannot be represented accurately in the
floating-point
nt format introduced in the bel2w
bel2 figure. (88 points)
points (If your
answer only contains
contain “b” or “d”,, you would get half of grade)
grade
a. 6 12
b. 9
c. 1316
d. 1732
ANS: b would require too large of an exponent. d would require too many
significant digits.
5. If a term paper consisted 42 pages, each containing 40 lines of 100 symbols
each (counting each space as a symbol), was to be encoded using Unicode,
how many bytes of storage space would be required? (5 points)
ANS: 336,000 bytes (168,000 symbols times 2 bytes per symbol)
6. Explain why adding only a few characters to a text file may increase the
file’s size by several hundred bytes and at other times may not increase the
file’s size at all. (5 points)
ANS: File space is allocated in terms of physical records, each of which
is several hundred bytes in size. Thus, the size of a file grows by
physical record units rather than by byte size units.
7. In which of the following situations does an overflow never occur? Justify
an answer. (12 points) (8 points from the answer “B, D”. 4 points from
the explanation of your answer)
(a) Adding two positive integers
(b) Adding one positive integer to a negative integer
(c) Subtracting one positive integer from a negative integer.
(d) Subtracting two negative integers.
ANS: (a) Overflow can occur because the magnitude of the result is
greater than the magnitude of each number and could fall out of the
presentable range.
(b) Overflow does not occur because the magnitude of the result is
smaller than one of the numbers; the result is in the presentable range.
(c) When we subtract a positive integer from a negative integer, the
magnitudes of the numbers are added. This is the negative version of
case a. Overflow can occur.
(d) When we subtract two negative numbers, the magnitudes are
subtracted from each other. This is the negative version of case b.
Overflow does not occur.
→ overflow does not occur in (b) and (d).
8. Multiple Choice Questions (單選題) (10 points)
i.
Which of the following is not contained in a CPU? ANS: D
A. Instruction register
B. Program counter
C. General-purpose register D. Memory cell
ii.
In which of the following locations is information most readily
available for manipulation by the CPU? ANS: A
A.
iii.
General-purpose registers
C. Mass storage
B. Parallel
C. Neither A nor B
Which of the following instructions does not fall in the category of
arithmetic/logic instructions? ANS: D
A.
v.
Main memory
The bus in a computer is an example of which form of
communication? ANS: B
A. Serial
iv.
B.
ROTATE
B.
ADD
C.
OR
D.
JUMP
Which of the following is not an activity performed entirely within a
CPU? ANS: A
A. Fetch instructions
B. Perform Boolean operations
C. Perform arithmetic operations
D. Move data between registers
9. Describe the machine cycle. (9 points)
ANS: Fetch an instruction and increment the program counter, decode
the instruction, and execute the instruction.
10. Suppose three values x, y, and z are stored in a machine’s memory. Describe
the sequence of events (such as, loading registers from memory, saving
values in memory, and so on) that leads to the computation (9 points)
a. x + y + z
b. (2x) + y
ANS: To compute x + y - z, each of the values must be retrieved from
memory and placed in a register, the sum of x and y must be computed
and saved in another register, z must be subtracted from that sum, and
the final answer must be stored in memory.
A similar process is required to compute (2x) + y. The point of this
example is that the multiplication by 2 is accomplished by adding x to
x.
11. Suppose the memory cells at addressed 00 through 05 in the machine
described in Appendix C of the textbook contain the following bit patterns:
(9 points)
Address
Content
00
21
01
04
02
31
03
00
04
C0
05
00
Assuming the program counter initially contained 00, record (a) the
contents of the program counter, (b) instruction register, and (c) memory
cell at address 00 at the end of each fetch phase of the machine cycle until
the machine halts.
ANS:
12. Suppose the memory cells at addressed 00 through 0D in the machine
described in Appendix C of the textbook contain the following bit patterns:
(9 points)
Address
Contents
00
20
01
03
02
21
03
01
04
40
05
12
06
51
07
12
08
B1
09
0C
0A
B0
0B
06
0C
C0
0D
00
Assume that the machine starts with its program counter containing 00.
(a) What bit pattern will be in register 1 when the machine halts?
(b) What bit pattern will be in register 0 when the machine halts?
(c) What bit pattern is in the program counter when the machine halts?
ANS: a. 03 b. 03 c. 0E
Download