Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Similar documents
CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

CPS 104 Computer Organization and Programming

Number Representations

Chapter 2 Bits, Data Types, and Operations

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Number System

Chapter 2 Bits, Data Types, and Operations

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Chapter 2 Bits, Data Types, and Operations

Data Representation and Binary Arithmetic. Lecture 2

Fundamentals of Programming (C)

Chapter 2 Bits, Data Types, and Operations

Fundamentals of Programming

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

The Binary Number System

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Chapter 3. Information Representation

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

3.1. Unit 3. Binary Representation

EE 109 Unit 2. Binary Representation Systems

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Number System (Different Ways To Say How Many) Fall 2016

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

CSE 30 Fall 2013 Final Exam

ASSIGNMENT 5 TIPS AND TRICKS

Oberon Data Types. Matteo Corti. December 5, 2001

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Number Systems Base r

Simple Data Types in C. Alan L. Cox

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Positional Number System

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Do not start the test until instructed to do so!

CSE 30 Winter 2014 Final Exam

Do not start the test until instructed to do so!

Chapter 8. Characters and Strings

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Fundamental Data Types

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE 30 Spring 2007 Final Exam

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE 30 Fall 2007 Final Exam

Do not start the test until instructed to do so!

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

CSE 30 Fall 2012 Final Exam

CSE 30 Winter 2009 Final Exam

CSE 30 Spring 2006 Final Exam

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

RS-232 Control of the Advantage EQ281/8, EQ282/8 and Advantage SMS200

CSE 30 Fall 2006 Final Exam

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Midterm CSE 131 Winter 2012

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Final CSE 131 Fall 2014

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

Source coding and compression

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

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

Binary Representations and Arithmetic

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Final CSE 131 Winter 2010

COMP2611: Computer Organization. Data Representation

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

UNIT 2 NUMBER SYSTEM AND PROGRAMMING LANGUAGES

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

M1 Computers and Data

CSCI 2212: Intermediate Programming / C Chapter 15

Beginning C Programming for Engineers

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

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

SIGNED AND UNSIGNED SYSTEMS

Digital Representation

Inf2C - Computer Systems Lecture 2 Data Representation

Hardware. ( Not so hard really )

Variables and data types

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

Midterm CSE 131 Winter 2015

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Representation of Information

Final CSE 131 Winter 2012

Midterm CSE 131 Winter 2014

Lecture (09) x86 programming 8

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Transcription:

Outline of Introduction Administrivia What is computer architecture? What do computers do? Representing high level things in binary Data objects: integers, decimals, characters, etc. Memory locations (We ll get to instruction representations a bit later in course) Number Systems for Computers Today s computers are digital systems Each signal in digital system is either off or on Need to represent numbers using only off and on Two symbols offand on can represent the digits 0 and 1 BIT is Binary Digit A bit can have a value of 0 or 1 Binary representation Weighted positional notation using base 2 11 10 = 1*2 3 + 1*2 1 + 1*2 0 = 1011 2 11 10 = 8 + 2 + 1 What is largest 4-bit number? (Hint: it s my office number) ECE 152 30 ECE 152 31 Binary, Octal and Hexadecimal numbers Computers can input/output decimal numbers, but internally they use binary representation Binary is good for computers, hard for us to read We often like to use numbers that are easily computed from binary Binary numbers use only two digits: {0,1} Example: 1200 10 = 0000010010110000 2 Octal numbers (base 8) use 8 digits: {0-7} Example: 1200 10 =? 8 Octal numbers often begin with 0 (e.g., 0317) Hexadecimal (base 16) uses 16 digits: {0-9,A-F} Example: 1200 10 =? 16 Hex numbers should begin with 0x (e.g., 0x7ad24) Hex notation doesn t distinguish between upper/lower case Issues for Binary Representation of Numbers There are many ways to represent numbers in binary What are the issues that we must address? Issue #1: Complexity of arithmetic operations Issue #2: Negative numbers Issue #3: Maximum representable number Choose representation that makes these issues easy for machine, even if it s not easy for humans Why? Machine has to do all the work! ECE 152 32 ECE 152 33

