Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

Similar documents
SECURE PROGRAMMING A.A. 2018/2019

Introduction to Computers and Programming. Numeric Values

Advanced Computer Architecture-CS501

Lecture 8: Addition, Multiplication & Division

Integer Representation

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

CYSE 411/AIT681 Secure Software Engineering Topic #10. Secure Coding: Integer Security

Chapter Two MIPS Arithmetic

2/9/18. Readings. CYSE 411/AIT681 Secure Software Engineering. Introductory Example. Secure Coding. Vulnerability. Introductory Example.

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

Chapter 4. Operations on Data

COMP2611: Computer Organization. Data Representation

COMP 122/L Lecture 2. Kyle Dewey

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Programming in C++ 5. Integral data types

Arithmetic Operations

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

More Programming Constructs -- Introduction

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CS61B Lecture #14: Integers

Inf2C - Computer Systems Lecture 2 Data Representation

CS 261 Fall Mike Lam, Professor Integer Encodings

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

Integers II. CSE 351 Autumn 2018

Lecture 3 Tao Wang 1

World Inside a Computer is Binary

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Fixed-Point Math and Other Optimizations

SIGNED AND UNSIGNED SYSTEMS

CS 64 Week 1 Lecture 1. Kyle Dewey

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Operators in C. Staff Incharge: S.Sasirekha

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

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

COMPUTER ARITHMETIC (Part 1)

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

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

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,...

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

CO212 Lecture 10: Arithmetic & Logical Unit

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

Arithmetic type issues

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

A flow chart is a graphical or symbolic representation of a process.

Informatics Ingeniería en Electrónica y Automática Industrial

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Microcomputers. Outline. Number Systems and Digital Logic Review

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

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

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

CS313D: ADVANCED PROGRAMMING LANGUAGE

Number Systems. Both numbers are positive

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

Computer System and programming in C

SECTION II: LANGUAGE BASICS

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

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

Experiment Objectives. 2. Preparation. 3. Tasks. 3.1 Task A: String to Integer Conversion

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

(+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)

EE292: Fundamentals of ECE

CPS 104 Computer Organization and Programming


CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Binary Representations and Arithmetic

Chapter 10 - Computer Arithmetic

The Design of C: A Rational Reconstruction

Work relative to other classes

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

The Design of C: A Rational Reconstruction"

Computer Programming CS F111

The Design of C: A Rational Reconstruction

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

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

Computer Systems C S Cynthia Lee

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Data III & Integers I

Midterm Sample Questions. a) What evaluates to FALSE in C? What evaluates to TRUE?

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Module 2: Computer Arithmetic

Synopsys Static Analysis Support for SEI CERT C Coding Standard

CHAPTER 5: Representing Numerical Data

Chapter 4 Arithmetic Functions

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

Number Systems (2.1.1)

CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS

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

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

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Signed Binary Numbers

CIS 4360 Secure Computer Systems. Integers

Transcription:

Lecture 12 Integers Computer and Network Security 19th of December 2016 Computer Science and Engineering Department CSE Dep, ACS, UPB Lecture 12, Integers 1/40

Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 2/40

Integers numbers non-floating point positive, negative or zero CSE Dep, ACS, UPB Lecture 12, Integers 3/40

Integer Use actual numbers and integers values and... array index length/size of an object pointer arithmetic loop counter, array bound argument to a memory allocation function CSE Dep, ACS, UPB Lecture 12, Integers 4/40

Data Types char short int long long long pointer CSE Dep, ACS, UPB Lecture 12, Integers 5/40

Size of Data Types. Data Models CERT C Programming Language Secure Coding Standard, pg. 118 CSE Dep, ACS, UPB Lecture 12, Integers 6/40

To Bear in Mind representation of data types bounds (minimum, maximum) signedness conversion pointers and integers CSE Dep, ACS, UPB Lecture 12, Integers 7/40

Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 8/40

Positive Integers always the same first bit is 0 for signed integers and the rest is the value the actual value is stored for unsigned integers representation differs for (signed) negative integers CSE Dep, ACS, UPB Lecture 12, Integers 9/40

Signed-magnitude for Negative Integers direct representation first bit is 1 the rest of the bits are the actual value issue: difficult hardware implementation CSE Dep, ACS, UPB Lecture 12, Integers 10/40

1 s Complement for Negative Integers first bit is 1 the rest of the bits are complemented (reversed) the circuitry is simpler issue: two values for zero (positive and negative zero) CSE Dep, ACS, UPB Lecture 12, Integers 11/40

