Arithmetic and Logical Operations

Similar documents
Arithmetic Operations

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

Chapter 4. Operations on Data

Number Systems and Computer Arithmetic

ECE 30 Introduction to Computer Engineering

Tailoring the 32-Bit ALU to MIPS

Chapter 10 Binary Arithmetics

CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: The MIPS ISA (P&H ) Consulting hours. Milestone #1 (due 1/26)

Chapter 3: part 3 Binary Subtraction

Number Systems and Their Representations

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

BINARY SYSTEM. Binary system is used in digital systems because it is:

Chapter 4 Arithmetic Functions

Week 7: Assignment Solutions

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

ECE 341 Midterm Exam

We are quite familiar with adding two numbers in decimal

EE 109 Unit 6 Binary Arithmetic

Arithmetic for Computers

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Binary Arithmetic. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T.

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Chapter 3: Arithmetic for Computers

CS 64 Week 1 Lecture 1. Kyle Dewey

Lecture 8: Addition, Multiplication & Division

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Outline. EEL-4713 Computer Architecture Multipliers and shifters. Deriving requirements of ALU. MIPS arithmetic instructions

CHW 261: Logic Design

Chapter 4. Combinational Logic

Basic Arithmetic (adding and subtracting)

COE 202: Digital Logic Design Number Systems Part 2. Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:

Computer Architecture and Organization

(+A) + ( B) + (A B) (B A) + (A B) ( A) + (+ B) (A B) + (B A) + (A B) (+ A) (+ B) + (A - B) (B A) + (A B) ( A) ( B) (A B) + (B A) + (A B)

Chapter 3 Arithmetic for Computers

Binary Adders. Ripple-Carry Adder

EE260: Logic Design, Spring n Integer multiplication. n Booth s algorithm. n Integer division. n Restoring, non-restoring

UNIT IV: DATA PATH DESIGN

Combinational Circuits

Addition and multiplication

MIPS Integer ALU Requirements

COMPUTER ORGANIZATION AND. Edition. The Hardware/Software Interface. Chapter 3. Arithmetic for Computers

Arithmetic Logic Unit. Digital Computer Design

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

COMP2611: Computer Organization. Data Representation

Principles of Computer Architecture. Chapter 3: Arithmetic

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

At the ith stage: Input: ci is the carry-in Output: si is the sum ci+1 carry-out to (i+1)st state

INF2270 Spring Philipp Häfliger. Lecture 4: Signed Binaries and Arithmetic

Number System. Introduction. Decimal Numbers

Advanced Computer Architecture-CS501

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Review: MIPS Organization

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

COMP MIPS instructions 2 Feb. 8, f = g + h i;

ECE331: Hardware Organization and Design

CS/COE0447: Computer Organization

carry in carry 1101 carry carry

CS/COE0447: Computer Organization

Shift and Rotate Instructions

Integer Multiplication and Division

ECE468 Computer Organization & Architecture. The Design Process & ALU Design

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

Module 2: Computer Arithmetic

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 3. Arithmetic for Computers Implementation

Basic Arithmetic and the ALU. Basic Arithmetic and the ALU. Background. Background. Integer multiplication, division

Computer Architecture Set Four. Arithmetic

Inf2C - Computer Systems Lecture 2 Data Representation

ECE 331: N0. Professor Andrew Mason Michigan State University. Opening Remarks

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS Arithmetic (a) The four possible cases Carry (b) Truth table x y

CPE300: Digital System Architecture and Design

Arithmetic and Bitwise Operations on Binary Data

CPE 335 Computer Organization. MIPS Arithmetic Part I. Content from Chapter 3 and Appendix B

By, Ajinkya Karande Adarsh Yoga

Chapter 5 : Computer Arithmetic

Arithmetic and Bitwise Operations on Binary Data

COMPUTER ARITHMETIC (Part 1)

Department of Electrical and Computer Engineering University of Wisconsin - Madison. ECE/CS 352 Digital System Fundamentals.

