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

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

ECE 250 / CS 250 Computer Architecture. C Programming

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

ECE 250 / CPS 250 Computer Architecture. C Programming

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

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

COMP2611: Computer Organization. Data Representation

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

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Binary Representations and Arithmetic

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

CPS 104 Computer Organization and Programming

Representation of Information

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

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

Number System. Introduction. Decimal Numbers

CSE 12 Spring 2016 Week One, Lecture Two

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Survey. Motivation 29.5 / 40 class is required

CS 64 Week 0 Lecture 1. Kyle Dewey

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Arithmetic and Bitwise Operations on Binary Data

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Inf2C - Computer Systems Lecture 2 Data Representation

Number Systems and Number Representation

211: Computer Architecture Summer 2016

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

More about Binary 9/6/2016

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

CSE 12 Spring 2018 Week One, Lecture Two

CS61C : Machine Structures

Understanding Pointers

CS61, Fall 2012 Section 2 Notes

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS 261 Fall Mike Lam, Professor Integer Encodings

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Quiz 0 Review Session. October 13th, 2014

COMP Overview of Tutorial #2

The Design of C: A Rational Reconstruction"

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

COMP2121: Microprocessors and Interfacing. Number Systems

Bits, Words, and Integers

CS & IT Conversions. Magnitude 10,000 1,

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

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code

CS 241 Data Organization Binary

Integer Representation

Signed Binary Numbers

CS2630: Computer Organization Homework 1 Bits, bytes, and memory organization Due January 25, 2017, 11:59pm

Arithmetic and Bitwise Operations on Binary Data

SIGNED AND UNSIGNED SYSTEMS

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

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

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

PROGRAMMAZIONE I A.A. 2017/2018

CSCI 2212: Intermediate Programming / C Chapter 15

Lecture Notes on Types in C

Chapter 2 Bits, Data Types, and Operations

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

UNIT 7A Data Representation: Numbers and Text. Digital Data

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Chapter 3: Arithmetic for Computers

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

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

BITS, BYTES, AND INTEGERS

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

COMP 524 Spring 2018 Midterm Thursday, March 1

Heap Arrays. Steven R. Bagley

Beginning C Programming for Engineers

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

Dynamic Memory Allocation and Command-line Arguments

Topic Notes: Bits and Bytes and Numbers

The type of all data used in a C++ program must be specified

EL2310 Scientific Programming

Memory Addressing, Binary, and Hexadecimal Review

Representing Information. Bit Juggling. - Representing information using bits - Number representations. - Some other bits - Chapters 1 and 2.3,2.

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

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Chapter 2. Data Representation in Computer Systems

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

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

Chapter 11 Introduction to Programming in C

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

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

Transcription:

ECE 250 / CS 250 Computer Architecture C to Binary: Memory & Data Representations Benjamin Lee Slides based on those from Alvin Lebeck, Daniel Sorin, Andrew Hilton, Amir Roth, Gershon Kedem

Administrivia What did you learn last week? Pointers are hard! Today More pointers / memory Data representations Resources (from course web page) Video snippets by Prof Drew Hilton in ECE o Videos don t work with Firefox (use Safari or Chrome) MIT Open Course 2

A Program s View of Memory What is Memory? A large linear array of bits Find things by indexing into array memory address (unsigned integer) read to and write from address Processor issues commands to read/write specific locations Read from memory location 0x1400 Write 0xff to memory location 0x8675309 Array of Bytes? 32-bit ints? 64-bit ints? Memory Address Memory 1 2 3 4 2 n -1-4 0 00110110 00001100 2 n -1 3 3

Text for instructions add dest, src1, src2 Memory Partitions mem[dest] = mem[src1] + mem[src2] Data static (constants, global variables) dynamic (heap, new allocated) grows up Stack local variables grows down Variables are names for memory locations int x; 2 n -1 0 Stack Heap Data Text Reserved Typical Address Space 4 4

A Simple Program s Memory Layout... int result; // global var main() { } int x;... result = x + result;... mem[0x208] = mem[0x400] + mem[0x208] 2 n -1 Stack x 0x400 5 Heap Data result 0x208 3 Text 0 Reserved 5 5

Pointers address of operator & don t confuse with bitwise AND operator (later) Given Then int x; int* p; // p points to an int p = &x; *p = 2; and x = 2; produce the same result Note: p is a pointer, *p is an int What happens for p = 2? On 32-bit machine, p is 32-bits x 0x26cf0... p 0x26d00 0x26cf0 6

C Memory Allocation How do you allocate an object in Java? What do you do when you are finished with an object? Garbage collection Counts references to objects, when == 0 can reuse C does not have garbage collection Must explicitly manage memory void * malloc(nbytes) Obtain storage for your data (like new in Java) Use sizeof(type), which returns bytes needed for type Cast return value into appropriate type(int) malloc(sizeof(int)); free(ptr) Return the storage when you are finished (no Java equivalent) ptr must be a value previously returned from malloc 7

