1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.

Slides:



Advertisements
Similar presentations
Chapter Three.
Advertisements

CSCE 212 Chapter 3: Arithmetic for Computers Instructor: Jason D. Bakos.
HEXADECIMAL NUMBERS Code
©UCB CPSC 161 Lecture 6 Prof. L.N. Bhuyan
1 CONSTRUCTING AN ARITHMETIC LOGIC UNIT CHAPTER 4: PART II.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
Arithmetic CPSC 321 Computer Architecture Andreas Klappenecker.
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Assembly Language and Computer Architecture Using C++ and Java
1  2004 Morgan Kaufmann Publishers Chapter Three.
Assembly Language and Computer Architecture Using C++ and Java
MIPS Architecture Arithmetic/Logic Instructions ALU Design – Chapter 4 By N. Guydosh 2/17/04.
1 Chapter 4: Arithmetic Where we've been: –Performance (seconds, cycles, instructions) –Abstractions: Instruction Set Architecture Assembly Language and.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Arithmetic I CPSC 321 Andreas Klappenecker. Administrative Issues Office hours of TA Praveen Bhojwani: M 1:00pm-3:00pm.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
1  1998 Morgan Kaufmann Publishers Chapter Four Arithmetic for Computers.
Chapter 3 Arithmetic for Computers. Arithmetic Where we've been: Abstractions: Instruction Set Architecture Assembly Language and Machine Language What's.
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
3-1 Chapter 3 - Arithmetic Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer Architecture.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
Arithmetic for Computers
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
1 Bits are just bits (no inherent meaning) — conventions define relationship between bits and numbers Binary numbers (base 2)
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
1 CHAPTER 4: PART I ARITHMETIC FOR COMPUTERS. 2 The MIPS ALU We’ll be working with the MIPS instruction set architecture –similar to other architectures.
1 Modified from  Modified from 1998 Morgan Kaufmann Publishers Chapter Three: Arithmetic for Computers citation and following credit line is included:
Basic Arithmetic (adding and subtracting)
EGRE 426 Fall 09 Chapter Three
IT253: Computer Organization
CS/COE0447 Computer Organization & Assembly Language
Computing Systems Basic arithmetic for computers.
07/19/2005 Arithmetic / Logic Unit – ALU Design Presentation F CSE : Introduction to Computer Architecture Slides by Gojko Babić.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
CSE 241 Computer Organization Lecture # 9 Ch. 4 Computer Arithmetic Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering.
1 EGRE 426 Fall 08 Chapter Three. 2 Arithmetic What's up ahead: –Implementing the Architecture 32 operation result a b ALU.
1  1998 Morgan Kaufmann Publishers Arithmetic Where we've been: –Performance (seconds, cycles, instructions) –Abstractions: Instruction Set Architecture.
CS Data representation 1 Ch. 2.1 Data Representation Unsigned and Signed Integers – representation, addition, subtraction.
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
1 Lecture 7: MARS, Computer Arithmetic Today’s topics:  MARS intro  Numerical representations  Addition and subtraction.
1 ELEN 033 Lecture 4 Chapter 4 of Text (COD2E) Chapters 3 and 4 of Goodman and Miller book.
Computer Architecture Lecture Notes Spring 2005 Dr. Michael P. Frank Competency Area 4: Computer Arithmetic.
Addition, Subtraction, Logic Operations and ALU Design
CHAPTER 3 Arithmetic For Computers 1/31/ Topics for discussion 1/31/ Number system: { radix/base, a set of distinct digits, operations} Radix.
순천향대학교 정보기술공학부 이 상 정 1 3. Arithmetic for Computers.
1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.
Lecture 2 Number Representation, Overflow and Logic Topics Adders Math Behind Excess-3 Overflow Unsigned, signed-magnitude Two’s Complement Gray Code Boolean.
Prof. Hsien-Hsin Sean Lee
Lec 11Systems Architecture1 Systems Architecture Lecture 11: Arithmetic for Computers Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some or all.
Computer Arthmetic Chapter Four P&H. Data Representation Why do we not encode numbers as strings of ASCII digits inside computers? What is overflow when.
MicroProcessors Lec. 4 Dr. Tamer Samy Gaafar. Course Web Page —
9/23/2004Comp 120 Fall September Chapter 4 – Arithmetic and its implementation Assignments 5,6 and 7 posted to the class web page.
EE204 L03-ALUHina Anwar Khan EE204 Computer Architecture Lecture 03- ALU.
May 2, 2001System Architecture I1 Systems Architecture I (CS ) Lecture 11: Arithmetic for Computers * Jeremy R. Johnson May 2, 2001 *This lecture.
By Wannarat Computer System Design Lecture 3 Wannarat Suntiamorntut.
1 CPTR 220 Computer Organization Computer Architecture Assembly Programming.
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
1 (Based on text: David A. Patterson & John L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, 3 rd Ed., Morgan Kaufmann,
Computer Arthmetic Chapter Four P&H.
Topic 3a Two’s Complement Representation
CS/COE0447 Computer Organization & Assembly Language
CS/COE0447 Computer Organization & Assembly Language
A 1-Bit Arithmetic Logic Unit
Chapter 3 Arithmetic for Computers
COMS 361 Computer Organization
Presentation transcript:

1 CS/COE0447 Computer Organization & Assembly Language Chapter 3

2 Topics Negative binary integers –Sign magnitude, 1’s complement, 2’s complement –Sign extension, ranges, arithmetic Signed versus unsigned operations Overflow (signed and unsigned) Branch instructions: branching backwards Implementation of addition Chapter 3 Part 2 will cover: –Implementations of multiplication, division –Floating point numbers Binary fractions IEEE 754 floating point standard Operations –underflow –Implementations of addition and multiplication (less detail than for integers) –Floating-point instructions in MIPS –Guard and Round bits

3 Computer Organization

4 Binary Arithmetic So far we have studied –Instruction set basics –Assembly & machine language We will now cover binary arithmetic algorithms and their implementations Binary arithmetic will provide the basis for the CPU’s “datapath” implementation

5 Binary Number Representation We looked at unsigned numbers before –B 31 B 30 …B 2 B 1 B 0 –B 31  B 30  …+B 2  2 2 +B 1  2 1 +B 0  2 0 We will deal with more complicated cases –Negative integers –Real numbers (a.k.a. floating-point numbers)

Unsigned Binary Numbers Limited number of binary numbers (patterns of 0s and 1s) –8-bit number: 256 patterns, to –General: 2 N bit patterns in N bits 16 bits: 2 16 = 65,536 bit patterns 32 bits: 2 32 = 4,294,967,296 bit patterns Unsigned numbers use the patters for 0 and positive numbers –8-bit number range corresponds to … –32-bit number range [ ] –In general, the range is [0…2 N -1] Addition and subtraction: as in decimal (on board) 6

Unsigned Binary Numbers in MIPS MIPS instruction set provides support –addu $1,$2,$3 - adds two unsigned numbers –Addiu $1,$2,33 – adds unsigned number with SIGNED immediate (see green card!) –Subu $1,$2,$3 –Etc In MIPS: the carry/borrow out is ignored –Overflow is possible, but hardware ignores it –Signed instructions throw exceptions on overflow (see footnote 1 on green card) Unsigned memory accesses: lbu, lhu –Loaded value is treated as unsigned –Convert from smaller bit width (8, 16) to 32 bits –Upper bits in the 32-bit destination register are set to 0s (see green card) 7

Important 7-bit Unsigned Numbers American Standard Code for Information Interchange (ASCII) –7 bits used for the characters –8 th bit may be used for error detection (parity) Unicode: A larger encoding; backward compatible with ASCII 8

Signed Numbers How shall we represent both positive and negative numbers? We still have a limited number of bits –N bits: 2 N bit patterns We will assign values to bit patterns differently –Some will be assigned to positive numbers and some to negative numbers 3 Ways: sign magnitude, 1’s complement, 2’s complement 9

10 Method 1: Sign Magnitude {sign bit, absolute value (magnitude)} –Sign bit “0” – positive number “1” – negative number –EX. (assume 4-bit representation) 0000: : : : : -0 Properties –two representations of zero –equal number of positive and negative numbers

11 Method 2: One’s Complement ((2 N -1) – number): To multiply a 1’s Complement number by -1, subtract the number from (2 N -1)_unsigned. Or, equivalently (and easily!), simply flip the bits 1CRepOf(A) + 1CRepOf(-A) = 2 N -1_unsigned (interesting tidbit) Let’s assume a 4-bit representation (to make it easy to work with) Examples: 0011: : : – 0110 or just flip the bits of : – 0000 or just flip the bits of : – 0111 or just flip the bits of 0111 Properties –Two representations of zero –Equal number of positive and negative numbers

12 Method 3: Two’s Complement (2 N – number): To multiply a 2’s Complement number by -1, subtract the number from 2 N _unsigned. Or, equivalently (and easily!), simply flip the bits and add 1. 2CRepOf(A) + 2CRepOf(-A) = 2 N _unsigned (interesting tidbit) Let’s assume a 4-bit representation (to make it easy to work with) Examples: 0011: : : – 0110 or just flip the bits of 0110 and add : – 0001 or just flip the bits of 0001 and add : – 0111 or just flip the bits of 0111 and add : – 1000 or just flip the bits of 1000 and add 1 Properties –One representation of zero: 0000 –An extra negative number: 1000 (this is -8, not -0)

13 Ranges of numbers Range (min to max) in N bits: –SM and 1C: -2 (N-1) -1 to +2 (N-1) -1 –2C: -2 (N-1) to +2 (N-1) -1

14 Sign Extension #s are often cast into vars with more capacity Sign extension (in 1c and 2c): extend the sign bit to the left, and everything works out la $t0,0x addi $t1,$t0, 7 addi $t2,$t0, -7 R[rt] = R[rs] + SignExtImm SignExtImm = {16{immediate[15]},immediate}

15 Summary Issues –# of zeros –Balance (and thus range) –Operations’ implementation CodeSign-Magnitude1’s Complement2’s Complement

16 2’s Complement Examples 32-bit signed numbers – = 0 – = +1 – = +2 –… – = +2,147,483,646 – = +2,147,483,647 – = - 2,147,483,648 -2^31 – = - 2,147,483,647 – = - 2,147,483,646 –… – = -3 – = -2 – = -1

17 Addition We can do binary addition just as we do decimal arithmetic –Examples in lecture Can be simpler with 2’s complement (1C as well) –We don’t need to worry about the signs of the operands! –Examples in lecture

18 Subtraction Notice that subtraction can be done using addition –A – B = A + (-B) –We know how to negate a number –The hardware used for addition can be used for subtraction with a negating unit at one input Add 1 Invert (“flip”) the bits

19 Signed versus Unsigned Operations “unsigned” operations view the operands as positive numbers, even if the most significant bit is 1 Example: 1100 is 12_unsigned but -4_2C Example: slt versus sltu –li $t0,-4 –li $t1,10 –slt $t3,$t0,$t1 $t3 = 1 –sltu $t4,$t0,$t1 $t4 = 0 !!

20 Signed Overflow Because we use a limited number of bits to represent a number, the result of an operation may not fit  “overflow” No overflow when –We add two numbers with different signs –We subtract a number with the same sign Overflow when –Adding two positive numbers yields a negative number –Adding two negative numbers yields a positive number –How about subtraction?

21 Overflow On an overflow, the CPU can –Generate an exception –Set a flag in a status register –Do nothing In MIPS on green card: –add, addi, sub: footnote (1) May cause overflow exception

22 Overflow with Unsigned Operations addu, addiu, subu –Footnote (1) is not listed for these instructions on the green card –This tells us that, In MIPS, nothing is done on unsigned overflow –How could it be detected for, e.g., add? Carry out of the most significant position (in some architectures, a condition code is set on unsigned overflow, which IS the carry out from the top position)

23 Branch Instructions: Branching Backwards # $t3 = ; $t4 = li $t0,0 li $t3,1 li $t4, 1 loop: addi $t3,$t3,2 addi $t4,$t4,3 addi $t0,$t0,1 slti $t5,$t0,4 bne $t5,$zero,loop machine code: 0x15a0fffb BranchAddr = {14{imm[15]}, imm, 2’b0}

24 1-bit Adder With a fully functional single- bit adder –We can build a wider adder by linking many one-bit adders 3 inputs –A: input A –B: input B –C in : input C (carry in) 2 outputs –S: sum –C out : carry out

25 Boolean Logic formulas S = A’B’C in +A’BC in ’+AB’C in ’+ABC in Cout = AB+BC in +AC in InputOutput ABC in SC out Can implement the adder using logic gates

26 Logic Gates Y=A&B Y=A|B Y=~(A&B) Y=~(A|B) 2-input AND 2-input OR 2-input NAND 2-input NOR A B A A A B B B Y Y Y Y

27 Implementation in logic gates Cout = AB+BC in +AC in We’ll see more boolean logic and circuit implementation when we finish with numbers AND GATES OR GATES

28 N-bit Adder An N-bit adder can be constructed with N one- bit adders –A carry generated in one stage is propagated to the next (“ripple carry adder”) 3 inputs –A: N-bit input A –B: N-bit input B –C in : input C (carry in) 2 outputs –S: N-bit sum –C out : carry out (0)

29 N-bit Ripple-Carry Adder (0)(1) (0) (1)1 0(0)1 …

30 Describing a single-bit adder –A truth table will tell us about the operation of a single-bit adder InputOutput ABC in SC out