Unsigned Binary Integers

Unsigned Binary Integers

ECE 341 Midterm Exam

Xuan Guo. Lecture XIV: Review of Chapter 3 & 4. CSC 3210 Computer Organization and Programming Georgia State University. March 5, 2015.

Number representations

CS 101: Computer Programming and Utilization

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Chapter 3 Arithmetic for Computers. ELEC 5200/ From P-H slides

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

COMP 122/L Lecture 2. Kyle Dewey

Lecture 6: Signed Numbers & Arithmetic Circuits. BCD (Binary Coded Decimal) Points Addressed in this Lecture

Microcomputers. Outline. Number Systems and Digital Logic Review

NUMBER OPERATIONS. Mahdi Nazm Bojnordi. CS/ECE 3810: Computer Organization. Assistant Professor School of Computing University of Utah

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

ECOM4311 Digital Systems Design

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

CS352H: Computer Systems Architecture

Transcription:

Arithmetic and Logical Operations

2 CMPE2c x +y + sum Or in tabular form Binary Addition Carry Out Sum B A Carry In

Binary Addition And as a full adder a b co ci sum 4-bit Ripple-Carry adder: Carry values propagate from bit to bit Like pencil-and-paper addition Time proportional to number of bits Lookahead-carry can propagate carry proportional to log(n) with more space. a b co ci sum a b co ci sum a b co ci sum a b co ci sum CMPE2c 3

Addition: unsigned Just like the simple addition given earlier: Examples: (33) () + (29) + (4) (62) (24) (we are ignoring overflow for now) CMPE2c 4

Addition: 2 s complement Just like unsigned addition Assume 6-bit and observe: (3) + (-4) (-) (-24) + (6) (-8) (-) + (8) (7) Ignore carry-outs (overflow) Sign bit is in the 2 n- bit position What does this mean for adding different signs? CMPE2c 5

Addition: 2 s complement More examples: Convert to 2SC and do the addition -2 + 5 5 + 2-2 + -25 CMPE2c 6

Addition: sign magnitude Add magnitudes only, just like unsigned addition Do not carry into the sign bit If there is a carry out of the MSB of the magnitude, then overflowed Add only integers of like sign (+ to + and to ) Sign of the result is the same as the addends CMPE2c 7

Addition: sign magnitude Examples: (5) + (3) (8) (-) + (-3) (-3) () + (-4) Cannot add!!! This is a subtraction CMPE2c 8

Subtraction General Rules: = = = = = need to borrow Replace (x y) with x + (-y) can replace subtraction with addition to the additive inverse CMPE2c 9

Subtraction: 2 s Complement Don t! Use addition instead (x y) x + (-y) Example: (-) (-) - (3) + (-3) (-3) CMPE2c

Subtraction: 2 s complement Can also flip bits of bottom # and add an LSB carry in, so for - - 3 we get: + add flip bits of bottom number (throw away carry out) Addition and subtraction are simple in 2 s complement, just need an adder and inverters. CMPE2c

Subtraction: Unsigned For n bits, use the two s complement method and underflow if result is negative. Example: (+28) - (+26) Becomes: (+) (+28) ( -25) (+2) only take 5 bits CMPE2c 2

Subtraction: Sign Magnitude If signs are different, change problem to addition If signs are the same, do the subtraction compare the magnitudes subtract smaller on from larger one if order was switched, switch sign of result CMPE2c 3

Subtraction: sign magnitude For example: (7) - (24) becomes (24) - (7) (-7) Switched sign since the order of the subtraction was reversed (-24) - (-2) (-22) Evaluation of the sign bit is not part of the arithmetic, it is determined by comparing magnitudes CMPE2c 4

Overflow in Addition Unsigned: if there is a carry-out of the MSB Ex: (8) (9) () Overflow CMPE2c 5

