150 likes | 268 Views
Chapter 3a: Arithmetic and Logic. Crunching Numbers. Topics we need to explore Representing numbers on a computer Negatives, too. Building hardware to do logic and math And, Or Addition, Subtraction Comparisons Multiplication and Division. Hardware Alert!.
E N D
Chapter 3a: Arithmetic and Logic
Crunching Numbers • Topics we need to explore • Representing numbers on a computer • Negatives, too • Building hardware to do logic and math • And, Or • Addition, Subtraction • Comparisons • Multiplication and Division Hardware Alert! • Building hardware to work with Floating Point numbers
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
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’
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... • First method: use an extra bit for the sign • 0 000 0000 0000 0000 0000 0000 0000 0101 • 1 000 0000 0000 0000 0000 0000 0000 0101 +5 -5 Sign bit: 0-->positive, 1-->negative 31 remaining bits for magnitude
Sign and Magnitude Representation -7 +0 Seven Negative Numbers and “Negative” Zero Note: Example is shown for 4-bit numbers +1 -6 0000 1111 0001 1110 -5 +2 1101 0010 -4 +3 Inner numbers:Binary representation 1100 0011 Seven Positive Numbers and “Positive” Zero -3 1011 +4 0100 1010 0101 -2 +5 1001 0110 +6 -1 1000 0111 +7 -0 • High order bit is sign: 0 = positive (or zero), 1 = negative • Three low order bits represent the magnitude: 0 (000) through 7 (111) • Number range for n bits = +/- 2n-1 -1 • Two different representations for 0! • Two discontinuities
Two’s Complement Representation -1 +0 Note: Example is shown for 4-bit numbers +1 -2 0000 Re-order Negative Numbers to Eliminate Discontinuities 1111 0001 1110 -3 +2 1101 0010 -4 +3 Inner numbers:Binary representation 1100 0011 Eight Positive Numbers -5 1011 +4 0100 1010 0101 -6 +5 1001 0110 +6 -7 1000 0111 +7 -8 Note: Negative numbersstill have 1 for MSB • Only one discontinuity now • Only one zero • One extra negative number
complement complement complement add 1 add 1 add 1 2’s Complement Negation Method #1 To calculate the negative of a 2’s complement number: WARNING: This is for calculating the negative of a number. There is no such thing as “taking the 2’s complement of a number”. 1. Complement the entire number 2. Add one Examples: n = 0110= 6 n = 01000100 = 68 n = 10010000= -112 1001 10111011 -n =1010 = -6 01101111 -n =10111100 = -68 -n =01110000 = 112
complement complement complement copy copy copy 2’s Complement Negation Method #2 To 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 3. Complement the remaining bits Examples: n = 01000100 = 68 n = 0110= 6 10111 100 -n = = -68 10 10 -n = = -6 n = 10010000= -112 011 10000 -n = = 112
4 + 3 7 0100 0011 0111 -4 + (-3) -7 1100 1101 11001 4 - 3 1 0100 1101 10001 -4 + 3 -1 1100 0011 1111 Adding Two’s Complement Numbers Just add the completenumbers together. Sign taken care of automatically. Ignore carry-out (for now) A carry out from sign bit does not necessarily mean overflow!
-1 +0 +1 -2 0000 1111 0001 1110 -3 +2 1101 0010 -4 +3 1100 0011 -5 1011 +4 0100 1010 0101 -6 +5 1001 0110 +6 -7 1000 0111 +7 -8 Overflow Add two positive numbers to get a negative number or two negative numbers to get a positive number Not a discontinuity - No Overflow 5 + 3 = -8 -7 - 2 = +7 Overflow occurs when crossing discontinuity A carryout from the MSB could mean crossing at either of these places – One is OK, one is Overflow Overflow cannot occur when adding a positive and negative number together
Cin Cin Cin Cin Cin Cin Cout Cout Cout Cout Cout Cout 0 1 1 0 0 1 0 0 1 1 1 0 0+ 0 0+ 0 1+ 1 1+ 0 1+ 0 1+ 1 Cin Overflow Cout Detecting Overflow Overflow occurs when: We add two positive numbers and obtain a negative We add two negative numbers and obtain a positive Looking at the sign bit (MSB): + + - - - - + + - - + + + - + - - + 0 1 0 1 1 0 No overflow Overflow Overflow No Overflow No Overflow No Overflow Overflow when carry in to sign bit does not equalcarry out
Signed and Unsigned operations Consider 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 5and $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
Using more bits What’s different between 4-bit 2’s complement and 32-bit? MSB has moved from bit 3 to bit 31! Sign Extension 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. Copy MSB of original number into all remaining bits 0000 0000 0000 0000 0000 0000 0000 0111 0111 710 (32-bit 2’s comp.) 710 4-bit2’s comp. 1111 1111 1111 1111 1111 1111 1111 1010 1010 -610 (32-bit 2’s comp.) -610
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) 0x prefix means Hex 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)