(5 pts) Exercise 3-1

advertisement
(5 pts) Exercise 3-1
•
Assume we have 4 bits. Convert the given decimal numbers to the stated binary
representations.
5
-7
Unsigned
0101
n.a.
0101
1111
0101
1000
0101
1001
Sign Magnitude
One’s Comp.
Two’s Comp.
(5 pts) Exercise 3-2
•
Convert the given decimal numbers to the stated binary representations.
-3
(using 4 bits)
-3
(using 6 bits)
Sign Magnitude
1011
100011
1100
111100
1101
111101
One’s Comp
Two’s Comp.
(5 pts) Exercise 3-3
•
Assume the following is in binary two’s complement form.
What do they represent in decimal?
8+2+1 = 11
001011
000100+1 = 000101 = -5
111011
•
Now negate these numbers and show the new binary form:
-(001011) =
-(111011) =
110100+1 = 110101
000100+1 = 000101
(10 pts) Exercise 3-6
•
Suppose we use 8 bits to represent a two’s complement binary number. What is
the largest number that can be represented (give answer in binary AND
decimal)
0111 11112 = 12710
•
What is the smallest number that can be represented (give binary AND decimal)
1000 00002 = -12810
(5 pts) Exercise 3-11
•
Do the following assuming 6 bit, two’s complement numbers.
Circle YES if overflow occurs or NO if not •Note carry out here, but
no overflow!
010101
+ 001101
YES
NO
100010
YES
NO
010011
+ 111110
YES
NO
111100
010011
+ 001110
100001
111111
+ 111101
YES
NO
010001 -Adding neg
and pos, so
overflow
impossible
(5 pts) Exercise 3-12
•
Do the following assuming 6 bit, two’s complement numbers.
When does overflow occur? (note subtraction here)
011101
- 100101
YES
NO
111111
- 111101
YES
NO
Negate second: 011011
Result: 111000 – overflow
010011 YES
- 001110 NO
Negate second: 000011
Result: 000010 – okay
010011 YES
- 111110
NO
Negate second: 110010
Result: 000101 – okay
Negate second: 000010
Result: 010101 – okay
(10 pts) Exercise 3-16
(You COULD use a calculator for these. But recommended not – you should be
able to do this by hand on an exam, where calculators are not permitted).
Convert 269ten into a 32-bit two’s complement binary number.
0000 0000 0000 0000 0000 0001 0000 1101
•
Convert -45ten into a 32-bit two’s complement binary number.
45 =
0000 0000 0000 0000 0000 0000 0010 1101
flip:
1111 1111 1111 1111 1111 1111 1101 0010
add 1: 1111 1111 1111 1111 1111 1111 1101 0011 (final result)
MUST include all the leading zeros (as done for 45 above) if
you want to convert a positive number to a negative number.
(10 pts) Exercise 3-17
(You COULD use a calculator for these. But recommended not – you should be
able to do this by hand on an exam, where calculators are not permitted).
What decimal number does this two’s complement binary number represent?
1111 1111 1111 1111 1111 1111 1000 0110two
Looks like a negative number from the sign bit, so let’s negate it to find its
magnitude:
Original:
1111 1111 1111 1111 1111 1111 1000 0110
Flip:
0000 0000 0000 0000 0000 0000 0111 1001
Add 1:
0000 0000 0000 0000 0000 0000 0111 1010
So result is: -1 * (64 + 32 + 16 + 8 + 2) = -122ten
What decimal number does this two’s complement binary number represent?
0000 0000 0000 0000 0000 0000 0101 0110two
Looks like a positive number. Can just add up
= 64 + 16 + 4 + 2 = 86
(5 pts) Exercise 3-21
•
pick:
sll $a1, $a1, 2
# a1 = index * 4
$a0 $a1
add $t0, $a0, $a1 # t0 = &G[index]
Convert the following C code to MIPS:
lwc1 $f0, 0($t0) # retval = G[index]
float pick (float G[], int index) { jr $ra
# return
return G[index];
What would change if G was an array of ints?
}
(just the load instruction)
(5 pts) Exercise 3-22
$f12 $f14
•
Convert the following C code to MIPS:
float max (float A, float B) {
if (A > B) return A / B;
else
return B / A;
}
max:
c.gt.s
bc1f
div.s
j Exit
Else:
div.s
Exit:
jr $ra
$f12, $f14
# is A > B?
Else
$f0, $f12, $f14 # f0 = A-B
$f0, $f14, $f12
# f0 = B-A
# return
(5 pts) Exercise 3-23
$a0 $a1
•
Convert the following C code to MIPS:
float sum (float A[], int N) {
int j;
float sum = 0.0;
for (j=0; j<N; j++)
sum = sum + A[j]
return sum;
}
sum:
li $t0, 0
lwc1 $f0, Zero($gp)
j Test
loop:
sll $t1, $t0, 2
add $t1, $a0, $t1 # t1 = &A[j]
lwc1 $f2, 0($t1) # f2 = A[j]
add.s $f0, $f0, $f2 # sum = sum + A[j]
addi $t0, $t0, 1
# j++;
Test:
blti $t0, $a0, loop
jr $ra
(10 pts) Exercise 3-25
• Convert the following C code into MIPS.
float function2 (float x, float y) {
function2:
if (x > y)
c.gt.s $f12, $f14
return x + y;
bc1f
else
add.s $f0, $f12, $f14
else
j skip
return x – y; else:
sub.s $f0, $f12, $f14
skip:
jr $ra
}
# is x < y?
# if not, go to else
# if so, retval = x + y
# retval = x - y
# return, retval in $f0
(20 pts) Exercise 3-26
•
Convert the following C code into MIPS. A C float is stored as a MIPS
single precision floating point value.
float dotproduct (float A[], float B[]) {
float sum = 0;
int ii;
for (ii = 0; ii < 10; ii++) {
sum = sum + A[ii] * B[ii];
}
return sum;
}
dotproduct:
lwc1
li
loop:
sll
add
add
lwc1
lwc1
mul.s
add.s
addi
blti
jr $ra
$f0, Zero($gp)
$t0, 0
$t1,
$t2,
$t3,
$f2,
$f4,
$f8,
$f0,
$t0,
$t0,
$t0, 2
$a0, $t1
$a1, $t1
0($t2)
0($t3)
$f2, $f4
$f0, $f8
$t0, 1
10, loop
# sum = 0
# ii = 0
#
#
#
#
#
#
#
#
#
#
t1 = 4 * ii
t2 = addr. of A[ii]
t2 = addr. of B[ii]
$f2 = A[ii]
$f4 = B[ii]
$f8 = A[ii] * B[ii]
sum += A[ii]* B[ii]
increment ii
loop if not done
return,retval in $f0
You could also do this by incrementing $a0 and $a1 by four
each time through the loop – this would result in more
efficient code. Recall that pointers (like the array
references here) are always passed as integer arguments.
(10 pts) Exercise 3-27
•
Convert the following C code into MIPS. ASSUME that the result of
multiplying g by h will always fit in just 32 bits.
integers
NOTE 1: using
, not floats, here!
NOTE 2: Use the integer “mult” instruction that we learned in class (that
takes just 2 arguments) NOT a pseudo-instruction that takes 3
arguments
int function6 (int g, int h) {
int prod = g * h;
if (prod < 0)
function6:
prod *= -1; mult $a0, $a1
# compute product
mflo
$v0
# get low 32 bits of product
return prod;
bge $v0, $zero, okay
# skip next part if positive
}
li
$a0, -1
# get -1 constant
okay:
mult $v0, $a0
mflo $v0
jr $ra
# compute $v0 * -1
# get low 32 bits of product
# return, result in $v0
OR An alternative way to multiply by negative one is just to substract from zero:
sub $vo, $zero, $v0
(3 pts EXTRA CREDIT) Exercise 3-31
$a0 $a1
•
Convert the following C code to MIPS:
float
average
(float A[], int N) {
int j;
float sum = 0.0;
for (j=0; j<N; j++)
sum = sum + A[j]
return sum / N;
}
Problem: can’t divide sum (a float) by N (an
integer). So how to get N to a floating point
value?
Strategy: Each time through loop, add 1
(constant from memory) to some fp register.
Then used that to divide at the end
OR: lookup how to convert an integer value
to a FP value (using mtc1 and cvt.s.w)
Download