Tuesday, 20 October 2015 Check your exams! (problem 10) --see email Questions about yesterday’s lab? (C pointers and arrays, shift operators, masks) Please complete the survey (see email) Department coffee/tea today at 2 p.m. Sign up for Gator Day lunch (next Tuesday) Today: Begin chapter 3 in P&H
Arithmetic at the Machine Level We’ve already talked about addition and subtraction: ■ Example: 7 + 6 Answer: … 0 0 1 1 0 1 = 13
Arithmetic at the Machine Level Subtraction: ■ Example: 7 - 6 = 7 + (-6) 1 1 1 0 0 0 Answer: … 0 0 0 0 1 = 1
Arithmetic at the Machine Level Overflow if result out of range Example : 2 31 -1 = 0x7FFFFFFF (largest positive) (2 31 -1) + 1 = -2 31 = 0x8000000 (smallest negative) Similarly, subtracting one from -2 31 causes wraparound to largest positive. Never any overflow when adding two values with opposite signs!
Arithmetic at the Machine Level
Arithmetic at the Machine Level When overflow occurs, what do we do? ● Ignore it (C, Java)? ● Terminate program with an error? ● Set some kind of internal flag that can be checked and handled? MIPS has add and addi (throw an exception), addu and addiu (ignore overflow). (See sample program in oct20 folder of shared repo.)
Addition and Subtraction--Efficiency How long does it take to add two 32-bit numbers? Problem: “carry propagation”. Consider: 1 1 1 1 1 1 1 1 1 1 1 1 1 CARRY: ...0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 + ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ...0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
Addition and Subtraction--Efficiency There are ways to avoid long chains of carry bits using hardware: “carry lookahead”. We may touch on it in chapter 4. Basic idea: examine more than just two bits at a time (requires additional circuitry).
What About Multiplication? Basic idea: repeated adding and shifting. Example: 7x9 (...00111 x ...01001 in binary): ...00111 ...01001 +...00000 +...01001 OR +...00000 +...01001 +...00111 =...0111111 = ...0111111 Repeat 32 times (assume positive):
Definitions: MULTIPLIER 10001 MULTIPLICAND x 01101 MULTIPLIER 0 = =11011101 RIGHTMOST BIT OF MULTIPLIER See programs “mult3-4.c” and “mverbose.c” in the oct20 folder of the shared repository.
Recommend
More recommend