CS61C Logical Instructions and MultiplicationDivision Lecture 8 - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

CS61C Logical Instructions and MultiplicationDivision Lecture 8

Description:

In general, can define them to accept 2 inputs, but in the case of MIPS ... Monday is a holiday, no lecture or discussion. No lab on Tuesday, Lab 5 is due Thurs ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 35
Provided by: davep165
Category:

less

Transcript and Presenter's Notes

Title: CS61C Logical Instructions and MultiplicationDivision Lecture 8


1
CS61CLogical Instructions andMultiplication/Divi
sionLecture 8
  • July 1, 1999
  • Nemanja Isailovic

2
Overview
  • Logical Instructions
  • Shifts
  • Multiplication and Division
  • Compilation

3
Bitwise Operations (1/2)
  • Up until now, weve done arithmetic (add, sub,
    mult, div) and memory access (lw and sw)
  • All of these instructions view contents of
    register as a single quantity (such as a signed
    or unsigned integer)
  • New Perspective View contents of register as 32
    bits rather than as a single 32-bit number

4
Bitwise Operations (2/2)
  • Since registers are composed of 32 bits, we may
    want to access individual bits rather than the
    whole.
  • Introduce two new classes of instructions
  • Logical Operators
  • Shift Instructions

5
Logical Operators (1/4)
  • How many of you have taken or are taking Math 55?
  • Two basic logical operators
  • AND outputs 1 only if both inputs are 1
  • OR outputs 1 if at least one input is 1
  • In general, can define them to accept gt2 inputs,
    but in the case of MIPS assembly, both of these
    accept exactly 2 bits as input and produce 1 bit
    as output

6
Logical Operators (2/4)
  • Truth Table standard table listing all possible
    combinations of inputs and resultant output for
    each
  • Truth Table for AND and OR
  • A B AND OR
  • 0 0 0 0
  • 0 1 0 1
  • 1 0 0 1
  • 1 1 1 1

7
Logical Operators (3/4)
  • Logical Instruction Syntax
  • 1 2,3,4
  • where
  • 1) operation name
  • 2) register that will receive value
  • 3) first operand (register)
  • 4) second operand (register) or immediate
    (numerical constant)

8
Logical Operators (4/4)
  • Instruction Names
  • and, or Both of these expect the third argument
    to be a register
  • andi, ori Both of these expect the third
    argument to be an immediate
  • MIPS Logical Operators are all bitwise, meaning
    that bit 0 of the output is produced by the
    respective bit 0s of the inputs, bit 1 by the
    bit 1s, etc.

9
Uses for Logical Operators (1/3)
  • Note that anding a bit with 0 produces a 0 at the
    output while anding a bit with 1 produces the
    original bit.
  • This can be used to create a mask.
  • Example
  • 1011 0110 1010 0100 0011 1101 1001 1010
  • 0000 0000 0000 0000 0000 1111 1111 1111
  • The result of anding these two is
  • 0000 0000 0000 0000 0000 1101 1001 1010

10
Uses for Logical Operators (2/3)
  • The second bitstring in the example is called a
    mask. It is used to isolate the rightmost 12
    bits of the first bitstring by masking out the
    rest of the string (e.g. setting it to all 0s).
  • Thus, the and operator can be used to set certain
    portions of a bitstring to 0s, while leaving the
    rest alone.
  • In particular, if the first bitstring in the
    above example were in t0, then the following
    instruction would mask it
  • andi t0,t0,0xFFF

11
Uses for Logical Operators (3/3)
  • Similarly, note that oring a bit with 1 produces
    a 1 at the output while oring a bit with 0
    produces the original bit.
  • This can be used to force certain bits of a
    string to 1s.
  • For example, if t0 contains 0x12345678, then
    after this instruction
  • ori t0, t0, 0xFFFF
  • t0 contains 0x1234FFFF (e.g. the high-order 16
    bits are untouched, while the low-order 16 bits
    are forced to 1s).

12
Shift Instructions (1/4)
  • Move (shift) all the bits in a word to the left
    or right by a number of bits, filling the emptied
    bits with 0s.
  • Example shift right by 8 bits
  • 0001 0010 0011 0100 0101 0110 0111 1000
  • 0000 0000 0001 0010 0011 0100 0101 0110
  • Example shift left by 8 bits
  • 0001 0010 0011 0100 0101 0110 0111 1000
  • 0011 0100 0101 0110 0111 1000 0000 0000

13
Shift Instructions (2/4)
  • Shift Instruction Syntax
  • 1 2,3,4
  • where
  • 1) operation name
  • 2) register that will receive value
  • 3) first operand (register)
  • 4) second operand (register)

14
Shift Instructions (3/4)
  • MIPS has three shift instructions
  • 1. sll (shift left logical) shifts left and
    fills emptied bits with 0s
  • 2. srl (shift right logical) shifts right and
    fills emptied bits with 0s
  • 3. sra (shift right arithmetic) shifts right and
    fills emptied bits by sign extending

15
Shift Instructions (4/4)
  • Example shift right arith by 8 bits
  • 0001 0010 0011 0100 0101 0110 0111 1000
  • 0000 0000 0001 0010 0011 0100 0101 0110
  • Example shift right arith by 8 bits
  • 1001 0010 0011 0100 0101 0110 0111 1000
  • 1111 1111 1001 0010 0011 0100 0101 0110

16
Uses for Shift Instructions (1/5)
  • Suppose we want to isolate byte 0 (rightmost 8
    bits) of a word in t0. Simply use
  • andi t0,t0,0xFF
  • Suppose we want to isolate byte 1 (bit 15 to
    bit 8) of a word in t0. We can use
  • andi t0,t0,0xFF00
  • but then we still need to shift to the right by
    8 bits...

