CS 270: Computer Organization. Integer Arithmetic Operations

Similar documents
Manipulating Integers

Systems Programming and Computer Architecture ( )

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

Bits, Bytes, and Integers Part 2

Integers. Today. Next time. ! Numeric Encodings! Programming Implications! Basic operations. ! Floats

Bits, Bytes, and Integers August 26, 2009

Representing and Manipulating Integers. Jo, Heeseung

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

Lecture 5-6: Bits, Bytes, and Integers

Systems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication

Integers Sep 3, 2002

Integers. Dr. Steve Goddard Giving credit where credit is due

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Bits, Bytes, and Integers

Arithmetic and Bitwise Operations on Binary Data

Topics of this Slideset. CS429: Computer Organization and Architecture. Encoding Integers: Unsigned. C Puzzles. Integers

CS429: Computer Organization and Architecture

Arithmetic and Bitwise Operations on Binary Data

Foundations of Computer Systems

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Computer Systems CEN591(502) Fall 2011

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

Integer Encoding and Manipulation

Bits, Bytes, and Integers

Page 1 CISC360. Encoding Integers Unsigned. Integers Sep 8, Numeric Ranges. Encoding Example (Cont.)

Computer Organization: A Programmer's Perspective

Integer Arithmetic. CS 347 Lecture 03. January 20, 1998

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Page # CISC360. Integers Sep 11, Encoding Integers Unsigned. Encoding Example (Cont.) Topics. Twoʼs Complement. Sign Bit

Bits, Bytes and Integers

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

CS 261 Fall Mike Lam, Professor Integer Encodings

Bits, Bytes and Integers Part 1

Integer Representation

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

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

Bits, Bytes, and Integer

Bits, Bytes, and Integers. Bits, Bytes, and Integers. The Decimal System and Bases. Everything is bits. Converting from Decimal to Binary

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

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

Chapter 4. Operations on Data

COMP 122/L Lecture 2. Kyle Dewey

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

C Puzzles The course that gives CMU its Zip! Integer Arithmetic Operations Jan. 25, Unsigned Addition. Visualizing Integer Addition

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

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

Integers II. CSE 351 Autumn 2018

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

Credits and Disclaimers

COMP Overview of Tutorial #2

Advanced Computer Architecture-CS501

CS61B Lecture #14: Integers

SIGNED AND UNSIGNED SYSTEMS

CS 64 Week 1 Lecture 1. Kyle Dewey

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

BITS, BYTES, AND INTEGERS

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Number Systems. Readings: , Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs

ISA 563 : Fundamentals of Systems Programming

C Puzzles! Taken from old exams. Integers Sep 3, Encoding Integers Unsigned. Encoding Example (Cont.) The course that gives CMU its Zip!

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Programming in C++ 5. Integral data types

EE 109 Unit 6 Binary Arithmetic

COMP2611: Computer Organization. Data Representation

Systems Programming and Computer Architecture ( )

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

Machine-Level Programming II: Control and Arithmetic

Floating Point January 24, 2008

Chapter Two MIPS Arithmetic

CHW 261: Logic Design

Lecture 8: Addition, Multiplication & Division

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

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

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

Arithmetic Processing

CS367 Test 1 Review Guide

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

September, Saeid Nooshabadi. Review: Floating Point Representation COMP Single Precision and Double Precision

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

UW CSE 351, Winter 2013 Midterm Exam

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Module 2: Computer Arithmetic

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

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Arithmetic for Computers

Representing Integers

COMP2121: Microprocessors and Interfacing. Number Systems

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

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

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

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

Transcription:

CS 270: Computer Organization Integer Arithmetic Operations Instructor: Professor Stephen P. Carl Unsigned Addition Operands: w bits True Sum: w+1 bits u + v u + v Discard Carry UAdd w (u, v) Standard addition ignores carry output; this is Modular Arithmetic s = UAdd w (u, v) = u + v mod 2 w UAdd w (u,v) = # u + v $ % u + v 2 w u + v < 2 w u + v 2 w 1