Sign and Magnitude Representation for Integers Unsigned Integers: 100101 2 = 1*2 5 + 0*2 4 + 0*2 3 + 1*2 2 + 0*2 1 + 1*2 0 4 bits max number is 15 (2 4 1) Can represent signed integers with sign and magnitude Add a sign bit Example: 010110 2 = 22 10 ; 110110 2 = -22 10 Advantages: Simple extension of unsigned numbers Same number of positive and negative numbers Easy for humans (although we recall that this isn t so important) Disadvantages: Two representations for 0: 0=000000 and -0=100000 Algorithm (circuit) for addition depends on the arguments signs 1 s Complement Representation for Integers Key: use largest positive binary numbers to represent negative numbers (-x) =2 n -1 x (11 1) x = (11 1) XOR x Simply invert each bit (0 1, 1 0) Unfortunately, still have two zeros 6-bit examples: 010110 = 22 10 ; 101001 = -22 10 1 10 = 000001; -1 10 = 111110 0 10 = 000000; -0 10 = 111111 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000-7 1001-6 1010-5 1011-4 1100-3 1101-2 1110-1 1111-0 ECE 152 34 ECE 152 35 2 s Complement Representation for Integers Pros and Cons of 2 s Complement Still use large positives to represent negatives (-x) =2 n -x This is 1 s complement + 1 (-x) =2 n -1-x + 1 So, just invert bits and add 1 6-bit examples: 010110 2 = 22 10 ; 101010 2 = -22 10 1 10 = 000001 2 ; -1 10 = 111111 2 0 10 = 000000 2 ; -0 10 = 000000 2 good! 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000-8 1001-7 1010-6 1011-5 1100-4 1101-3 1110-2 1111-1 Advantages: Only one representation for 0: 0 = 000000 Addition algorithm is much easier than with sign and magnitude Independent of sign bits Disadvantage: One more negative number than positive Example: 6-bit 2 s complement number 100000 2 = -32 10 ; but 32 10 could not be represented All modern computers use 2 s complement for integers ECE 152 36 ECE 152 37

2 s Complement Negation and Addition To negate a number (positive or negative): Step 1. complement the digits Step 2. add 1 Example 14 10 = 001110 2-14 10 = 110001 2 + 1 110010 2 To add signed numbers, use regular addition, but disregard carry out (Why? Hint: what does carry out equal?) 2 s Complement (cont.) Example: A = 0x0C, B = 0x3B Compute: A + B and A - B in 8-bit 2 s complement arithmetic What s the answer (in hex)? Example: 18 10-14 10 = 18 10 + (-14 10 ) = 4 10 010010 2 +110010 2 000100 2 ECE 152 38 ECE 152 39 2 s Complement Precision Extension Most computers today support 32-bit (int) or 64-bit integers Specify 64-bit using gcc C compiler with long long To extend precision, use sign bit extension Integer precision is number of bits used to represent a number Examples 14 10 = 001110 2 in 6-bit representation. 14 10 = 000000001110 2 in 12-bit representation -14 10 = 110010 2 in 6-bit representation -14 10 = 111111110010 2 in 12-bit representation. What About Non-integer Numbers? There are infinitely many real numbers between two integers Many important numbers are real Speed of light ~= 3x10 8 Pi = 3.1415 Fixed number of bits limits range of integers Can t represent some important numbers Humans use Scientific Notation 1.3x10 4 We ll learn how computers represent floating point numbers later in the semester ECE 152 40 ECE 152 41

