Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Similar documents
The x86 Architecture

The Critical-Path Algorithm

Minimal Spanning Trees

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Scope and Parameter Passing

Scheduling and Digraphs

The Pairwise-Comparison Method

Sampling Distribution Examples Sections 15.4, 15.5

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Recognition of Tokens

The x86 Instruction Set

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Solving Recursive Sequences by Iteration

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Fundamental Data Types

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

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Recursive Descent Parsers

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

The Decreasing-Time Algorithm

The Traveling Salesman Problem Brute Force Method

Scope and Parameter Passing

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Density Curves Sections

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

LR Parsing - Conflicts

Rotations and Translations

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

The Class Construct Part 1

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Programming Languages

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Magnification and Minification

Friends and Unary Operators

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

MySQL Creating a Database Lecture 3

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Chapter 3: part 3 Binary Subtraction

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Introduction to Databases

The Plurality-with-Elimination Method

Ambient and Diffuse Light

The Coefficient of Determination

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

Stacks and their Applications

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

The Graphics Pipeline

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

Advanced Computer Architecture-CS501

The Graphics Pipeline

Introduction to Databases

Nondeterministic Programming in C++

Binary Tree Applications

Function Definition Syntax Tree

Programming Languages

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

DTDs and XML Attributes

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Basic Pentium Instructions. October 18

ECE/Comp Sci 352 Digital Systems Fundamentals. Charles R. Kime Section 2 Fall Logic and Computer Design Fundamentals

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Street-Routing Problems

CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS

Implementing Linked Lists

The x87 Floating-Point Unit

Building the Abstract Syntax Trees

Introduction to Compiler Design

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Arithmetic Operations on Binary Numbers

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Recursive Linked Lists

Computer Architecture and Organization: L04: Micro-operations

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Basic PHP Lecture 17

Code segment Stack segment

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Lecture 9. INC and DEC. INC/DEC Examples ADD. ADD Examples SUB. INC adds one to a single operand DEC decrements one from a single operand

XQuery FLOWR Expressions Lecture 35

Relational Databases Lecture 2

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

COMP 122/L Lecture 2. Kyle Dewey

Inheritance: The Fundamental Functions

Ex : Write an ALP to evaluate x(y + z) where x = 10H, y = 20H and z = 30H and store the result in a memory location 54000H.

Displaying Distributions - Quantitative Variables

Chapter 4 Arithmetic Functions

The Class Construct Part 2

EE 109 Unit 6 Binary Arithmetic

Relational Databases

Insertions, Deletions, and Updates

Chapter 4 Arithmetic

List Iterator Implementation

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Transcription:

Integer Overflow Lecture 8 Section 2.5 Robb T. Koether Hampden-Sydney College Mon, Jan 27, 2014 Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 1 / 32

1 Signed Addition and Subtraction 2 Signed Overflow Signed Overflow of Addition Signed Overflow of Subtraction 3 Unsigned Addition and Subtraction 4 Assignment Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 2 / 32

Outline 1 Signed Addition and Subtraction 2 Signed Overflow Signed Overflow of Addition Signed Overflow of Subtraction 3 Unsigned Addition and Subtraction 4 Assignment Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 3 / 32

Addition of Signed Integers To add signed integers, Express any negative values in two s complement form. Add them, using the ordinary rules of addition. To catch overflow, check two bits: The carry-in bit of the last column. The carry-out bit of the last column. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 4 / 32

Subtraction of Signed Integers To subtract signed integers, Express any negative values in two s complement form. Replace the subtrahend with its two s complement. Add them, using the ordinary rules of addition. To catch overflow, check two bits: The carry-in bit of the last column. The carry-out bit of the last column. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 5 / 32

Outline 1 Signed Addition and Subtraction 2 Signed Overflow Signed Overflow of Addition Signed Overflow of Subtraction 3 Unsigned Addition and Subtraction 4 Assignment Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 6 / 32

Outline 1 Signed Addition and Subtraction 2 Signed Overflow Signed Overflow of Addition Signed Overflow of Subtraction 3 Unsigned Addition and Subtraction 4 Assignment Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 7 / 32

Signed Overflow If the correct result is too large to fit in the allotted space, the condition is called overflow. Integer overflow under addition occurs when The sum of two positive integers is too large. The sum of two negative integers is too large. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 8 / 32

Detecting Signed Overflow a b a + b c-in c-out Valid? P P P 0 0 Yes P P N 1 0 No P N P 1 1 Yes P N N 0 0 Yes N P P 1 1 Yes N P N 0 0 Yes N N P 0 1 No N N N 1 1 Yes What characterizes overflow? Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 9 / 32

