Upload
aileen-clarke
View
222
Download
0
Embed Size (px)
Citation preview
Ch3a- 2EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Crunching Numbers
• Topics we need to explore
• Representing numbers on a computer• Negatives, too
Hardware Alert!Hardware Alert!
• Building hardware to work with Floating Point numbers
• Building hardware to do logic and math• And, Or• Addition, Subtraction• Comparisons• Multiplication and Division
Ch3a- 3EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Representation
• All data on a computer is represented in binary
• 32 bits of data may be:• 32-bit unsigned integer• 4 ASCII characters• Single-precision IEEE floating point number• Who knows...
data: 1000 1001 0100 0110 0000 0101 0010 1000
As 32-bit unsigned integer: 2,303,067,432
As 32-bit 2’s complement integer: -1,991,899,864
As 4 ASCII characters: ‘??’, ‘F’, ENQ, ‘(‘
Note: Limited ASCII chart on p. 142
Ch3a- 4EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
ASCII Representation of Numbers
• Terminal I/O (keyboard, display) only deals with ASCII characters
• Typically, strings of characters
• “We’re #1” --> 87,101,44,114,101,32,35,49,0
NULL Termination
• Note that the number ‘1’ is represented by 49
• Numbers in I/O consist of their ASCII representations
• To output 103, use ASCII values 49, 48, 51 (3 bytes)• Outputting 103 (one byte) won’t work
‘1’, ‘0’, ‘3’ code for ‘g’
Ch3a- 5EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Number Systems- Negative Numbers
• What about negative numbers?
• We need to represent numbers less than zero as well as zero or higher
• In n bits, we get 2n combinations• Make half positive, half negative...
Sign bit: 0-->positive, 1-->negativeSign bit: 0-->positive, 1-->negative 31 remaining bits for magnitude31 remaining bits for magnitude
First method: use an extra bit for the sign0 000 0000 0000 0000 0000 0000 0000 01011 000 0000 0000 0000 0000 0000 0000 0101
+5
-5
Ch3a- 6EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Sign and Magnitude Representation
• Two different representations for 0!
0000
0111
0011
1011
11111110
1101
1100
1010
1001
1000
0110
0101
0100
0010
0001
+0+1
+2
+3
+4
+5
+6
+7-0
-1
-2
-3
-4
-5
-6
-7
Note: Example is shown for 4-bit numbers
Note: Example is shown for 4-bit numbers
Inner numbers:Binary
representationSeven Positive Numbers and “Positive” Zero
Seven Negative Numbers and
“Negative” Zero
• Number range for n bits = +/- 2n-1 -1
• Three low order bits represent the magnitude: 0 (000) through 7 (111)
• Two discontinuities
• High order bit is sign: 0 = positive (or zero), 1 = negative
Ch3a- 7EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Two’s Complement Representation
• Only one discontinuity now
0000
0111
0011
1011
11111110
1101
1100
1010
1001
1000
0110
0101
0100
0010
0001
+0+1
+2
+3
+4
+5
+6
+7-8
-7
-6
-5
-4
-3
-2
-1
Note: Example is shown for 4-bit numbers
Note: Example is shown for 4-bit numbers
Inner numbers:Binary
representationEight Positive
Numbers
Re-order Negative
Numbers to Eliminate
Discontinuities
• Only one zero
• One extra negative number
Note: Negative numbersstill have 1 for MSB
Note: Negative numbersstill have 1 for MSB
Ch3a- 8EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
2’s Complement Negation Method #1To calculate the negative of a 2’s complement number:
1. Complement the entire number
2. Add one
Examples:
n = 0110= 6
complement n = 01000100 = 68n = 10010000= -1121001
add 1
-n =1010 = -6
complement
10111011add 1
-n =10111100 = -68
complement
01101111add 1
-n =01110000 = 112
WARNING: This is for calculating the negative of a number. There is no such thing as “taking the 2’s complement of a number”.
Ch3a- 9EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
2’s Complement Negation Method #2To calculate the negative of a 2’s complement number:
1. Starting at LSB, search to the left for the first one
2. Copy (unchanged) all of the bits to the right of the first one and the first one itself
Examples:
n = 0110= 6
-n = 1010 = -6
copycomplement
n = 01000100 = 68
-n = 10010111 = -68
copycomplement
n = 10010000= -112
-n = 10000011 = 112
copycomplement
3. Complement the remaining bits
Ch3a- 10
EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Adding Two’s Complement Numbers
4
+ 3
7
0100
0011
0111
-4
+ 3
-1
1100
0011
1111
-4
+ (-3)
-7
1100
1101
11001
4
- 3
1
0100
1101
10001
Just add the completenumbers together.Just add the completenumbers together.
Sign taken care of automatically.Sign taken care of automatically.
Ignore carry-out (for now)Ignore carry-out (for now)
A carry out from sign bit does not necessarily mean overflow!A carry out from sign bit does not necessarily mean overflow!
Ch3a- 11
EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
OverflowAdd two positive numbers to get a negative numberor two negative numbers to get a positive numberAdd two positive numbers to get a negative numberor two negative numbers to get a positive number
5 + 3 = -85 + 3 = -8
-7 - 2 = +7-7 - 2 = +7
Overflow cannot occur when adding a positive and negative number together
0000
0111
0011
1011
11111110
1101
1100
1010
1001
1000
0110
0101
0100
0010
0001
+0+1
+2
+3
+4
+5
+6
+7-8
-7
-6
-5
-4
-3
-2
-1
Overflow occurs when crossing discontinuity
Not a discontinuity - No Overflow
A carryout from the MSB could mean crossing at either of these places – One is OK, one is Overflow
Ch3a- 12
EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Detecting OverflowOverflow occurs when:
We add two positive numbers and obtain a negative
Looking at the sign bit (MSB):
0+ 0
0Cin
0Cout
+
++
0+ 0
1Cin
0Cout
+
+-
No overflow Overflow
We add two negative numbers and obtain a positive
1+ 1
0Cin
1Cout
-
-+
1+ 1
1Cin
1Cout
-
--
Overflow No Overflow
Overflow when carry in to sign bit does not equal carry outOverflow when carry in to sign bit does not equal carry out
Cin
Cout
Overflow
0 110
1+ 0
0Cin
0Cout
-
+-
No Overflow
1
1+ 0
1Cin
1Cout
-
++
No Overflow
0
Ch3a- 13
EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Signed and Unsigned operationsConsider the following:
$t0 = 0000 0000 0000 0000 0000 0000 0000 0101
$t1 = 1111 1111 1111 1111 1111 1111 1111 1001
execute: slt $s0, $t0, $t1
What’s the result?
If we mean for $t0 to be 5 and $t1 to be 4,294,967,289 (treatas unsigned integers) then $s0 should get 1
If we mean for $t0 to be 5 and $t1 to be -7 (treat as signedintegers) then $s0 should get 0
The default is to treat as signed integers
Use sltu for unsigned integers
Ch3a- 14
EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Using more bits
What’s different between 4-bit 2’s complement and 32-bit?
MSB has moved from bit 3 to bit 31!
Copy MSB of original number into all remaining bits
0111
1010
710710
(32-bit 2’s comp.)
-610-610 (32-bit 2’s comp.)
Sign ExtensionSign Extension
0000 0000 0000 0000 0000 0000 0000 0111
1111 1111 1111 1111 1111 1111 1111 1010
4-bit2’s comp.
To convert from 4-bit 2’s complement to 32-bit: Copy all 4 bits to 4 Least significant bits of the 32-bit number.
Ch3a- 15
EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Loading a single byte from memory
We can read a single 8-bit byte from memory location 3000 by using:
lb $t0, 3000($0) # read byte at mem[3000]
assuming mem[3000] = 0xF3, we get...
$t0: 0xFFFFFFF3 (sign-extension for other 3 bytes)
If we only want the byte at 3000 (without extension), used an unsigned load:
lbu $t0, 3000($0) # read byte a mem[3000]
$t0: 0x000000F3 (no sign-extension)
0x prefix means Hex