What about strings? Many important things stored as strings E.g., your name How should we store strings? ASCII Character Representation Oct. Char 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us 040 sp 041! 042 " 043 # 044 $ 045 % 046 & 047 ' 050 ( 051 ) 052 * 053 + 054, 055-056. 057 / 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077? 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w 170 x 171 y 172 z 173 { 174 175 } 176 ~ 177 del Each character represented by 7-bit ASCII code. Packed into 8-bits ECE 152 42 ECE 152 43 Basic Data Types Bit: 0, 1 Bit String: sequence of bits of a particular length 4 bits is a nibble 8 bits is a byte 16 bits is a half-word 32 bits is a word 64 bits is a double-word Character: ASCII 7 bit code Decimal: (binary coded decimal or BCD) digits 0-9 encoded as 0000-1001 2 decimal digits packed per byte Integers: 2's Complement (32-bit representation) Floating Point: Single Precision (32-bit representation) Double Precision (64-bit representation) Extended Precision (128 -bit representation) Summary of Data Representations Computers operate on binary numbers (0s and 1s) Conversion to/from binary, octal, hex Signed binary numbers 2 s complement arithmetic, negation Real number representation later in course Characters and strings ECE 152 44 ECE 152 45

Computer Memory A Program s View of Memory What is computer memory? What does it look like to the program? How do we find things in computer memory? What is memory?a bunch of bits Looks like a large linear array Find things by indexing into array Index is unsigned integer Most computers support byte (8-bit) addressing Each byte has a unique address (location). Byte of data at address 0x100 and 0x101 Word of data at address 0x100 and 0x104 32-bit v.s. 64-bit addresses We will assume 32-bit for rest of course, unless otherwise stated How many bytes can we address with 32 bits? With 64 bits? Word Address 0 1 2 n -1 Byte Address Memory 0 00110110 1 00001100 2 3 4 2 n -1-4 2 n -1 ECE 152 46 ECE 152 47 Memory Partitions A Simple Program s Memory Layout Text for instructions add res, src1, src2 mem[res] = mem[src1] + mem[src2] Data Static (constants, globals) Dynamic (heap, new allocated) Grows upward Stack Local variables Grows down from top of memory Variables are names for memory locations int x; // x is a location in memory 2 n-1 0 Stack Data Text Reserved Typical Address Space... int result; // global variable x 0x400 main() { int x; // allocated on stack... result = x + result;... } result 0x208 2 n-1 0 Stack 5 Data 3 Text add r,s1,s2 Reserved ECE 152 48 ECE 152 49

Pointers A pointer is a memory location that contains the address of another memory location address of operator & in C/C++ Don t confuse with bitwise AND operator (which is && operator) Given int x; int* p; p = &x; // p points to x (i.e., p is the address of x) Then *p = 2; and x = 2; produce the same result On 32-bit machine, p is 32-bits x 0x26cf0... p 0x26d00 0x26cf0 Arrays In C++: allocate using array form of new int* a = new int[100]; double* b = new double[300]; new[] returns a pointer to a block of memory How big? Where? Size of chunk can be set at runtime delete[] a; // storage returned In C: int* ptr = malloc(nbytes); free(ptr); ECE 152 50 ECE 152 51 Address Calculation More Pointer Arithmetic x is a pointer, what is x+33? A pointer, but where? What does calculation depend on? Result of adding an int to a pointer depends on size of object pointed to Result of subtracting two pointers is an int (d + 3) - d = int * a = new int[100] 0 1 32 33 98 99 a[33] is the same as *(a+33) if a is 0x00a0, then a+1 is 0x00a4, a+2 is 0x00a8 (decimal 160, 164, 168) double * d = new double[200]; 0 1 33 199 *(d+33) is the same as d[33] if d is 0x00b0, then d+1 is 0x00b8, d+2 is 0x00c0 (decimal 176, 184, 192) address one past the end of an array is ok for pointer comparison only what s at *(begin+44)? what does begin++ mean? how are pointers compared using < and using ==? what is value of end - begin? 15 16 42 0 1 43 char* a = new char[44]; char* begin = a; char* end = a + 44; while (begin < end) { *begin = z ; begin++; } ECE 152 52 ECE 152 53

