Download CO - 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Organization
Assignment 1
Due: March 28, 2008
35 marks
Problems asking for a MIPS program (except the first) expect as solution a running MIPS program (or
procedure stub) conforming to the specifications.
[2 marks]
1.
Consider the following fragment of C code:
for (i=0; i<=100; i=i+1) {a[i] = b[i] + c;}
Assume that the a and b are arrays of words and the base address of a is
in $a0 and the base address of b is in $a1. Register $t0 is associated with
variable i and register $s0 with c. Write the code for MIPS. How many instructions are executed during the running of this code? How many memory
data references will be made during execution?
[2 marks]
2.
Some computers have explicit instructions to extract an arbitrary field from
a 32-bit register and to place it in the least significant bits of a register. The
instruction has operands (with implicit parameters i and j), places the j i
bits (from i 1 to j) of its first operand (a register) at the least significant j i
positions of the second operand (another register).
Find the shortest sequence of MIPS instructions that extracts a field for the
constant values i 5 and j 22 from register $t3 and places it in register
$t0. (Hint: It can be done in two instructions.)
[2 marks]
3.
Given your understanding of PC-relative addressing, explain why an assembler might have problems directly implementing the branch instruction in
the following code sequence:
here:
there:
beq $s0, $s2, there
add $s0, $s0, $s0
Show how the assembler might rewrite this code sequence to solve these
problems.
[2 marks]
4.
Add comments to the following MIPS code and describe in one sentence
what it computes. Assume that $a0 and $a1 are used for the input and both
initially contain the integers a and b, respectively. Assume that $v0 is used
for the output.
loop:
finish:
5.
add
beq
add
sub
j
addi
add
$t0,
$a0,
$t0,
$a1,
loop
$t0,
$v0,
$zero, $zero
$zero, finish
$t0, $a0
$a1, 1
$t0, 100
$t0, $zero
[4 marks]
Write a MIPS procedure to implement the insertion sort algorithm. Assume
that the argument registers $a0 and $a1 contain the address of the first element of the array and the number of elements in the array, respectively.
When the procedure exits, register $a0 should (still!) point to the first element of the array. Take care to save/restore registers properly on procedure
entry and exit.
6.
[4 marks]
Write a (recursive) MIPS procedure to implement the binary search algorithm. Assume that the argument registers $a0, $a1, and $a2 contain the
element to be searched, the address of the first element of the array, and the
number of elements in the array, respectively. When the procedure exits, register $a0 should (still!) point to the first element of the array. Further $v0
should contain the position of the array (starting from 0) in which the element is found, if the element is present in the array. Otherwise $v0 should
contain 1. Take care to save/restore registers properly on procedure entry
and exit.
7.
[6 marks]
(a) Write a procedure, bfind, in MIPS assembly language. The procedure
should take a single argument that is a pointer to a null-terminated
string in register $a0. The bfind procedure should locate the first b
character in the string and return its address in register $v0. If there are
no b’s in the string, then bfind should return a pointer to the null character at the end of the string. For example, if the argument to bfind
points to the string “imbibe,” then the return value will be a pointer to
the third character of the string.
(b) Write a procedure, bcount, in MIPS assembly language. The bcount
procedure takes a single argument, which is a pointer to a string in
register $a0, and it returns a count of the total number of b characters
in the string in register $v0. You must use your bfind procedure from
the first part of this problem.
[3 marks]
8.
Consider addition of two 32-bit numbers a and b represented in 2’s-complement.
An overflow is said to occur when the result is too large to be represented in
2’s-complement with 32 bits. The following is a formula for overflow:
ˆa
31 ` b 31 a 31
Prove the correctness of the formula by considering all the four cases (a negative, b negative; a negative, b positive; etc.).
[2 marks]
9.
A combinational circuit is defined by the following three Boolean functions:
F1
xœ yœ zœ xz, F2
xyœ zœ xœ y, F3
xœ yœ z xy
Design the circuit with a decoder and external gates.
10.
[2 marks]
An 8 1 multiplexer has inputs A, B, and C connected to the selection inputs
S2 , S1 , and S0 , respectively. The data inputs I0 through I7 , are as follows:
I1 I2 I7 0; I3 I5 1; I0 I4 D; and I6 Dœ . Determine the Boolean
function that the multiplexer implements.
11.
[2 marks]
Construct a JK flip-flop using a D flip-flop, a 2-to-1-line multiplexer and an
inverter.
12.
[4 marks]
Design a sequential circuit with two JK flip-flops A and B and two inputs E
and x. If E 0, the circuit remains in the same state regardless of the value
of x. When E 1 and x 1, the circuit goes through the state transitions
from 00 to 01 to 10 to 11 back to 00, and repeats. When E 1 and x 0, the
circuit goes through the state transitions from 00 to 11 to 10 to 01 back to 00,
and repeats.