2 s Complement for Negative Integers first bit is 1 the rest of the bits are complemented and added one a single (positive) representation for zero CSE Dep, ACS, UPB Lecture 12, Integers 12/40

Signed/Unsigned Representation and Ranges we use 2 s complement consider N bits width representation 2 N possible values for unsigned integers range is (0... 2 N 1) for signed integers, range is ( 2 N 1... 2 N 1 1) CSE Dep, ACS, UPB Lecture 12, Integers 13/40

Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 14/40

Promotions integers smaller than int are promoted to int when performing operations done to avoid overflows char c1, c2; c1 = c1 + c2; CSE Dep, ACS, UPB Lecture 12, Integers 15/40

Types and Ranks type: signed or unsigned ranks: long long > long > int > short > char convert to the larger rank CSE Dep, ACS, UPB Lecture 12, Integers 16/40

Unsigned to Unsigned safe from shorter range to larger range truncation if converting from larger range to shorter range CSE Dep, ACS, UPB Lecture 12, Integers 17/40

Unsigned to Signed same rank high-order bit is signed bit sign may change CSE Dep, ACS, UPB Lecture 12, Integers 18/40

Signed to Signed sign-extension is convert to larger rank preserve lower part in case of conversion to a smaller rank CSE Dep, ACS, UPB Lecture 12, Integers 19/40

Signed to Unsigned value unchanged if signed integer is positive truncation if unsigned rank is smaller sign-extension if larger unsigned rank in case of negative integers, a large positive integers is obtained CSE Dep, ACS, UPB Lecture 12, Integers 20/40

Signed/Unsigned Characters char may be signed char on unsigned char when storing negative signed char in an integer, the result is a large negative number if value above 127 (0x7F) use unsigned char CSE Dep, ACS, UPB Lecture 12, Integers 21/40

In a Nutshell CSE Dep, ACS, UPB Lecture 12, Integers 22/40

Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 23/40

Addition integer and integer pointer and integer may result in overflow arithmetic conversion CSE Dep, ACS, UPB Lecture 12, Integers 24/40

Subtraction integer and integer pointer and integer may result in negative overflow CSE Dep, ACS, UPB Lecture 12, Integers 25/40

Multiplication prone to overflow even for small operands good idea to allocate twice the storage for the product CSE Dep, ACS, UPB Lecture 12, Integers 26/40

Division division by zero division of minimum signed integer value divided by -1 CSE Dep, ACS, UPB Lecture 12, Integers 27/40

Modulo similar to division, since division is involved careful at negative remainders CSE Dep, ACS, UPB Lecture 12, Integers 28/40

Shifting careful when shifting signed negative integers integer promotion must be taken into consideration CSE Dep, ACS, UPB Lecture 12, Integers 29/40

Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 30/40

Truncation convert to a smaller integer type the initial value is outside range high order bits are lost in case of converting from larger to shorter CSE Dep, ACS, UPB Lecture 12, Integers 31/40

Overflow may be signed on usigned go beyond maximum value or below minimum value CSE Dep, ACS, UPB Lecture 12, Integers 32/40

Sign Error convert unsigned to signed if bit sign in integer is set it results in a negative number convert signed to unsigned negative numbers result in large positive numbers CSE Dep, ACS, UPB Lecture 12, Integers 33/40

Negative Indices negative arrays index value outside bounds CSE Dep, ACS, UPB Lecture 12, Integers 34/40

Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 35/40

Recommendations INT00-C to INT18-C on 04. Integers (INT) in CERT C Secure Coding Standard CSE Dep, ACS, UPB Lecture 12, Integers 36/40

Rules INT30-C to INT36-C on 04. Integers (INT) in CERT C Secure Coding Standard CSE Dep, ACS, UPB Lecture 12, Integers 37/40

Outline Data Types Representation Conversions Operations Issues Rules and Recommendations Conclusion CSE Dep, ACS, UPB Lecture 12, Integers 38/40

Keywords integer type long long, long, int, short, char signed, unsigned bounds/range 2 s complement conversion promotion rank addition subtraction multiplication division modulo truncation overflow sign error negative index CSE Dep, ACS, UPB Lecture 12, Integers 39/40

References CERT C Secure Coding Standard 04. Integers (INT) https://www.securecoding.cert.org/confluence/ pages/viewpage.action?pageid=270 Secure Coding in C and C++ Class Module 3. Integers Secure Coding in C and C++ Chapter 5. Integer Security CSE Dep, ACS, UPB Lecture 12, Integers 40/40