physics 202 - La Salle University

advertisement
PHYSICS 202
SPRING 00
LAB 2
More Arithmetic
Part 1. Overflow.
In last week's lab, we made the tacit assumption that the numbers represented were
unsigned integers (i.e. nonnegative integers). If we use N bits, what is the largest
unsigned integer we can represent (assuming the lowest is 0)?
If we add two integers whose sum exceeds our largest integer, we say we have an
"overflow." In the multi-bit adder we made last week, what could be used as an indication
of whether an overflow occurred?
Run the following C++ program in the Microsoft Visual C++ 6.0.
// not an infinite loop
// use to demonstrate overflow
#include<iostream.h>
main()
{
unsigned num;
num=1;
while(num>=1)
{
num=2*num;
cout << num << endl;
}
return 0;
}
Why is not an infinite loop?
What is the largest unsigned integer allowed by this compiler?
How many bits does it use to represent an unsigned integer?
Part 2. Negative Numbers.
We would like to extend the capabilities of our ALU to include subtraction. However,
one of the possible outcomes of subtraction is a negative number. So we will first extend
our representation to include negative numbers. Note that -5 is that number which when
added to +5 gives zero. Assuming we are using eight bits to represent a number and
ignoring the carry out, fill in the number that when added to 5 gives zero.
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
0
+
Fill in a similar table below for the number 83 instead of 5
+
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
And for 60
+
State the pattern you observe for representing the negative of a number in this way.
This is called the 2's complement representation. If you have done it properly, the leftmost (most significant) bit of the negative numbers above should be 1. It can now be
called the sign bit. So what used to be the largest numbers when we were representing
unsigned integers are now negative.
If we use 8 bits, what is the largest integer (signed) we can represent?
What is its binary representation?
What is the smallest (most negative) integer?
What is its binary representation?
Run the following C++ program in the Microsoft Visual C++ 6.0.
// not an infinite loop
// use to demonstrate overflow
#include<iostream.h>
main()
{
int num;
num=1;
while(num>=1)
{
num=2*num;
cout << num << endl;
}
return 0;
}
Why is not an infinite loop?
What is the largest integer allowed by this compiler?
How many bits does it use to represent integer?
Construct the 10's complement of the decimal number 267.
2
6
7
0
0
0
+
Construct the 16's complement of the decimal number 3EB.
3
E
B
0
0
0
+
Part 3. Subtraction.
One can construct the logic of subtraction from scratch, but A-B is just the A+(-B), and
-B is obtained by taking the 2's complement of B. Given that we don't want to build all
new circuitry for subtracting two numbers. Instead we want to add to the circuitry we
already have. First of all there will have to be an extra input which tells us whether we
mean to add or subtract the inputs A and B (0 for add and 1 for subtract). If this input is a
1, we should take the 2's complement of B.
Use full adders and logic gates to build a 4-bit adder/subtractor. Paste a copy of it in this
document.
Hint 1. What is 0B? (Recall  stands for an excluded OR.)
What is 1B?
Hint 2. The carry-in for the least significant bit is useful here.
Part 4. Overflow.
Find the scenarios in which the adder/subtractor may experience overflow. Add some
circuitry that will serve as an overflow flag, i.e. an output that is 1 if overflow has
occurred and 0 otherwise. Paste a copy below.
Part 5. Multiplication.
Perform the following binary multiplication step-by-step.

+
0
0
0
0
0
0
0
0
1
0
0
1
1
1
0
1
Download