Visualizing Unsigned Addition Modulus wraps around at most once if True Sum 2 w True Sum 2 w+1 Overflow 2 w 0 Modular Sum Two s Complement Addition Operands: w bits True Sum: w+1 bits u + v u + v Discard Carry TAdd w (u, v) TAdd and UAdd have Identical Bit-Level Behavior Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v Will give s == t 2

TAdd Overflow Functionality True sum requires w+1 bits Drop off MSB Treat remaining bits as 2 s comp. integer 0 111 1 0 100 0 0 000 0 True Sum 2 w 1 PosOver 2 w 1 0 TAdd Result 011 1 000 0 1 011 1 2 w 1 1 100 0 1 000 0 2 w NegOver Multiplication Computing Exact Product of w-bit numbers x, y Product Ranges: Unsigned: 0 x * y (2 w 1) 2 = 2 2w 2 w+1 + 1 Up to 2w bits Two s complement min: x * y ( 2 w 1 )*(2 w 1 1) = 2 2w 2 + 2 w 1 Up to 2w 1 bits Two s complement max: x * y ( 2 w 1 ) 2 = 2 2w 2 Up to 2w bits, but only for (TMin w ) 2 Maintaining Exact Results Would need to keep expanding word size with each product computed Done in software by arbitrary precision arithmetic libraries 3

Unsigned Multiplication in C u Operands: w bits * v u v Discard w bits: UMult w (u, v) True Product: 2*w bits Standard multiplication function ignores uppermost w bits Again, this is Modular Arithmetic UMult w (u, v)= u v mod 2 w Another Code Security Example SUN XDR library Widely used library for transferring data between machines void* copy_elements(void *source[], int count, size_t size); source Need an array of size (count * size) 4

XDR Code void * copy_elements(void *source[], int count, size_t size) { /* * Allocate array for count objects, each of size bytes * and copy from locations designated by source */ void *result = malloc(count * size); if (result == NULL) /* allocation failed */ return NULL; void *next = result; int i; for (i = 0; i < count; i++) { /* Copy object i to destination */ memcpy(next, source[i], size); /* Update pointer to next memory region */ next += size; } return result; } XDR Vulnerability malloc(count * size) What if: count = 2 20 + 1 size = 4096 = 2 12 Product type is Allocation =????? Expected allocation size is (2 20 + 1) * 2 12 = 2 32 + 2 12 = 4,294,971,392 5

XDR Vulnerability malloc(count*size) Actual allocation: Allocation = (count*size) mod 2 32 Allocation = (2 20 + 1) * 2 12 mod 2 32 (due to 32-bit unsigned integer multiplication) Allocation = What is the effect of this error? How can we make this function secure? Signed Multiplication in C Operands: w bits u * v True Product: 2*w bits u v Discard w bits TMult w (u, v) Standard Multiplication Function Ignores high order w bits Some bits will be different for signed vs. unsigned multiplication Lower bits are the same 6

Power-of-2 Multiply with Shift Operation u << k is same as u * 2 k Works for both signed and unsigned Operands: w bits True Product: w+k bits u 2 k Lose top k bits UMult w(u, 2 k ) TMult w(u, 2 k ) Examples u << 3 == u * 8 u << 5 - u << 3 == u * 24 On most machines, shift and add are faster than multiply Compiler will generate this optimization automatically * u 2 k k 0 0 1 0 0 0 0 0 0 0 0 0 Compiled Multiplication Code The C compiler automatically generates shift/add code when multiplying by constant: In C Function int mul12; mul12 = x * 12; Compiled Arithmetic Operations leal (%eax,%eax,2), %eax sall $2, %eax Explanation t <- x+x*2 return t << 2; 7

