Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Similar documents
Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

Introduction to 8086 Assembly

Introduction to Computers and Programming. Numeric Values

COMP2611: Computer Organization. Data Representation

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Organisasi Sistem Komputer

Divide: Paper & Pencil

Lecture 8: Addition, Multiplication & Division

Module 2: Computer Arithmetic

Computer Architecture and System Programming Laboratory. TA Session 3

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Integer Arithmetic Part2

Signed number Arithmetic. Negative number is represented as

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

Basic Pentium Instructions. October 18

Chapter 2 Data Representations

We will first study the basic instructions for doing multiplications and divisions

Number Systems. Both numbers are positive

Data Representations & Arithmetic Operations

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Arithmetic for Computers

Chapter 5 : Computer Arithmetic

Inf2C - Computer Systems Lecture 2 Data Representation

CHW 261: Logic Design

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

Instructions moving data

Defining and Using Simple Data Types

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

Number Systems and Computer Arithmetic

Floating-Point Arithmetic

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

EEM336 Microprocessors I. Arithmetic and Logic Instructions

Chapter 3: Arithmetic for Computers

Number representations

Chapter 4. Operations on Data

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

Numeric Encodings Prof. James L. Frankel Harvard University

Floating Point Arithmetic

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

World Inside a Computer is Binary

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

8088/8086 Programming Integer Instructions and Computations

Week /8086 Microprocessor Programming I

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Digital Fundamentals

Number Systems and Their Representations

MIPS Integer ALU Requirements

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

FLOATING POINT NUMBERS

8086 Programming. Multiplication Instructions. Multiplication can be performed on signed and unsigned numbers.

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

ECE232: Hardware Organization and Design

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Arithmetic Instructions

Chapter 10 - Computer Arithmetic

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

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Timing for Ripple Carry Adder

COMPUTER ARITHMETIC (Part 1)

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

Signed Multiplication Multiply the positives Negate result if signs of operand are different

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

Arithmetic Processing

M1 Computers and Data

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Chapter 4: Data Representations

xx.yyyy Lecture #11 Floating Point II Summary (single precision): Precision and Accuracy Fractional Powers of 2 Representation of Fractions

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

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

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

CO212 Lecture 10: Arithmetic & Logical Unit

Topic Notes: Bits and Bytes and Numbers

Thomas Polzer Institut für Technische Informatik

CS61C : Machine Structures

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

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

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

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

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

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

ECE260: Fundamentals of Computer Engineering

Chapter 2. Data Representation in Computer Systems

Topic Notes: Bits and Bytes and Numbers

CPS 104 Computer Organization and Programming

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

Representing numbers on the computer. Computer memory/processors consist of items that exist in one of two possible states (binary states).

Floating Point Numbers. Lecture 9 CAP

EC 333 Microprocessor and Interfacing Techniques (3+1)

Arithmetic Logic Unit

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Transcription:

Part 5 Adding Binary Integers Arithmetic Logic Unit = Adding Binary Integers Adding Base Numbers Computer's add binary numbers the same way that we do with decimal Columns are aligned, added, and "'s" are carried to the next column In computer processors, this component is called an adder 2 3 7 7 8 2 6 5 2 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 4 Adding Binary Example Adding 2's Complement 82 5 9-3 233 78 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 5 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 6

Extending Unsigned Integers Extending Bytes Converting from 8-bit to 6-bit and more Often in programs, data needs to moved to a integer with a larger number of bits For example, an 8-bit number is moved to a 6-bit representation /22/28 Sacramento State - Cook - CSc 35 - Fall 28 8 Extending Unsigned Integers Unsigned 3 Extended For unsigned numbers is fairly easy just add zeros to the left of the number This, naturally, is how our number system works anyway: 456 = 456 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 9 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 Extending Signed Integers 2's Complement Extended Incorrectly When the data is stored in a signed integer, the conversion is a little more complex Simply adding zeroes to the left, will convert a negative value to a positive one Each type of signed representation has its own set of rules -3 243 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 2 2

Sign Magnitude Extension Sign Magnitude Extended: 77 In signed magnitude, the most-significant bit (msb) stores the negative sign The new sign-bit needs to have this value Rules: copy the old sign-bit to the new sign-bit fill in the rest of the new bits with zeroes including the old sign bit /22/28 Sacramento State - Cook - CSc 35 - Fall 28 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 4 Sign Magnitude Extended: -77 2's Complement Extension 2's Complement is very simple to convert to a larger representation Remember that we inverted the bits and added to get a negative value Rule: copy the old most-significant bit to all the new bits /22/28 Sacramento State - Cook - CSc 35 - Fall 28 5 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 6 2's Complement Extended: 77 2's Complement Extended: -77 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 7 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 8 3