17
Uses for Shift Instructions (2/5)
  • Instead, use
  • sll t0,t0,16 srl t0,t0,24
  • 0001 0010 0011 0100 0101 0110 0111 1000
  • 0101 0110 0111 1000 0000 0000 0000 0000
  • 0000 0000 0000 0000 0000 0000 0101 0110

18
Uses for Shift Instructions (3/5)
  • In decimal
  • Multiplying by 10 is same as shifting left by 1
  • 71410 x 1010 714010
  • 5610 x 1010 56010
  • Multiplying by 100 is same as shifting left by 2
  • 71410 x 10010 7140010
  • 5610 x 10010 560010
  • etc.

19
Uses for Shift Instructions (4/5)
  • In binary
  • Multiplying by 2 is same as shifting left by 1
  • 112 x 102 1102
  • 10102 x 102 101002
  • Multiplying by 4 is same as shifting left by 2
  • 112 x 1002 11002
  • 10102 x 1002 1010002
  • Multiplying by 2n is same as shifting left by n

20
Uses for Shift Instructions (5/5)
  • Since shifting is so much faster than
    multiplication (you can imagine how complicated
    multiplication is), a good compiler usually
    notices when C code multiplies by a power of 2
    and compiles it to a shift instruction
  • a 8 (in C)
  • would compile to
  • sll s0,s0,3 (in MIPS)

21
Administrivia
  • Lab 4 today, HW 2 tomorrow
  • Lab 5 and Homework 3 will be up before the
    weekend in case you want to work early.
  • Monday is a holiday, no lecture or discussion
  • No lab on Tuesday, Lab 5 is due Thurs
  • HW 3 is still due on Friday

22
Multiplication (1/3)
  • Paper and pencil example (unsigned)
  • Multiplicand 1000 Multiplier
    x1001 1000 0000
    0000 1000 01001000
  • m bits x n bits m n bit product

23
Multiplication (2/3)
  • In MIPS, we multiply registers, so
  • 32-bit value x 32-bit value 64-bit value
  • Syntax of Multiplication
  • mult register1, register2
  • Multiplies 32-bit values in specified registers
    and puts 64-bit product in special result
    registers
  • puts upper half of product in hi
  • puts lower half of product in lo
  • hi and lo are 2 registers separate from the 32
    general purpose registers

24
Multiplication (3/3)
  • Example
  • in C a b c
  • in MIPS
  • let b be s2 let c be s3 and let a be s0 and
    s1 (since it may be up to 64 bits)
  • mult s2,s3 bc mfhi s0 upper half
    of product into s0 mflo s1 lower half
    of product into s1
  • Note Often, we only care about the lower half of
    the product.

25
Division (1/3)
  • Paper and pencil example (unsigned)
  • 1001 Quotient Divisor
    10001001010 Dividend -1000
    10 101 1010
    -1000 10 Remainder (or Modulo result)
  • Dividend Quotient x Divisor Remainder

26
Division (2/3)
  • Syntax of Division
  • div register1, register2
  • Divides 32-bit values in register 1 by 32-bit
    value in register 2
  • puts remainder of division in hi
  • puts quotient of division in lo
  • Notice that this can be used to implement both
    the C division operator (/) and the C modulo
    operator ()

27
Division (3/3)
  • Example
  • in C a c / d
  • b c d
  • in MIPS
  • let a be s0 let b be s1 let c be s2 and let
    d be s3
  • div s2,s3 loc/d, hicd mflo s0 get
    quotient mfhi s1 get remainder

28
More Overflow Instructions
  • In addition, MIPS has versions for these two
    arithmetic instructions which do not detect
    overflow
  • multu
  • divu
  • Otherwise, they are exactly the same as mult and
    div.

29
Compilation (1/3)
  • How turn notation programmers prefer into
    notation computer understands?
  • Make a program to translate C statements into
    Assembly Language instructions called a compiler
  • Big Idea compiler translates notation from 1
    level of abstraction to lower level

30
Compilation (2/3)
  • Example compile by hand this C code a (bc)
    (d-e)
  • In MIPS
  • let a be s0 let b be s1 let c be s2
    let d be s3 let e be s4
  • add t0,s1,s2 abc sub t1,s3,s4
    t0d-e div t0,t1 lot0/t1,hit0t1 mfhi
    s0 a(bc)(d-e)

31
Compilation (3/3)
  • C statement (5 operands, 3 operators) a
    (bc) (d-e)
  • Becomes 4 assembly instructions (7 unique
    operands, 3 operators)
  • In general, each line of C code produces many
    assembly instructions
  • One reason why people program in C vs. Assembly
    fewer lines of code
  • Other reasons?
  • portability
  • time efficiency
  • compiler (sometimes?) does it better

32
Things to Remember (1/3)
  • Memory is byte-addressable, but lw and sw access
    one word at a time.
  • A pointer (used by lw and sw) is just a memory
    address, so we can add to it or subtract from it
    (using offset).
  • Logical and Shift Instructions operate on bits
    individually, unlike arithmetic, which operate on
    entire word.
  • Use Logical and Shift Instructions to isolate
    fields, either by masking or by shifting back and
    forth.

33
Things to Remember (2/3)
  • New Instructions
  • and, andi, or, ori
  • sll, srl, sra
  • mult, multu
  • div, divu

34
Things to Remember (3/3)
  • No lecture Monday, next ones on Tuesday
  • Happy 4th of July!
Write a Comment
User Comments (0)
About PowerShow.com