Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm.

Similar presentations


Presentation on theme: "Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm."— Presentation transcript:

1 Arithmetic I CPSC 321 Andreas Klappenecker

2 Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm

3 Any Questions?

4 What happened so far? We learned the basics of the MIPS assembly language We briefly touched upon the translation to machine language We formulated our goal, namely the implementation of a MIPS processor.

5 Pipelined MIPS Processor

6 Welcome to the Future! The execution of machine instructions can follow, for example, the steps: Instruction fetch Instruction decode and register read Execute opn. or calculate an address Access operand in data memory Write the result into a register

7 Pipelined MIPS Processor We concentrate first on the arithmetic-logic unit

8 The Arithmetic-Logic Unit Arithmetic (addition and subtraction) we need to know number representations there exist various interesting algorithms for addition and subtraction integer and floating point arithmetic Logical operations (and, or, not)

9 Computer Arithmetic

10 Unsigned Numbers 32 bits are available Range 0..2 32 -1 1101 2 = 2 3 +2 2 +2 0 = 13 10 Upper bound 2 32 –1 = 4,294,967,295

11 Unsigned Numbers If we have n bit unsigned integers, then addition really means a+b mod 2 n For example, if n=4, then 1101 2 + 1001 2 = 13 + 9 = 22 = 6 mod 16 1101 2 + 1001 2 =0110 2

12 Number representations What signed integer number representations do you know?

13 Signed Numbers Sign-magnitude representation MSB represents sign, 31bits for magnitude One’s complement Use 0..2 31 -1 for non-negative range Invert all bits for negative numbers Two’s complement Same as one’s complement except negative numbers are obtained by inverting all bits and adding 1

14 One’s Complement Suppose we want to express -30 as an 8bit integer in one’s complement representation. 30 = 0001 1110 2 Invert the bits to obtain the negative number: -30 = 1110 0001 2

15 Two’s Complement Suppose we want to express -30 as an 8bit integer in two’s complement representation. 30 = 0001 1110 2 Invert the bits to obtain the negative number: 1110 0001 2 Add one: -30 = 1110 0010 2

16 Advantages and Disadvantages sign-magnitude representation one’s complement representation two’s complement representation

17 Signed Numbers (3bits) sign magnitudeone’s complementtwo’s complement 000 2 = 0 001 2 = 1 010 2 = 2 011 2 = 3 100 2 = -0100 2 = -3100 2 = -4 101 2 = -1101 2 = -2101 2 = -3 110 2 = -2110 2 = -1110 2 = -2 111 2 = -3111 2 = -0111 2 = -1

18 Two’s complement The unsigned sum of an n-bit number and its negative yields? Example with 3 bits: 011 2 101 2 1000 2 = 2 n => negate(x) = 2 n -x Explain one’s complement

19 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = +1 ten 0000 0000 0000 0000 0000 0000 0000 0010 two = +2 ten... 0111 1111 1111 1111 1111 1111 1111 1110 two = +2,147,483,646 ten 0111 1111 1111 1111 1111 1111 1111 1111 two = +2,147,483,647 ten 1000 0000 0000 0000 0000 0000 0000 0000 two = –2,147,483,648 ten 1000 0000 0000 0000 0000 0000 0000 0001 two = –2,147,483,647 ten 1000 0000 0000 0000 0000 0000 0000 0010 two = –2,147,483,646 ten... 1111 1111 1111 1111 1111 1111 1111 1101 two = –3 ten 1111 1111 1111 1111 1111 1111 1111 1110 two = –2 ten 1111 1111 1111 1111 1111 1111 1111 1111 two = –1 ten MIPS 32bit signed numbers

20 Conversions How do you convert an n-bit number into a 2n-bit number? (Assume two’s complement representation)

21 Conversions Suppose that you have 3bit two’s complement number 101 2 = -3 Convert into a 6bit two’s complement number 111101 2 = -3 Replicate most significant bit!

22 Comparisons What can go wrong if you accidentally compare unsigned with signed numbers?

23 Comparisons for [un]signed Register $s0 1111 1111 1111 1111 Register $s1 0000 0000 0000 0000 0000 0000 0000 0001 Compare registers (set less than) slt $t0, $s0, $s1true, since –1 < 1 sltu $t1, $s0, $s1 false, since 2 32 -1>1

24 Just like in grade school (carry/borrow 1s) 0111 0111 0110 + 0110- 0110- 0101 1101 0001 0001 Two's complement operations are simple subtraction using addition of negative numbers 0111 = 7 + 1010 = -6 0001 Addition & Subtraction

25 MIPS instructions lb loads a byte and stores the sign- extended version in a word. lbu loads a byte and stores it in a word Which of these two is typically used to process characters?

26 Overflow means that the result is too large for a finite computer word. For instance, adding two n-bit numbers does not yield an n-bit number. Suppose we add two 3-bit numbers 111 + 001 1000 Overflow

27 No overflow when adding a positive and a negative number No overflow when signs are the same for subtraction Overflow occurs when the value affects the sign: overflow when adding two positives yields a negative or, adding two negatives gives a positive or, subtract a negative from a positive and get a negative or, subtract a positive from a negative and get a positive Detecting Overflow

28 OperationOperand AOperand BOverflow if result A+B>=0 <0 A+B<0 >=0 A-B>=0<0 A-B<0>=0

29 An exception (interrupt) occurs Control jumps to predefined address for exception Interrupted address is saved for possible resumption Don't always want to detect overflow MIPS instructions: addu, addiu, subu note: addiu still sign-extends! Effects of Overflow

30 Building an Arithmetic Logic Unit

31 Logic Gates: AND a b c 0 0 0 0 1 0 1 0 0 1 1 1 a b c

32 Logic Gates: OR a b c 0 0 0 0 1 1 1 0 1 1 1 1 a b c

33 Let's build an ALU to support the andi and ori instructions Selection of operation 0 = and, 1 = or we'll just build a 1 bit ALU, and use 32 of them Possible Implementation (sum-of-products): b a operation result An ALU (arithmetic logic unit)

34 Selects one of the inputs to be the output, based on a control input Build (and/or) ALU using a MUX S C A B 0 1 The Multiplexor note: it is called a 2-input mux even though it has 3 inputs!

35 Not easy to decide the “best” way to build something Don't want too many inputs to a single gate for our purposes, ease of comprehension is important Don’t want to have to go through too many gates Let's look at a 1-bit ALU for addition: Different Implementations c out = a b + a c in + b c in sum = a xor b xor c in

36 Different Implementations How could we build a 1-bit ALU for add, and, and or? How could we build a 32-bit ALU?

37 Building a 32 bit ALU

38 Two's complement approach: just negate b and add. How do we negate? A solution: What about subtraction (a – b) ?


Download ppt "Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm."

Similar presentations


Ads by Google