Multiplying Binary Numbers = Multiplying Binary Numbers Many processors today provide complex mathematical instructions However, the processor only needs to know how to add Historically, multiplication was performed with successive additions /22/28 Sacramento State - Cook - CSc 35 - Fall 28 2 Multiplying Scenario Multiplying: The Bad Way Let's say we have two variables: A and B Both contain integers that we need to multiply Our processor can only add (and subtract using 2's complement) How do we multiply the values? One way of multiplying the values is to create a For Loop using one of the variables A or B Then, inside the loop, continuously add the other variable to a running total /22/28 Sacramento State - Cook - CSc 35 - Fall 28 2 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 22 Multiplying: The Bad Way Multiplying: The Bad Way total = ; for (i = ; i < A; i) { total = B; } If one of the operands A or B is large, then the computation could take a long time This is incredibly inefficient Also, given that A and B could contain drastically different values the number of iterations would vary Required time is not constant /22/28 Sacramento State - Cook - CSc 35 - Fall 28 23 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 24 4

Multiplying: The Best Way Unsigned Integer: 3 Computers can perform multiplication using long multiplication just like you do The number of additions is then fixed to 8, 6, 32, 64 depending on the size of the integer The following example multiplies 2 unsigned 4-bit numbers /22/28 Sacramento State - Cook - CSc 35 - Fall 28 25 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 26 Unsigned Integer: 3 Unsigned Integer: 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 27 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 28 Unsigned Integer: 3 Unsigned Integer: 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 29 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 3 5

6 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 3 Unsigned Integer: 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 32 Unsigned Integer: 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 33 Unsigned Integer: 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 34 Unsigned Integer: 3 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 35 Unsigned Integer: 3 3 When two numbers are multiplied, the product will have twice the number of digits Examples: 8-bit 8-bit 6-bit 6-bit 6-bit 32-bit Often processors will store the result in the original bit-size and flag an overflow if it does not fit /22/28 Sacramento State - Cook - CSc 35 - Fall 28 36 Multiplication Doubles the Bit-Count

