ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.

Slides:



Advertisements
Similar presentations
Integer Arithmetic: Multiply, Divide, and Bitwise Operations
Advertisements

CSE431 Chapter 3.1Irwin, PSU, 2008 CSE 431 Computer Architecture Fall 2008 Chapter 3: Arithmetic for Computers Mary Jane Irwin ( )
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Chapter 3 Arithmetic for Computers. Exam 1 CSCE
Lecture 15: Computer Arithmetic Today’s topic –Division 1.
CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©
Lecture Objectives: 1)Perform binary division of two numbers. 2)Define dividend, divisor, quotient, and remainder. 3)Explain how division is accomplished.
Lecture 9 Sept 28 Chapter 3 Arithmetic for Computers.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 4: Arithmetic / Data Transfer Instructions Partially adapted from Computer Organization.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
CSE431 L03 MIPS Arithmetic Review.1Irwin, PSU, 2005 CSE 431 Computer Architecture Fall 2005 Lecture 03: MIPS Arithmetic Review Mary Jane Irwin (
Computer Systems Organization: Lecture 3
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture Objectives: 1)Explain the relationship between addition and subtraction with twos complement numbering systems 2)Explain the concept of numeric.
Arithmetic for Computers
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
CSE378 Instr. encoding.1 Instruction encoding The ISA defines –The format of an instruction (syntax) –The meaning of the instruction (semantics) Format.
Computer Architecture Chapter 3 Instructions: Arithmetic for Computer Yu-Lun Kuo 郭育倫 Department of Computer Science and Information Engineering Tunghai.
Chapter 3 Arithmetic for Computers (Integers). Florida A & M University - Department of Computer and Information Sciences Arithmetic for Computers Operations.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
True Assembly Language Part I. The Assembly Process The process of translating a MAL code (an assembly language program) into machine code (a sequence.
Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering.
Chapter 3 Arithmetic for Computers. Chapter 3 — Arithmetic for Computers — 2 Arithmetic for Computers Operations on integers Addition and subtraction.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
EI 209 Chapter 3.1CSE, 2015 EI 209 Computer Organization Fall 2015 Chapter 3: Arithmetic for Computers Haojin Zhu ( )
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
MIPS Assembly Language Chapter 13 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Division Check for 0 divisor Long division approach – If divisor ≤ dividend bits 1 bit in quotient, subtract – Otherwise 0 bit in quotient, bring down.
CDA 3101 Spring 2016 Introduction to Computer Organization
Integer Multiplication, Division Arithmetic shift Twice the number of places MIPS multiply unit. mult, multu Significant bits Mfhi, mflo, div, divu Arithmetic.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic6: Logic, Multiply and Divide Operations José Nelson Amaral.
CDA 3101 Spring 2016 Introduction to Computer Organization
Integer Multiplication and Division COE 301 Computer Organization Dr. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University.
Integer Multiplication and Division ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
CSE 340 Computer Architecture Spring 2016 MIPS Arithmetic Review.
Arithmetic for Computers Chapter 3 1. Arithmetic for Computers  Operations on integers  Addition and subtraction  Multiplication and division  Dealing.
/ Computer Architecture and Design
Computer Architecture & Operations I
Computer Architecture & Operations I
Integer Multiplication and Division
COMPUTER ARCHITECTURE & OPERATIONS I
Morgan Kaufmann Publishers Arithmetic for Computers
Computer Organization and Design Instruction Sets - 2
CS 314 Computer Organization Fall Chapter 3: Arithmetic for Computers
Computer Organization and Design Instruction Sets - 2
Morgan Kaufmann Publishers
/ Computer Architecture and Design
Morgan Kaufmann Publishers
CDA 3101 Summer 2007 Introduction to Computer Organization
Computer Organization and Design Instruction Sets
CS352H: Computer Systems Architecture
Lecture 8: Addition, Multiplication & Division
Lecture 8: Addition, Multiplication & Division
Computer Architecture & Operations I
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Flow of Control -- Conditional branch instructions
MIPS Assembly.
Morgan Kaufmann Publishers Arithmetic for Computers
Flow of Control -- Conditional branch instructions
MIPS Arithmetic and Logic Instructions
Number Representation
MIPS Arithmetic and Logic Instructions
Presentation transcript:

ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy, and classes taught by Ryan Kastner at UCSB and Marry Jane Irwin from PSU

Logic and Shift Instructions ECE 15B Spring 2010

Bitwise operations Up until now, instructions have been: – Arithmetic (e.g. add, sub, addi) – Memory access (e.g. lw, sw) – Branches and jumps (e.g. j, beq, bne) All these instructions view contents of registers as a single quantity (such as signed or unsigned integer) New Perspective – View contents of register as 32 individual bits rather than as a single 32 bit number – Introduce two new classes of instructions: Logical operations Shift Instructions ECE 15B Spring 2010

Logical Operations Instructions for bitwise manipulation OperationCJavaMIPS Shift left<< sll Shift right>>>>> srl Bitwise AND&& and, andi Bitwise OR|| or, ori Bitwise NOT~~ nor Useful for extracting and inserting groups of bits in a word ECE 15B Spring 2010

AND Operations Useful to mask bits in a word – Select some bits, clear others to 0 and $t0, $t1, $t $t2 $t $t0 ECE 15B Spring 2010

OR Operations Useful to include bits in a word – Set some bits to 1, leave others unchanged or $t0, $t1, $t $t2 $t $t0 ECE 15B Spring 2010

NOT Operations Useful to invert bits in a word – Change 0 to 1, and 1 to 0 MIPS has NOR 3-operand instruction – a NOR b == NOT ( a OR b ) nor $t0, $t1, $zero $t $t0 Register 0: always read as zero ECE 15B Spring 2010

Shift Operations shamt: how many positions to shift Shift left logical (SLL) – Shift left and fill with 0 bits – sll by i bits multiplies by 2 i Shift right logical (SRL) – Shift right and fill with 0 bits – srl by i bits divides by 2 i (unsigned only) Shift right arithmetic (SRA) – Shift right and fill emptied bits by sign extending Note that shamt (immediate value) is only 5 bits oprsrtrdshamtfunct 6 bits 5 bits ECE 15B Spring 2010

Uses for Shift Instructions Very convenient operation to extract group of bits (e.g. one byte) within a word (e.g. think of operations on 8-bit pixels or 8-bit characters) For example, suppose we want to get bits 8 to 15 from $t0. The code below will do the job sll $t0, $t0, 16 srl $t0, $t0, 24 ECE 15B Spring 2010

Uses for Shift Instructions Since shifting is faster than multiplication, a good compiler (or a good programmer for that matter) usually notices when C code multiplies by a power of 2 and compiles it to a shift instruction For example: a = a*8; (in C) would compile to: sll $s0, $s0, 3 (in MIPS) ECE 15B Spring 2010

Overflow when adding and subtracting numbers ECE 15B Spring 2010

Integer Addition Example: Overflow if result out of range Adding +ve and –ve operands, no overflow Adding two +ve operands Overflow if result sign is 1 Adding two –ve operands Overflow if result sign is 0 ECE 15B Spring 2010

Integer Subtraction Add negation of second operand Example: 7 – 6 = 7 + (–6) +7: … –6: … : … Overflow if result out of range – Subtracting two +ve or two –ve operands, no overflow – Subtracting +ve from –ve operand Overflow if result sign is 0 – Subtracting –ve from +ve operand Overflow if result sign is 1 ECE 15B Spring 2010

Dealing with Overflow Some languages (e.g., C) ignore overflow MIPS solution is 2 kinds of arithmetic instructions to recognize 2 choices – add (add), add immediate (addi) and subtract (sub) cause overflow to be detected assuming operation on two’s compliment – add unsigned (addu), add immediate unsigned (addiu) and subtract unsigned (subu) do not cause overflow detection MIPS signals overflow with an exception (aka interrupt) – an unscheduled procedure call where the EPC contains the address of the instruction that caused the exception ECE 15B Spring 2010

Multiplication and Division Review ECE 15B Spring 2010

Multiply Binary multiplication is just a bunch of right shifts and adds multiplicand multiplier partial product array double precision product n 2n n can be formed in parallel and added in parallel for faster multiplication ECE 15B Spring 2010

Multiplication Start with long-multiplication approach 1000 × Length of product is the sum of operand lengths multiplicand multiplier product ECE 15B Spring 2010

Multiplication Hardware Initially 0 ECE 15B Spring 2010

Optimized Multiplier Hardware multiplicand 32-bit ALU multiplier Control add shift right product ECE 15B Spring 2010

Optimized Multiplier Hardware: Example multiplicand 32-bit ALU multiplier Control add shift right product = = 5 add add add add = 30 ECE 15B Spring 2010

Faster Multiplier Uses multiple adders – Cost/performance tradeoff Can be pipelined Several multiplication performed in parallel ECE 15B Spring 2010

MIPS Multiplication Two 32-bit registers for product – HI: most-significant 32 bits – LO: least-significant 32-bits Instructions – mult rs, rt / multu rs, rt 64-bit product in HI/LO – mfhi rd / mflo rd Move from HI/LO to rd Can test HI value to see if product overflows 32 bits – mul rd, rs, rt Least-significant 32 bits of product –> rd ECE 15B Spring 2010

Division Division is just a bunch of quotient digit guesses and left shifts and subtracts dividend = quotient x divisor + remainder dividend divisor partial remainder array quotient n n remainder n ECE 15B Spring 2010

Division Check for 0 divisor Long division approach – If divisor ≤ dividend bits 1 bit in quotient, subtract – Otherwise 0 bit in quotient, bring down next dividend bit Restoring division – Do the subtract, and if remainder goes < 0, add divisor back Signed division – Divide using absolute values – Adjust sign of quotient and remainder as required n-bit operands yield n-bit quotient and remainder quotient dividend remainder divisor ECE 15B Spring 2010

Division Hardware Initially dividend Initially divisor in left half ECE 15B Spring 2010

Optimized Division Hardware divisor 32-bit ALU quotient Control subtract shift left dividend remainder ECE 15B Spring 2010

Optimized Division Hardware divisor 32-bit ALU quotient Control subtract shift left dividend remainder = = sub rem neg, so ‘ient bit = restore remainder sub rem neg, so ‘ient bit = restore remainder sub rem pos, so ‘ient bit = sub rem pos, so ‘ient bit = 1 = 3 with 0 remainder ECE 15B Spring 2010

Faster Division Can’t use parallel hardware as in multiplier – Subtraction is conditional on sign of remainder Faster dividers (e.g. SRT devision) generate multiple quotient bits per step – Still require multiple steps ECE 15B Spring 2010

MIPS Division Use HI/LO registers for result – HI: 32-bit remainder – LO: 32-bit quotient Instructions – div rs, rt / divu rs, rt – No overflow or divide-by-0 checking Software must perform checks if required – Use mfhi, mflo to access result ECE 15B Spring 2010

Review Instructions so far: add, addi, sub, addu, addiu, subu mult, div, mfhi, mflo, multu, divu lw, sw, lb, lbu, lh, lhu, sb, shw beq, bne, j, slt, slti, sltu, sltui and, andi, or, ori, xor, xori, sll, srl, sra Registers so far C variables: $s0 - $s7 Temporary variables: $t0 - $t9 Zero: $zero ECE 15B Spring 2010