Memory Manager (Heap Manager) Malloc & free are library routines that handle memory management for the heap (allocation / deallocation) Java has garbage collection C must use free else memory leak -> no more available memory Stack Available Memory Allocated Memory (part of this is data structures for managing memory Write a Heap Manager in Compsci 310 Text Memory 8

Linked List (two nodes) #include <stdio.h> #include <stdlib.h> struct list_ent { }; main() { int id; struct list_ent *next; struct list_ent *head, *ptr; head = (struct list_ent *) malloc(sizeof(struct list_ent)); head->id = 66; head->next = NULL; ptr = (struct list_ent *) ptr->id = 23; malloc(sizeof(struct list_ent)); ptr->next = NULL; head->next = ptr; printf("head id: %d, next id: %d\n", head->id,head->next->id); ptr = head; head = ptr->next; printf("head id: %d, next id: %d\n", head->id, ptr->id); free(head); free(ptr); } 9

Back to C: Command Line Arguments Parameters to main (int argc, char *argv[]) argc = number of arguments (0 to argc-1)! argv is array of strings (i.e., array of character pointers) argv[0] = program name main(int argc, char *argv[]) {! int i;! }! printf("%d arguments\n", argc);! for (i=0; i< argc; i++)! printf("argument %d: %s\n", i, argv[i]); 10

C Summary C Language is lower level than Java Many things are similar Data types Control flow Some important differences No objects! Explicit memory allocation/deallocation Create and compile a program Intro to Memory & Pointers Up Next: So what are those chars, ints, floats? 11

Representations: Thought Experiment Do this at home with friends Using only the three symbols @ # $ specify a representation for the following: All integers from 0 to 10 Commands to 1) walk, 2) turn, 3) sit, 4) raise right arm, 5) raise left arm Using only your representation write down series of commands & integers (if appropriate, e.g., raise left arm-3, turn-2) Must have at least 5 commands Give someone your representations from above and the series of commands, see if they can execute the commands. 12

Data Representation Compute two hundred twenty nine minus one hundred sixty seven divided by twelve Compute XIX - VII + IV We reason about numbers many different ways Computers store variables (data) Typically numbers, characters or combination of these Computers have instructions (operations like add) The key is to use a representation that is efficient 13

Number Systems A number is a mathematical concept 10 Many ways to represent a number 10, ten, 2x5, X, 100/10, Symbols are used to create a representation Which representation is best for counting? Which representation is best for addition and subtraction? Which representation is best for multiplication and division? 14

More Number Systems Humans use decimal (base 10) digits 0-9 are composed to make larger numbers 11 = 1*10 1 + 1*10 0 weighted positional notation Addition and Subtraction are straightforward carry and borrow (today called regrouping) Multiplication and Division less so can use logarithms and then do adds and subtracts The key is to use a representation that is efficient 15

Number Systems for Computers Today s computers are built from transistors Transistor is either off or on Need to represent numbers using only off and on two symbols off and on can represent the digits 0 and 1 BIT is Binary Digit A bit can have a value of 0 or 1 Everything in a computer is represented using Bits! 16

Representing High Level Things in Binary Computers represent everything using binary (0 or 1) Instructions are specified using binary Instructions must be able to describe Operation types (add, subtract, shift, etc.) Data objects (integers, decimals, characters, etc.) Memory locations Example: int x, y; // Where are x and y? How to represent an int? bool decision; // How do we represent a bool? Where is it? y = x + 7; // How do we specify add? How to represent 7? decision=(y>18); // Etc. 17

Representing Operation Types How do we tell computer to add? Shift? Read from memory? Etc. Arbitrarily! J Each Instruction Set Architecture (ISA) has its own binary encodings for each operation type E.g., in MIPS: Integer add is: 00000 010000 Read from memory (load) is: 010011 Etc. More on Instruction Sets later this semester 18

Representing Data Types How do we specify an integer? A character? A floating point number? A bool? Etc. Same as before: binary! Key Idea: the same 32 bits might mean one thing if interpreted as an integer but another thing if interpreted as a floating point number, and yet another if interpreted as an instruction, etc. 19

Basic Data Types Bit (bool): 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 128 bits is a quad-word Integers (int, long): 2's Complement (32-bit or 64-bit representation) Floating Point (float, double): Single Precision (32-bit representation) Double Precision (64-bit representation) Extended (Quad) Precision (128-bit representation) Character (char): ASCII 7-bit code 20

Binary, Octal and Hexadecimal numbers Computers can input and output decimal numbers but they convert them to internal binary representation. Binary is good for computers, hard for us to read Use numbers easily computed from binary Binary numbers use only two different digits: {0,1} Example: 1200 10 = 0000010010110000 2 Octal numbers use 8 digits: {0-7} Example: 1200 10 = 02260 8 Hexadecimal numbers use 16 digits: {0-9, A-F} Example: 1200 10 = 04B0 16 = 0x04B0 does not distinguish between upper and lower case 21

Issues for Binary Representation of Numbers There are many ways to represent numbers in binary Binary representations are encodings à many encodings possible 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 22

Unsigned Binary Numbers Weighted positional notation using base 2 11 10 = 1*2 3 + 1*2 1 + 1*2 0 = 1011 2 11 10 = 8 + 2 + 1 Only positive numbers unsigned int data type What is largest number, given 4 bits? 23

Binary and Hex To convert to and from hex: group binary digits in groups of four and convert according to table 2 4 = 16 Hex Bin Hex Bin 0 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 Example: 1100 0010 0110 0111 0100 1111 1101 0101 2 C 2 6 7 4 F D 5 16 24

Sign Magnitude Use leftmost bit for + (0) or (1): 6-bit example (1 sign bit + 5 magnitude bits): +17 = 010001-17 = 110001 Pros: Conceptually simple Easy to convert Cons: Harder to compute (add, subtract, etc.) with Positive and negative 0: 000000 and 100000 25

1 s Complement Representation for Integers Use largest positive binary numbers to represent negative numbers To negate a number, invert ( not ) each bit: 0 à 1 1 à 0 Cons: two 0s hard to compute with 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 26

2 s Complement Integers Use large positives to represent negatives (-x) = 2 n - x This is 1 s complement + 1 (-x) = 2 n - 1 - x + 1 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 27

Pros and Cons of 2 s Complement Advantages: Only one representation for 0 (unlike 1 s comp): 0 = 000000 Addition algorithm is much easier than with sign and magnitude o 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 28

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 type 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. 29

Binary Math : Addition Suppose we want to add two 8-bit numbers: 00011101 + 00101011 How do we do this? 30

Binary Math : Addition Suppose we want to add two 8-bit numbers: 00011101 695 + 00101011 + 232 How do we do this? Let s revisit decimal addition Think about the process as we do it 31

Binary Math : Addition Suppose we want to add two 8-bit numbers: 00011101 695 + 00101011 + 232 7 First add one s digit 5+2 = 7 32

Binary Math : Addition Suppose we want to add two 8-bit numbers: 1 00011101 695 + 00101011 + 232 27 First add one s digit 5+2 = 7 Next add ten s digit 9+3 = 12 (2 carry a 1) 33

Binary Math : Addition Suppose we want to add two 8-bit numbers: 00011101 695 + 00101011 + 232 927 First add one s digit 5+2 = 7 Next add ten s digit 9+3 = 12 (2 carry a 1) Last add hundred s digit 1+6+2 = 9 34

Binary Math : Addition Suppose we want to add two 8-bit numbers: 00011101 + 00101011 Back to the binary: First add 1 s digit 1+1 =? 35

Binary Math : Addition Suppose we want to add two 8-bit numbers: 1 00011101 + 00101011 0 Back to the binary: First add 1 s digit 1+1 = 2 (0 carry a 1) 36

Binary Math : Addition Suppose we want to add two 8-bit numbers: 11 00011101 + 00101011 00 Back to the binary: First add 1 s digit 1+1 = 2 (0 carry a 1) Then 2 s digit: 1+0+1 =2 (0 carry a 1) You all finish it out. 37

Binary Math : Addition Suppose we want to add two 8-bit numbers: 111111 00011101 = 29 + 00101011 = 43 01001000 = 72 Can check our work in decimal 38

What about this one: Binary Math : Addition 01011101 + 01101011 39

What about this one: 1111111 Binary Math : Addition 01011101 = 93 + 01101011 = 107 11001000 = -56 But that can t be right? What do you expect for the answer? What is your expected answer in 8-bit signed 2 s complement? 40

Software Implication! Integer Overflow Answer should be 200 Not representable in 8-bit signed representation No right answer Call Integer Overflow Real problem in programs 41

Subtraction 2 s complement makes subtraction easy: Remember: A - B = A + (-B) And: -B = ~B + 1 é that means flip bits ( not ) So we just flip the bits and start with CI = 1 Later: No new circuits to subtract (re-use add) 1 0110101 -> 0110101-1010010 + 0101101 42

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 43

Option 1: Fixed Point Representation Decimal fixed point (6 digits) 102832 1028.32 Same digits, different decimal point Right of decimal point weight is 1/10 -i (i starts at 1 -> 1/10, 1/100, ) Binary Point (6 bits) 001010 0010.10 Right of binary point weight is 1/2 -i (i starts at 1 -> ½, ¼, 1/8, ) General Fixed Point representation specifies Total number of bits (i.e., width) Fixed point position (e.g., fixed<6,0> fixed<6,2>) It s all just bits what decimal values are above binary numbers? 44

Fixed Point Representation Pros: Addition/subtraction just like integers ( free ) Cons: Mul/div require renormalizing (divide by 64K) Range limited (no good rep for large + small) Can be good in specific situations 45

Can we do better? Think about scientific notation for a second: For example: 6.82 * 10 23 Real number, but comprised of ints: 6 generally only 1 digit here 82 10 23 any number here always 10 (base we work in) can be positive or negative Can we do something like this in binary? 46

Option 2: Floating Point Representation How about: +/- X.YYYYYY * 2 +/-N Big numbers: large positive N Small numbers (<1): negative N Numbers near 0: small N This is floating point : most common way 47