More Pointers & Arrays int* a = new int[100]; 0 1 32 33 98 99 a is a pointer *a is an int a[0] is an int (same as *a) a[1] is an int a+1 is a pointer a+32 is a pointer *(a+1) is an int (same as a[1]) *(a+99) is an int *(a+100) is trouble #include <iostream.h> main() { int* a = new int[100]; int* p = a; int k; } Array Example for (k = 0; k < 100; k++) { *p = k; p++; } cout << "entry 3 = " << a[3] << endl; ECE 152 54 ECE 152 55 Strings as Arrays Bit Manipulations A string is an array of characters with \0 at the end Each element is one byte, ASCII code \0 is null (ASCII code 0) s t r i g \0 0 1 15 16 42 43 Problem 32-bit word contains many values e.g., input device, sensors, etc. Current x,y position of mouse and which button (left, mid, right) Assume x, y position is 0-255 How many bits for position? How many for button? Goal Extract position and button from 32-bit word Need operations on individual bits of binary number ECE 152 56 ECE 152 57

Bitwise AND / OR &operator performs bitwise AND operator performs bitwise OR Per bit (remember your truth tables from ECE 151) 0 & 0 = 0 0 0 = 0 0 & 1 = 0 0 1 = 1 1 & 0 = 0 1 0 = 1 1 & 1 = 1 1 1 = 1 For multiple bits, apply operation to individual bits in same position AND 011010 101110 001010 OR 011010 101110 111110 Mouse Example 32-bit word with x, y, and button fields Bits 0-7 contain x position Bits 8-15 contain y position Bits 16-17 contain button (0 = left, 1 = middle, 2 = right) To extract value, we need to clear all other bits How do we use bitwise operations to do this? button y x 0x1a34c = 01 1010 0011 0100 1100 ECE 152 58 ECE 152 59 Mouse Solution More of the Mouse Solution AND with a bit mask Specific values that clear some bits, but pass others through To extract x position, use mask 0x000ff xpos = 0x1a34c & 0x000ff button y x 0x1a34c = 01 1010 0011 0100 1100 0x000ff = 00 0000 0000 1111 1111 0x0004c = 00 0000 0000 0100 1100 To extract y position use mask 0x0ff00 ypos = 0x1a34c & 0x0ff00 Similarly, button is extracted with mask 0x30000 button = 0x1a34c & 0x30000 Not quite done why? button y x 0x1a34c = 01 1010 0011 0100 1100 0x000ff = 00 1111 1111 0000 0000 0x0a300 = 00 1010 0011 0000 0000 ECE 152 60 ECE 152 61

The SHIFT operator >>is shift right, << is shift left, operands are int and number of positions to shift (1 << 3) is 000001 0001000 (it s 2 3 ) 0xff00 is 0xff << 8, and 0xff is 0xff00 >> 8 So, true ypos value is ypos = (0x1a34c & 0x0ff00) >> 8 button = (0x1a34c & 0x30000) >> 16 Summary: Representing High Level in Computer Computer memory is linear array of bytes Pointer is memory location that contains address of another memory location Bitwise operations We ll visit these topics again throughout semester ECE 152 62 ECE 152 63 Outline of Introduction Administrivia What is computer architecture? What do computers do? Representing high level things in binary What You Will Learn In This Course The basic operation of a computer Primitive operations (instructions) Computer arithmetic Instruction sequencing and processing Memory Input/output Doing all of the above, just faster! Understand the relationship between abstractions Interface design High-level program to control signals (SW HW) ECE 152 64 ECE 152 65

Course Outline Introduction to Computer Architecture Instruction Sets & Assembly Programming (next!) Central Processing Unit (CPU) Memory Hierarchy I/O Devices and Networks Pipelined and Parallel Processors Computer Performance Analysis (time permitting) ECE 152 66