Docsity

Lecture Slides on Arithmetic | Computer Architecture | CS 472, Exams of Computer Architecture and Organization

Partial preview of the text

Download Lecture Slides on Arithmetic | Computer Architecture | CS 472 and more Exams Computer Architecture and Organization in PDF only on Docsity!

Chapter 4: Arithmetic^ •^ Where we've been:^ –^ Performance (seconds, cycles, instructions)^ –^ Abstractions:Instruction Set ArchitectureAssembly Language and Machine Language^ •^ What's up ahead:^ –^ Implementing the Architecture^3232

operation result^32 a^ ALU b

-^ Bits are just bits (no inherent meaning)— conventions define relationship between bits and numbers •^ Binary numbers (base 2)0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...decimal: 0...

n -

Numbers^ •^ Of course it gets more complicated:numbers are finite (overflow)fractions and real numbersnegative numberse.g., no MIPS subi instruction; addi can add a negative number)^ •^ How do we represent negative numbers?i.e., which bit patterns will represent which numbers?

-^ 32 bit signed numbers:^ 0000 0000 0000 0000 0000 0000 0000 0000

= 0two ten 0000 0000 0000 0000 0000 0000 0000 0001

= + 1two ten 0000 0000 0000 0000 0000 0000 0000 0010

= + 2two ten ... 0111 1111 1111 1111 1111 1111 1111 1110

= + 2,147,483,646two ten 0111 1111 1111 1111 1111 1111 1111 1111

= + 2,147,483,647two ten 1000 0000 0000 0000 0000 0000 0000 0000

= – 2,147,483,648two ten 1000 0000 0000 0000 0000 0000 0000 0001

= – 2,147,483,647two ten 1000 0000 0000 0000 0000 0000 0000 0010

= – 2,147,483,646two ten ... 1111 1111 1111 1111 1111 1111 1111 1101

= – 3two ten 1111 1111 1111 1111 1111 1111 1111 1110

= – 2two ten 1111 1111 1111 1111 1111 1111 1111 1111

= – 1two ten

maxintminint

MIPS

-^ Negating a two's complement number: invert all bits and add 1^ –^ remember: “negate” and “invert” are quite different! •^ Converting n bit numbers into numbers with more than n bits:^ –^ MIPS 16 bit immediate gets converted to 32 bits for arithmetic^ –^ copy the most significant bit (the sign bit) into the other bits 0010 -> 0000 00101010 -> 1111 1010 – "sign extension"^ (lbu vs. lb)

Two's Complement Operations

Detecting Overflow^ •^ 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^ •^ Consider the operations A + B, and A – B^ –^ Can overflow occur if B is 0?^ –^ Can overflow occur if A is 0?

-^ An exception (interrupt) occurs^ –^ Control jumps to predefined address for exception^ –^ Interrupted address is saved for possible resumption •^ Details based on software system / language^ –^ example: flight control vs. homework assignment •^ Don't always want to detect overflow— new MIPS instructions:

addu, addiu,^ subu

Effects of Overflow^ note:^ addiu^ still sign-extends!note:^ sltu ,^ sltiu^ for unsigned comparisons

Review: Boolean Algebra & Gates^ •^ Problem: Consider a logic function with three inputs: A, B, and C.Output D is true if at least one input is trueOutput E is true if exactly two inputs are trueOutput F is true only if all three inputs are true^ •^ Show the truth table for these three functions.^ •^ Show the Boolean equations for these three functions.^ •^ Show an implementation consisting of inverters, AND, and OR gates.

-^ Let's build an ALU to support the

andi^ and^ ori^ instructions

-^ we'll just build a 1 bit ALU, and use 32 of themoperationa b • Possible Implementation (sum-of-products):

op^ a result b^ res

an ALU (arithmetic logic unit)

Different Implementations^ •^ Not easy to decide the “best” way to build something^ –^ Don't want too many inputs to a single gate^ –^ Don’t want to have to go through too many gates^ –^ for our purposes, ease of comprehension is important^ •^ Let's look at a 1-bit ALU for addition:^ •^ How could we build a 1-bit ALU for add, and, and or?^ •^ How could we build a 32-bit ALU?

c= a b + a c+ b cout^ in^

in su m = a xor b xor c

in CarryIn a^ Sumb CarryOut

Building a 32 bit ALU^ b

OperationCarryIna^01 Result^2 CarryOut

CarryInOperationa0CarryInResult0ALU0b0CarryOut a1CarryInResult1ALU1b1CarryOut a2CarryInResult2ALU2b2CarryOut a31CarryInResult31ALU31b

Tailoring the ALU to the MIPS^ •^ Need to support the set-on-less-than instruction (slt)^ –^ remember: slt is an arithmetic instruction^ –^ produces a 1 if rs < rt and 0 otherwise^ –^ use subtraction: (a-b) < 0 implies a < b^ •^ Need to support test for equality (beq $t5, $t6, $t7)^ –^ use subtraction: (a-b) = 0 implies a = b

Supporting slt^ •^ Can we figure out the idea?

Operation^0 Result^3 a BinvertCarryIn^101 CarryOut b^

2 Less

Operation^0 Result^3 a BinvertCarryIn^101 b^

2 Less

Set OverflowOverflowdetection a. b.

CarryInOperation a0Result0ALU0^ b0 a1Result1b1 (^0) a2Result2b2 (^0) a31Result31Setb31 0 Overflow Binvert CarryInLessCarryOut CarryInALU1LessCarryOut CarryInALU2LessCarryOut CarryIn CarryInALU31Less

Conclusion^ •^ We can build an ALU to support the MIPS instruction set^ –^ key idea: use multiplexor to select the output we want^ –^ we can efficiently perform subtraction using two’s complement^ –^ we can replicate a 1-bit ALU to produce a 32-bit ALU^ •^ Important points about hardware^ –^ all of the gates are always working^ –^ the speed of a gate is affected by the number of inputs to the gate^ –^ the speed of a circuit is affected by the number of gates in series(on the “critical path” or the “deepest level of logic”)^ •^ Our primary focus: comprehension, however,^ –^ Clever changes to organization can improve performance(similar to using better algorithms in software)^ –^ we’ll look at two examples for addition and multiplication

bc= 33 4 Not feasible! Why?

  • 000 = + Two's Complement
  • 000 = +
  • 000 = +
    • 001 = +
    • 001 = +
    • 001 = +
      • 010 = +
      • 010 = +
      • 010 = +
        • 011 = +
        • 011 = +
        • 011 = +
          • 100 = -
          • 100 = -
          • 100 = -
            • 101 = -
            • 101 = -
            • 101 = -
              • 110 = -
              • 110 = -
              • 110 = -
                • 111 = -
                • 111 = -
                • 111 = -
  • • Is a 32-bit ALU as fast as a 1-bit ALU? • Is there more than one way to do addition? – two extremes: ripple carry and sum-of-productsCan you see the ripple? How could you get rid of it? c= bc+ ac+ a^1 00
    • b
      • c= bc+ ac+ a^2 11
      • bc=
        • c= bc+ ac+ a^3 22
        • bc=
          • c= bc+ ac+ a^4 33