Unsigned Power-of-2 Divide with Shift Quotient of Unsigned by Power of 2 u >> k gives ë u / 2 k û Uses logical shift Operands: Division: Result: u 2 k / u / 2 k ë u / 2 k û k 0 0 1 0 0 0 0 0 0 0 0 0. Binary Point Division Computed Hex Binary x 15213 15213 3B 6D 00111011 01101101 x >> 1 7606.5 7606 1D B6 00011101 10110110 x >> 4 950.8125 950 03 B6 00000011 10110110 x >> 8 59.4257813 59 00 3B 00000000 00111011 Compiled Unsigned Division Code C Function unsigned udiv8(unsigned x) { return x/8; } Compiled Arithmetic Operations shrl $3, %eax Explanation # Logical shift return x >> 3; Uses logical shift for unsigned In Java, logical shift written as >>> 8

Signed Power-of-2 Divide with Shift Quotient of Signed by Power of 2 x >> k gives ë x / 2 k û Uses arithmetic shift Rounds wrong direction when x < 0 Operands: Division: Result: x 2 k / x / 2 k RoundDown(x / 2 k ) k Binary Point 0 0 1 0 0 0 0. 0 Division Computed Hex Binary y -15213-15213 C4 93 11000100 10010011 y >> 1-7606.5-7607 E2 49 11100010 01001001 y >> 4-950.8125-951 FC 49 11111100 01001001 y >> 8-59.4257813-60 FF C4 11111111 11000100 Compiled Signed Division Code C Function int idiv8(int x) { return x/8; } Compiled Arithmetic Operations testl %eax, %eax js L4 L3: sarl $3, %eax ret L4: addl $7, %eax jmp L3 Explanation if x < 0 x += 7; # Arithmetic shift return x >> 3; Uses arithmetic shift for int In Java, arith. shift written as >> 9

Arithmetic: Basic Rules Addition: Unsigned/signed: Normal addition followed by truncate, same operation on bit level Unsigned: addition mod 2 w Mathematical addition + possible subtraction of 2w Signed: modified addition mod 2 w (result in proper range) Mathematical addition + possible addition or subtraction of 2w Multiplication: Unsigned/signed: Normal multiplication followed by truncate, same operation on bit level Unsigned: multiplication mod 2 w Signed: modified multiplication mod 2 w (result in proper range) Arithmetic: Basic Rules Left shift Unsigned/signed: same as multiplication by 2 k Always logical shift Right shift Unsigned: logical shift, div (division + round to zero) by 2 k Signed: arithmetic shift Positive numbers: div (division + round to zero) by 2 k Negative numbers: div (division + round away from zero) by 2 k, use biasing to fix 10

Why Use Unsigned? Not just because you have non-negative numbers! Why? Easy to make mistakes unsigned i; for (i = count-2; i >= 0; i--) // does the loop terminate? a[i] += a[i+1]; Mistakes can be very subtle #define DELTA sizeof(int) int i; for (i = count; i-delta >= 0; i-= DELTA)... Counting Down with Unsigned Proper way to use unsigned as loop index unsigned i; for (i = count-2; i < count; i--) a[i] += a[i+1]; See Robert Seacord, Secure Coding in C and C++ C Standard guarantees that unsigned addition will behave like modular arithmetic Even better size_t i; for (i = count-2; i < count; i--) a[i] += a[i+1]; Data type size_t defined as unsigned value with length = word size Code will work even if count = UMax What if count is signed and < 0? 11

Why Should I Use Unsigned? (cont.) Do Use When Performing Modular Arithmetic - Multiprecision arithmetic Do Use When Using Bits to Represent Sets - Logical right shift, no sign extension Summary: Integer Operations Representation: unsigned and signed Conversions and casting Bit representation maintained but reinterpreted Expanding, truncating Truncating = modular arithmetic Addition, negation, multiplication, shifting Operations are mod 2 w Ordering properties do not hold u > 0 does not mean u + v > v u, v > 0 does not mean u v > 0 12