COSC 2410

advertisement
COSC 3430
Homework 1:
(100 points). Please show all your work.
1. 2.2
What binary number does this hexadecimal number
represent: 0x7fff fffa? What decimal number does it
represent?
Answer:
2. 2.3
What hexadecimal number does this binary number
represent:
Answer:
3. 2.4
Why doesn’t MIPS have a subtract immediate instruction?.
Answer:
4. 2.6
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 figure below shows
the desired operation:
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.)
Answer:
2
5. 2.20
Compute the decimal byte values that form the nullterminated ASCII representation of the following string:
Answer:
Figure 2.21 shows the decimal values corresponding to ASCII
characters:
3
6. 2.29
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.
Answer:
4
7. 2.30
For following code fragment processes two arrays and
produces an important value in register $v0. Assume that
each array consists of 2500 words indexed 0 through 2499,
that the base addresses of the arrays are stored in $a0 and
$a1 respectively, and their sizes (2500) are stored in $a2 and
$a3, respectively. Add comments to the code and describe in
one sentence what this code does. Specifically, what will be
returned in $v0?
5
Answer:
6
8. 2.32
Show the single MIPS instruction or minimal sequence of
instructions for this C statement:
Assume that a corresponds to register $t0 and b corresponds
to register $t1.
Answer:
7
9. 2.34
The following program tries to copy words from the address
in register $a0 to the address in register $a1, counting the
number of words copied in register $v0. The program stops
copying when it finds a word equal to 0. You do not have to
preserve the contents of registers $v1, $a0, and $a1. This
terminating word should be copied but not counted.
There are multiple bugs in this MIPS program; fix them and turn in a
bug-free version. Like many of the exercises, the easiest way to write
MIPS programs is to use the simulator.
Answer:
8
10. 2.38
Given your understanding of PC-relative addressing, explain
why an assembler might have problems directly
implementing the branch instruction in the following code
sequence:
Show how the assembler might rewrite this code sequence to solve
these problems.
Answer:
The problem is that we are using PC-relative addressing, so if that
address is too far away, we won’t be able to use 16 bits to describe
where it is relative to the PC. One simple solution would be:
This will work as long as our program does not cross the 256MB
address boundary described in the elaboration on page 98.
9
11. 3.6
What decimal number does this two’s complement binary
number represent:
0111 1111 1111 1111 1111 1111 1110 1111two ?
Answer:
2 147 483 631ten
12. 3.30
Given the bit pattern:
1010 1101 0001 0000 0000 0000 0000 0010
what does it represent, assuming that it is
a. a two’s complement integer?
b. an unsigned integer?
c. a single precision floating-point number?
d. a MIPS instruction?
Answer:
a. -1 391 460 351ten
b. 2 903 506 946ten
c. -8.18546 * 10-12
d. sw $s0, $t0(2)
10
13. 3.43
With x = 0101 1111 1011 1110 0100 0000 0000 0000two,
y = 0011 1111 1111 1000 0000 0000 0000 0000two, and
z = 1101 1111 1011 1110 0100 0000 0000 0000two representing
single IEEE 754 floating-point numbers, perform:
a. x + y
b. (result of a) + z
c. Why is the result counterintuitive?
Answer:
a. 0101 1111 1011 1110 0100 0000 0000 0000
b. 0000 0000 0000 0000 0000 0000 0000 0000
c. We are computing (x+y) + z, where z = -x and y!=0
(x+y) + (-x) = y intuitively
(x+y) + (-x) = y with finite floating-point accuracy
11
14. 2.16.1 4ed
The table below holds various binary values for register $t0:
Suppose that the register $t0 contains a value from the above table
and $t1 has the value:
What is the value of $t2 after the following instructions?
Answer:
12
15. 2.16.3 4ed
Suppose the program counter (PC) is set to 0x0000 0020. It
is possible to use the jump (j) MIPS assembly instruction to
set the PC to the address shown in the data table below ?
Is it possible to use the branch-on-equal (beq) MIPS
assembly language instruction to set the PC to the address as
shown in the data table above?
Answer:
13
16. 2.18.2 4ed
For the table below, translate the C code to MIPS assembly
code. Use a minimum number of instructions. Assume that
the value a, b, i, j are in registers $s0, $s1, $t0, $t1,
respectively. Also, assume that the register $s2 holds the
base address of the array D.
Answer:
14
17. Old Exam 1
Represent the negative of the hexadecimal number DBCA
in hexadecimal as a 32 bit 2’s complement number.
Answer:
0xFFFF2436
15
18. Old Exam 3
Assume that arrayB is located at offset 0x1001 0000. Also
assume that if data is not placed in a byte by the data
statement the byte is filled with the null character 00.
.data
arrayB: .byte 1,2,3,4
arrayW: .half 1000,2000,3000,4000
arrayD: .word 0x42345678
.ascii “00”, “ab”
arrayE: .half 52, 2, 0xAB
arrayF: .word 0x12340000
How will this data be stored in memory starting with the
smallest byte address?
Answer:
This data will be stored in memory as follows with 01 at
the lowest address 10010000
01 02 03 04 E8 03 D0 07 B8 0B A0 0F 78 56 34 42 30 30
61 62 34 00 02 00 AB 00 00 00 34 12
16
19. Old Exam 11
Write a procedure to compute the expression x + y - z and
return the answer in $v0. Assume x, y, and z are passed to
the procedure in $a0, $a1, $a2.
Answer:
.text
Comp:
add $v0, $a0, $a1
sub $v0, $v0, $a2
jr $31
17
20. Old Exam 4
Express the binary number 1011.010011 in 32 bit floating
point representation.
Answer:
First this is 1.011010011 × 23, so 3 + 127 = 130 =
10000010 is the biased exponent. Therefore the number
is
0 10000010 01101001100 ….0
18
Download