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

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

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

Lecture 8: Addition, Multiplication & Division

Introduction to 8086 Assembly

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Signed number Arithmetic. Negative number is represented as

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

Computer Architecture and System Programming Laboratory. TA Session 3

Module 2: Computer Arithmetic

Integer Arithmetic Part2

Arithmetic for Computers

8088/8086 Programming Integer Instructions and Computations

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

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

Organisasi Sistem Komputer

EEM336 Microprocessors I. Arithmetic and Logic Instructions

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

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Defining and Using Simple Data Types

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Divide: Paper & Pencil

Instructions moving data

Arithmetic Instructions

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

Intel 8086: Instruction Set

COMPUTER ARITHMETIC (Part 1)

Number representations

Integer Multiplication and Division

Number Systems and Their Representations

CPS 104 Computer Organization and Programming

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

Number Systems and Computer Arithmetic

Basic Pentium Instructions. October 18

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

Introduction to Computers and Programming. Numeric Values

361 div.1. Computer Architecture EECS 361 Lecture 7: ALU Design : Division

World Inside a Computer is Binary

Arithmetic Processing

Week /8086 Microprocessor Programming I

COMP 303 Computer Architecture Lecture 6

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

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

Integer Arithmetic. Shift and rotate. Overview. Shift and Rotate Instructions

Computer Arithmetic Ch 8

Computer Arithmetic Ch 8

Chapter 4. Operations on Data

carry in carry 1101 carry carry

Chapter 7 Integer Arithmetic

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

CS & IT Conversions. Magnitude 10,000 1,

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

Chapter 5 : Computer Arithmetic

Chapter 10 - Computer Arithmetic

Integer Arithmetic. Pu-Jen Cheng. Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed.

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

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

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

Timing for Ripple Carry Adder

ENE 334 Microprocessors

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

Arithmetic and Bitwise Operations on Binary Data

Floating-Point Arithmetic

IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

EC 333 Microprocessor and Interfacing Techniques (3+1)

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

CHW 261: Logic Design

Computer Organization

Homework 3. Assigned on 02/15 Due time: midnight on 02/21 (1 WEEK only!) B.2 B.11 B.14 (hint: use multiplexors) CSCI 402: Computer Architectures

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

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

Chapter 3: part 3 Binary Subtraction

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

UNIT 2 PROCESSORS ORGANIZATION CONT.

COMP 122/L Lecture 2. Kyle Dewey

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

Number System. Introduction. Decimal Numbers

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

ELEG3923 Microprocessor Ch.6 Arithmetic and Logics

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

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

Iterative Division Techniques COMPUTER ARITHMETIC: Lecture Notes # 6. University of Illinois at Chicago

Numbering systems. Dr Abu Arqoub

CS 64 Week 1 Lecture 1. Kyle Dewey

3.1 DATA MOVEMENT INSTRUCTIONS 45

Chapter 10 Binary Arithmetics

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

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Manipulating Integers

9 Multiplication and Division

MIPS Integer ALU Requirements

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

University of Illinois at Chicago. Lecture Notes # 13

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

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

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Computer Architecture Prof. Smruti Ranjan Sarangi Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Fast Arithmetic. Philipp Koehn. 19 October 2016

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS

Transcription:

Part 4 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 4 Adding Binary Example 82 5 Negative Binary Integers 233 Have a positive attitude about negatives 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 5

Negative Binary Numbers Negative Binary Numbers When we write a negative number, we generally use a "-" as a prefix character However, binary numbers can only store ones and zeros So, how we store a negative a number? When a number can represent both positive and negative numbers, it is called a signed integer Otherwise, it is unsigned 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 7 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 8 Signed Magnitude Signed Magnitude One approach is to use the most significant bit (msb) to represent the negative sign If positive, this bit will be a zero If negative, this bit will be a This gives a byte a range of -27 to 27 rather than to 255 Value most significant bit 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 9 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 Signed Magnitude: 3 and -3 Signed Magnitude Drawback # Positive Negative When two numbers are added, the system needs to check and sign bits and act accordingly For example: if both numbers are positive, add values if one is negative subtract it from the other etc There are also rules for subtracting 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 2 2

Signed Magnitude Drawback #2 Oh noes! Two zeros? Also, signed magnitude also can store a positive and negative version of zero Yes, there are two zeroes! Imagine having to write Java code like - if (x == x == -) 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 4 's Complement Advantages / Disadvantages Rather than use a sign bit, the value can be made negative by inverting each bit each becomes a each becomes a Result is a "complement" of the original This is logically the same as subtracting the number from Advantages over signed magnitude very simple rules for adding/subtracting numbers are simply added: 5-3 is the same as 5-3 Disadvantages positive and negative zeros still exist so, it's not a perfect solution 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 5 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 6 's Complement: 3 and -3 's Complement Has Two Zeros Positive Negative - 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 7 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 8 3