Add & Subtract x86 Mathematics Complex Math is Complex The Add and Subtract instructions take two operands and store the result in the second operand This is the same as the = and -= operators used in Visual Basic.NET, C, C, Java, etc /22/28 Sacramento State - Cook - CSc 35 - Fall 28 38 Addition Subtraction Immediate, Register, Memory Immediate, Register, Memory ADD value, target SUB value, target Register, Memory Register, Memory /22/28 Sacramento State - Cook - CSc 35 - Fall 28 39 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 4 Negate (2's complement) Example: Simple Add Move value into RAX NEG register MOV $7, %rax ADD $2, %rax RAX = 2 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 4 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 42 7

Multiplication & Division Multiplication Review Multiplication and division are far more complex The x86 treats this type of math differently than add/subtract It requires specific registers to be used: RAX, RDX Remember: when two n bit numbers are multiplied, result will be 2n bits So two 8-bit numbers 6-bit two 6-bit numbers 32-bit two 32-bit numbers 64-bit two 64-bit numbers 28-bit /22/28 Sacramento State - Cook - CSc 35 - Fall 28 43 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 44 Multiplication on the x86 x86 Multiplication First operand Instruction inputs are strange first operand is must be stored in RAX second operand must be a register (can't be a immediate) Result is stored into two registers rax will contain the lower 8 bytes rdx will contain the upper 8 bytes RAX MUL register RDX RAX Upper 8 bytes Lower 8 bytes /22/28 Sacramento State - Cook - CSc 35 - Fall 28 45 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 46 Multiply - Unsigned Multiply - Signed MUL operand IMUL operand Register, Memory Register, Memory /22/28 Sacramento State - Cook - CSc 35 - Fall 28 47 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 48 8

Unsigned Multiply: 846 by 42 Multiplication Tips MOV $846, %rax MOV $42, %rbx MUL %rbx #First operand #Need register for MUL #RAX gets low 8 bytes #RDX gets high 8 bytes Even though you are just using RAX as input, both RAX and RDX will change Be aware that you might lose important data, and backup to memory if needed /22/28 Sacramento State - Cook - CSc 35 - Fall 28 49 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 5 Additional x86 Multiply Instructions IMUL (few more combos) x86 also contains versions of the IMUL instruction that take multiple operands Allows "short" multiplication just stored in register Please note: these do not exist for MUL IMUL immediate, reg IMUL memory, reg IMUL reg, reg /22/28 Sacramento State - Cook - CSc 35 - Fall 28 5 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 52 Signed Multiply: 846 by 42 Division on the x86 MOV $846, %rax IMUL $42, %rax Division on the x86 is very interesting Like multiplication, it uses 2 registers The dividend (number being divided) uses two registers RAX contains the lower 8 bytes RDX contains the upper 8 bytes /22/28 Sacramento State - Cook - CSc 35 - Fall 28 53 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 54 9

Division on the x86 x86 Division Upper 8 bytes Lower 8 bytes These two registers are used for the result The output contains: RAX will contain the quotient RDX will contain the remainder RDX DIV register RDX Remainder RAX RAX Quotient /22/28 Sacramento State - Cook - CSc 35 - Fall 28 55 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 56 Divide - Unsigned Divide - Signed DIV denominator IDIV denominator Register, Memory Register, Memory /22/28 Sacramento State - Cook - CSc 35 - Fall 28 57 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 58 Dividing Rules Division Tips Sign Magnitude create a new sign that is a XOR of the old ones clear the old sign bits and expand with zeros 2's complement numerator must be expanded to the destination size (twice the original) this must be done beforehand otherwise the result will be incorrect Even you don't store anything into RDX, you must clear it For signed-division RDX needs to contain the signextended value of RAX this is covered in the next section /22/28 Sacramento State - Cook - CSc 35 - Fall 28 59 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 6

CWD (6 bit): Extend AX DX CDQ (32 bit): Extend EAX EDX CWD CDQ Convert Word Double Convert Double Quad /22/28 Sacramento State - Cook - CSc 35 - Fall 28 6 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 62 CQO (64 bit): Extend RAX RDX Divide 6-bit: -846 by 42 CQO Convert Quad Oct MOV $-846, %ax #AX is the dividend MOV $42, %bx #Divisor CWD #Extend the sign to DX IDIV %bx #AX gets quotient #DX gets remainder /22/28 Sacramento State - Cook - CSc 35 - Fall 28 63 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 64 Divide 64-bit: -846 by 42 MOV $-846, %rax #RAX is the dividend MOV $42, %rbx #Divisor CQO #Extend the sign to RDX IDIV %rbx #RAX gets quotient #RDX gets remainder Numbers in Programs The primitive types are pretty primitive /22/28 Sacramento State - Cook - CSc 35 - Fall 28 65

Primitive Data Types Integer Data Types Most popular program languages hide the true nature of the computer from you However, most of the language's primitive data types are the same types recognized by the processor Integer data types are stored in simple binary numbers The number of bytes used varies:, 2, 4, etc. Languages often have a unique name for each short, int, long, etc /22/28 Sacramento State - Cook - CSc 35 - Fall 28 67 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 68 Floating-Point Data Type Floating-Point Data Type Floating-point numbers are usually stored using the IEEE 754 standard Languages often have unique names for them such as float, double, real This is not always the case some languages implement their own structures e.g. COBOL Why? some processors do not have floating-point instructions or the language needs more precision and control /22/28 Sacramento State - Cook - CSc 35 - Fall 28 69 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 7 Floating Point Numbers Floating Point Numbers Real numbers are real complex Often, programs need to perform mathematics on real numbers Floating point numbers are used to represent quantities that cannot be represented by integers /22/28 Sacramento State - Cook - CSc 35 - Fall 28 72 2

Floating Point Numbers IEEE 754 Why? regular binary numbers can only store whole positive and negative values many numbers outside the range representable within the system's bit width (too large/small) Practically modern computers use the IEEE 754 Standard to store floating-point numbers Represent by a mantissa and an exponent similar to scientific notation the value of a number is: mantissa 2 exponent uses signed magnitude /22/28 Sacramento State - Cook - CSc 35 - Fall 28 73 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 74 IEEE 754 IEEE 754 Single Precision (32 bit) Comes in three forms: single-precision: 32-bit double-precision: 64-bit quad-precision: 28-bit Also supports special values: negative and positive infinity and "not a number" for errors (e.g. /) /22/28 Sacramento State - Cook - CSc 35 - Fall 28 75 /22/28 Sacramento State - Cook - CSc 35 - Fall 28 76 Interpretation: Invalid Numbers NaN / Naan /22/28 Sacramento State - Cook - CSc 35 - Fall 28 77 3