Detecting Signed Overflow a b a + b c-in c-out Valid? P P P 0 0 Yes P P N 1 0 No P N P 1 1 Yes P N N 0 0 Yes N P P 1 1 Yes N P N 0 0 Yes N N P 0 1 No N N N 1 1 Yes What characterizes overflow? Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 10 / 32

Detecting Signed Overflow Integer overflow under addition of signed integers is detected when the carry-in bit does not match the carry-out bit in the high-order position. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 11 / 32

Detecting Signed Overflow Using 8-bit signed integers, when we add 80 + 90 we get 86. 127 + 1 = 01111111 + 00000001 = 10000000 = 128. (127 + 1) 256 = 128. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 12 / 32

Detecting Signed Overflow Using 8-bit signed integers, when we add ( 80) + ( 90) we get 86. ( 128) + ( 1) = 10000000 + 11111111 = 01111111 = 127. (( 128) + ( 1)) + 256 = 127. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 13 / 32

The x86 Processor The x86 processor has a 32-bit EFLAGS register. Among the 32 bits are four flags: Carry Flag (CF) Set if an arithmetic operation generates a carry or a borrow out of the most-significant bit of the result; cleared otherwise. This flag indicates an overflow condition for unsigned-integer arithmetic. Sign Flag (SF) Set equal to the most-significant bit of the result, which is the sign bit of a signed integer (0 indicates a positive value and 1 indicates a negative value). Overflag Flag (OF) Set if the integer result is too large a positive number or too small a negative number (excluding the sign bit) to fit in the destination operand; cleared otherwise. This flag indicates an overflow condition for signed-integer (two s complement) arithmetic. Zero Flag (ZF) Set if the result is zero; cleared otherwise. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 14 / 32

Signed Addition a b a + b CF SF OF ZF 01000000 64 00100000 32 01100000 96 0 0 0 0 01000000 64 01100000 96 10100000-96 0 1 1 0 01000000 64 11100000-32 00100000 32 1 0 0 0 00100000 32 11000000-64 11100000-32 0 1 0 0 11100000-32 01000000 64 00100000 32 1 0 0 0 11000000-64 00100000 32 11100000-32 0 1 0 0 11000000-64 10100000-96 01100000 96 1 0 1 0 11000000-64 11100000-32 10100000-96 1 1 0 0 Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 15 / 32

Signed Addition a b a + b CF SF OF ZF 01000000 64 00100000 32 01100000 96 0 0 0 0 01000000 64 01100000 96 10100000-96 0 1 1 0 01000000 64 11100000-32 00100000 32 1 0 0 0 00100000 32 11000000-64 11100000-32 0 1 0 0 11100000-32 01000000 64 00100000 32 1 0 0 0 11000000-64 00100000 32 11100000-32 0 1 0 0 11000000-64 10100000-96 01100000 96 1 0 1 0 11000000-64 11100000-32 10100000-96 1 1 0 0 Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 16 / 32

The Relational and Equality Operators Mnemonic Condition Tested For Status Flags Setting O Overflow OF = 1 NO No Overflow OF = 0 B Below CF = 1 NB Not Below CF = 0 E Equal ZF = 1 NE Not Equal ZF = 0 S Sign SF = 1 NS No Sign SF = 0 BE Below or Equal (CF OR ZF) = 1 NBE Neither Below nor Equal (CF OR ZF) = 0 L Less (SF XOR OF) = 1 NL Not Less (SF XOR OF) = 0 LE Less or Equal (SF XOR OF) OR ZF = 1 NLE Neither Less nor Equal (SF XOR OF) OR ZF = 0 Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 17 / 32

Overflow/No Overflow a b a + b CF SF OF ZF 01000000 64 00100000 32 01100000 96 0 0 0 0 01000000 64 01100000 96 10100000-96 0 1 1 0 01000000 64 11100000-32 00100000 32 1 0 0 0 00100000 32 11000000-64 11100000-32 0 1 0 0 11100000-32 01000000 64 00100000 32 1 0 0 0 11000000-64 00100000 32 11100000-32 0 1 0 0 11000000-64 10100000-96 01100000 96 1 0 1 0 11000000-64 11100000-32 10100000-96 1 1 0 0 Overflow/No Overflow (OF = 1/0) Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 18 / 32

Sign/No Sign a b a + b CF SF OF ZF 01000000 64 00100000 32 01100000 96 0 0 0 0 01000000 64 01100000 96 10100000-96 0 1 1 0 01000000 64 11100000-32 00100000 32 1 0 0 0 00100000 32 11000000-64 11100000-32 0 1 0 0 11100000-32 01000000 64 00100000 32 1 0 0 0 11000000-64 00100000 32 11100000-32 0 1 0 0 11000000-64 10100000-96 01100000 96 1 0 1 0 11000000-64 11100000-32 10100000-96 1 1 0 0 Sign/No Sign (SF = 1/0) Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 19 / 32