2's Complement 2's Complement Advantages Practically all computers nowadays use 2's Complement Similar to 's complement, but after the number is inverted, is added to the result Logically the same as: subtracting the number from 2 n where n is the total number of bits in the integer Since negatives are subtracted from 2 n they can simply be added the extra carry (if it exists) is discarded this simplifies the hardware considerably since the processor only has to add The for negative numbers makes it so there is only one zero values range from -28 to 27 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 9 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 2 2's Complement: 3 and -3 Just One Zero! Positive Negative Add - 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 2 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 22 Adding 2's Complement 9-3 Extending Bytes 78 Converting from 8-bit to 6-bit and more 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 23 4

Extending Unsigned Integers Extending Unsigned Integers 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 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 25 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 26 Unsigned 3 Extended Extending Signed Integers 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 27 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 28 2's Complement Extended Incorrectly Sign Magnitude Extension -3 243 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 29 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 3 5

Sign Magnitude Extended: 77 Sign Magnitude Extended: -77 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 32 2's Complement Extension 2's Complement Extended: 77 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 33 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 34 2's Complement Extended: -77 Multiplying Binary Numbers = 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 35 6

Multiplying Binary Numbers Multiplying Scenario Many processors today provide complex mathematical instructions However, the processor only needs to know how to add Historically, multiplication was performed with successive additions 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? 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 37 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 38 Multiplying: The Bad Way Multiplying: The Bad Way 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 total = ; for (i = ; i < A; i) { total = B; } 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 39 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 4 Multiplying: The Bad Way Multiplying: The Best Way 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 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 4 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 42 7

8 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 43 Unsigned Integer: 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 44 Unsigned Integer: 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 45 Unsigned Integer: 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 46 Unsigned Integer: 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 47 Unsigned Integer: 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 48 Unsigned Integer: 3

Unsigned Integer: 3 Unsigned Integer: 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 49 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 5 Unsigned Integer: 3 Unsigned Integer: 3 3 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 5 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 52 Multiplication Doubles the Bit-Count 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 x86 Mathematics Complex Math is Complex 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 53 9

Add & Subtract Addition 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 Immediate, Register, Memory ADD value, target Register, Memory 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 55 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 56 Subtraction Negate (2's complement) Immediate, Register, Memory SUB value, target NEG register Register, Memory 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 57 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 58 Example: Simple Add Multiplication & Division MOV $7, %rax ADD $2, %rax Move value into RAX RAX = 2 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 59 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 6

Multiplication Review Multiplication on the x86 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 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 6 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 62 x86 Multiplication Multiply - Unsigned First operand RAX MUL register MUL operand RDX Upper 8 bytes RAX Lower 8 bytes Register, Memory 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 63 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 64 Multiply - Signed Unsigned Multiply: 846 by 42 IMUL operand MOV $846, %rax MOV $42, %rbx MUL %rbx #First operand #Need register for MUL #RAX gets low 8 bytes #RDX gets high 8 bytes Register, Memory 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 65 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 66

Multiplication Tips Additional x86 Multiply Instructions 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 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 67 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 68 IMUL (few more combos) Signed Multiply: 846 by 42 IMUL immediate, reg IMUL memory, reg IMUL reg, reg MOV $846, %rax IMUL $42, %rax 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 69 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 7 Division on the x86 Division on the x86 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 These two registers are used for the result The output contains: RAX will contain the quotient RDX will contain the remainder 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 7 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 72 2

x86 Division Divide - Unsigned Upper 8 bytes RDX Lower 8 bytes RAX DIV register DIV denominator RDX Remainder RAX Quotient Register, Memory 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 73 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 74 Divide - Signed Dividing Rules IDIV denominator Register, Memory 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 the numerator must be expanded to the destination size (twice the original) this must be done beforehand otherwise the result will be incorrect 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 75 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 76 Division Tips CWD Instruction: 6-Bits: AX:DX 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 CWD Convert Word Double 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 77 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 78 3

CDQ Instruction: 32-Bits: EAX:EDX CDO Instruction: 64-Bits: RAX:RDX CDQ CQO Convert Double Quad Convert Quad Oct 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 79 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 8 Divide 6-bit: -846 by 42 Divide 64-bit: -846 by 42 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 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 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 8 2/2/28 Sacramento State - Cook - CSc 35 - Spring 28 82 4