Overflow in Addition Signed Magnitude: if there is a carryout of the MSB of the magnitude part Ex: (-8) (-9) () Overflow CMPE2c 6

Overflow in Addition 2 s Complement: if the sign of the addends are the same sign and the sign of the result is different: Ex: (3) (6) (-7) Overflow Cannot overflow if addends are different signs. Why not? CMPE2c 7

Underflow in Subtraction Unsigned: if the result is negative. Signed Magnitude: never happens when actually doing subtraction. 2 s Complement: never do subtraction, do addition and use the rules for overflow. CMPE2c 8

Unsigned Binary Multiplication The multiplicand is multiplied by the multiplier to produce the product, the sum of the partial products Example: multiplicand x multiplier = product (+3) x (+6) (+8) Longhand, it looks just like decimal Result can require twice as many bits as the operands. Why? CMPE2c 9

2 s Complement Multiplication If negative multiplicand, just sign-extend it. If negative multiplier, take 2SC of both multiplicand and multiplier (-7 x -3 = 7 x 3, and 7 x 3 = -7 x 3) (3) x (-5) Only need 8 bits for result (-3) x (+5) + (-5) CMPE2c 2

Division Complex operation Only required to know for unsigned binary Ex: 4/3 =? CMPE2c 2

Logical Operations Operate on raw bits with = true and = false In In2 & ~(&) ~( ) ^ ~(^) (AND OR NAND NOR XOR XNOR ) CMPE2c 22

Logical Operations In MAL, done bit-wise in parallel for corresponding bits Example: X = Y = X AND Y =? X OR Y =? X NOR Y =? X XOR Y =? CMPE2c 23

MAL Logical Instructions Operate bit-wise on 32-bit words # x is destination, y & z are sources not x, y and x, y, z nand x, y, z or x, y, z nor x, y, z xor x, y, z xnor x, y, z CMPE2c 24

MAL Logical Instructions Example: a:.word x3 # b:.word xa # # assume $t=a and $t=b and $t2,$t,$t # $t2=$t & $t or $t3,$t,$t # $t3=$t $t CMPE2c 25

MAL Logical Instructions Masking refers to using AND operations to isolate bits in a word (These are SAL instructions. MAL is just the same, but operate on registers rather than words) Example: abcd:.word x6626364 mask:.word xff mask2:.word xff tmp:.word and tmp, abcd, mask beq tmp, d, found d # d == x64 in ASCII How about adding this, does it work? and tmp, abcd, mask2 beq tmp, c, found c # c == x63 in ASCII CMPE2c 26

Logical Operations: Shifts and Rotates Logical right Move bits to the right, same order Throw away the bit that pops off the LSB Introduce a into the MSB (shift right by ) Logical left Move bits to the left, same order Throw away the bit that pops off the MSB Introduce a into the LSB (shift left by 2 ) CMPE2c 27

Logical Operations: Shifts and Rotates Arithmetic right Move bits to the right, same order Throw away the bit that pops off the LSB Reproduce the original MSB into the new MSB Alternatively, shift the bits, and then do sign extension (right by ) (right by 2) Arithmetic left Move bits to the left, same order Throw away the bit that pops off the MSB Introduce a into the LSB (left by ) CMPE2c 28

Logical Operations: Shifts and Rotates Rotate left Move bits to the left, same order Put the bit(s) that pop off the MSB into the LSB No bits are thrown away or lost (rotate by ) (rotate by ) Rotate right Move bits to the right, same order Put the bit that pops off the LSB into the MSB No bits are thrown away or lost (rotate by ) (rotate by 2) CMPE2c 29

MAL shift and Rotate Instructions sll $t, $t, value # shift left logical srl $t, $t, value # shift right logical sra $t, $t, value # shift right arithmetic Example: abcd:.word x6626364 mask:.word xff lw $t, abcd lw $t2, mask and $t3, $t, $t2 srl $t3, $t3, 8 li $t4, c beq $t3, $t4, found c CMPE2c 3