Less/Not Less a b a + b CF SF OF ZF 01000000 64 00100000 32 01100000 96 0 0 0 0 01000000 64 01100000 96 10100000-96 0 1 1 0 01000000 64 11100000-32 00100000 32 1 0 0 0 00100000 32 11000000-64 11100000-32 0 1 0 0 11100000-32 01000000 64 00100000 32 1 0 0 0 11000000-64 00100000 32 11100000-32 0 1 0 0 11000000-64 10100000-96 01100000 96 1 0 1 0 11000000-64 11100000-32 10100000-96 1 1 0 0 Less/Not Less ((SF XOR OF) = 1/0) Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 20 / 32

title Explain why the combinations never occur. CF = 1, SF = 1, OF = 1 CF = 0, SF = 0, OF = 1 Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 21 / 32

Outline 1 Signed Addition and Subtraction 2 Signed Overflow Signed Overflow of Addition Signed Overflow of Subtraction 3 Unsigned Addition and Subtraction 4 Assignment Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 22 / 32

Signed Overflow of Subtraction Overflow occurs under signed subtraction when the carry-in bit does not match the carry-out bit in the high-order position. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 23 / 32

Signed Subtraction a b a b CF SF OF ZF 01000000 64 00100000 32 00100000 32 1 0 0 0 00100000 32 01000000 64 11100000-32 0 1 0 0 01000000 64 11100000-32 01100000 96 0 0 0 0 01000000 64 10100000-96 10100000-96 0 1 1 0 11000000-64 01100000 96 01100000 96 1 0 1 0 11000000-64 00100000 32 10100000-96 1 1 0 0 11100000-32 11000000-64 00100000 32 1 0 0 0 11000000-64 11100000-32 11100000-32 0 1 0 0 Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 24 / 32

Overflow/No Overflow a b a + b CF SF OF ZF 01000000 64 00100000 32 00100000 32 1 0 0 0 00100000 32 01000000 64 11100000-32 0 1 0 0 01000000 64 11100000-32 01100000 96 0 0 0 0 01000000 64 10100000-96 10100000-96 0 1 1 0 11000000-64 01100000 96 01100000 96 1 0 1 0 11000000-64 00100000 32 10100000-96 1 1 0 0 11100000-32 11000000-64 00100000 32 1 0 0 0 11000000-64 11100000-32 11100000-32 0 1 0 0 Overflow/No Overflow (OF = 1/0) Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 25 / 32

Less/Not Less a b a + b CF SF OF ZF 01000000 64 00100000 32 00100000 32 1 0 0 0 00100000 32 01000000 64 11100000-32 0 1 0 0 01000000 64 11100000-32 01100000 96 0 0 0 0 01000000 64 10100000-96 10100000-96 0 1 1 0 11000000-64 01100000 96 01100000 96 1 0 1 0 11000000-64 00100000 32 10100000-96 1 1 0 0 11100000-32 11000000-64 00100000 32 1 0 0 0 11000000-64 11100000-32 11100000-32 0 1 0 0 Less/Not Less ((SF XOR OF) = 1/0) Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 26 / 32

Outline 1 Signed Addition and Subtraction 2 Signed Overflow Signed Overflow of Addition Signed Overflow of Subtraction 3 Unsigned Addition and Subtraction 4 Assignment Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 27 / 32

Unsigned Addition Overflow occurs under unsigned subtraction when the carry-out bit in the high-order position is 1. Overflow occurs under unsigned subtraction when the carry-out bit in the high-order position is 0. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 28 / 32

Unigned Addition a b a + b CF SF OF ZF 01000000 64 00100000 32 01100000 96 1 0 0 0 01000000 64 01100000 96 10100000 160 0 1 0 0 01000000 64 10100000 160 11100000 224 0 0 0 0 01100000 96 11100000 224 01000000 64 0 1 1 0 10100000 160 11000000 192 01100000 96 1 0 1 0 11100000 224 11100000 224 11000000 192 1 1 0 0 Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 29 / 32

Outline 1 Signed Addition and Subtraction 2 Signed Overflow Signed Overflow of Addition Signed Overflow of Subtraction 3 Unsigned Addition and Subtraction 4 Assignment Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 30 / 32

Collected Collected Sec. 2.3: 11, 23, 40. Sec. 2.4: 15, 19. Sec. 2.5: 2, 18. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 31 / 32

Assignment Assignment Read Section 2.5. Perform signed addition and check for overflow. 10010011 01110010 +11011101 +01110101 Perform signed subtraction and check for overflow. 10010011 01110010 11011101 01110101 Repeat the above sets using unsigned addition and subtraction. Robb T. Koether (Hampden-Sydney College) Integer Overflow Mon, Jan 27, 2014 32 / 32