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

Similar documents
CPS 104 Computer Organization and Programming

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

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

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

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

Topic Notes: Bits and Bytes and Numbers

CSE 12 Spring 2016 Week One, Lecture Two

Topic Notes: Bits and Bytes and Numbers

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CENG3420 Lecture 03 Review

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Representing numbers on the computer. Computer memory/processors consist of items that exist in one of two possible states (binary states).

Hardware: Logical View

Memory, Data, & Addressing II CSE 351 Spring

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

CSE 12 Spring 2018 Week One, Lecture Two

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

CSC 1600 Memory Layout for Unix Processes"

Chapter 2A Instructions: Language of the Computer

Integer Representation

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

ECE232: Hardware Organization and Design

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Computer Organization MIPS ISA

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

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

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

M1 Computers and Data

Beginning C Programming for Engineers

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

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

ECE331: Hardware Organization and Design

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Data III & Integers I

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

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

Representation of Information

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

CS 241 Data Organization Binary

Inf2C - Computer Systems Lecture 2 Data Representation

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C

211: Computer Architecture Summer 2016

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

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Computer Organization & Systems Exam I Example Questions

Bits and Bytes January 13, 2005

Where we are going (today)

Data III & Integers I

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

EC 413 Computer Organization

Lecture 7: Examples, MARS, Arithmetic

MIPS Memory Access Instructions

PRINCIPLES OF OPERATING SYSTEMS

C-string format with scanf/printf

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

Memory, Data, & Addressing I

Memory, Data, & Addressing II

COMP2611: Computer Organization. Data Representation

ECE232: Hardware Organization and Design

We would like to find the value of the right-hand side of this equation. We know that

CS61, Fall 2012 Midterm Review Section

Survey. Motivation 29.5 / 40 class is required

CS 11 C track: lecture 5

JAVA OPERATORS GENERAL

EL2310 Scientific Programming

Class Information ANNOUCEMENTS

Final CSE 131B Spring 2004

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Section Notes - Week 1 (9/17)

C Programming & Memory

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

Intermediate Programming, Spring 2017*

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

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

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

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

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

CS Programming In C

CS 61c: Great Ideas in Computer Architecture

Final CSE 131B Spring 2005

Instructions: Language of the Computer

Arrays and Pointers. CSE 2031 Fall November 11, 2013

o Code, executable, and process o Main memory vs. virtual memory

Arrays, Pointers and Memory Management

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

More about Binary 9/6/2016

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

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

Transcription:

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

Homework #1 Due Feb 6 Reading TAs Finish Chapter 1 Start Chapter 2 Admin +1 UTA: Michael Zhou Lindsay is Head TA Office Hours Posted See Piazza or course webpage 2

Last time Who can remind us what we covered last time? 3

Last time Who can remind us what we covered last time? Representing positive and negative integer Sign Magnitude 1 s Complement 2 s Complement 2 s Complement math Addition, subtraction, negation Representing real numbers Fixed Point Rational IEE Floating Point Characters/strings 4

Computer Memory Where do we put these numbers? Registers [more on these later] Memory 5

Computer Memory Where do we put these numbers? Registers [more on these later] In the processor Compute directly on them Few of them (~16 or 32) Memory 6

Computer Memory Where do we put these numbers? Registers [more on these later] In the processor Compute directly on them Few of them (~16 or 32) Memory [Our focus now] External to processor Load/store values to/from registers Very large (multiple GB) 7

Memory Organization Memory: billions of numbers how to get the right one? We number our numbers! Each memory location has an address Processor asks to read or write specific address Memory, please load address 0x123400 Memory, please write 0xFE into address 0x8765000 Kind of like a giant array 8

Memory Organization Memory: billions of numbers how to get the right one? We number our numbers! Each memory location has an address Processor asks to read or write specific address Memory, please load address 0x123400 Memory, please write 0xFE into address 0x8765000 Kind of like a giant array Array of what? Bytes? 32-bit ints? 64-bit ints? 9

Memory Organization Most systems: byte (8-bit) addressed Memory is array of bytes Each address specifies 1 byte Support to load/store 16, 32, 64 bit quantities Byte ordering varies from system to system Some systems word addressed Memory is array of words Word is int Smaller operations faked in processor Not very common 10

Byte Order Word of the Day: Endianess Big Endian: byte 0 is 8 most significant bits IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA Little Endian: byte 0 is 8 least significant bits Intel 80x86, DEC Vax, DEC Alpha little endian byte 0 msb (most significant bit) 3 2 1 0 0 1 2 3 lsb (least significant bit) big endian byte 0 11

Endianess Example: 12 as 32-bit int at address 0x1234: Address Little Endian 0x1237 0x00 0x0C 0x1236 0x00 0x00 0x1235 0x00 0x00 0x1234 0x0C 0x00 Big Endian Programming impact: machines of different Endianess Network programs htons, htonl, ntohl, ntohs Saved data files 12

Alignment Alignment: require that objects fall on address that is multiple of their size. 32-bit integer Aligned if address % 4 = 0 64-bit integer? Aligned if? Aligned Not Aligned Byte # 0 1 2 3 Results of un-aligned access? Depends Nothing Accepted, but slow (very very slow?) Crash your program 13

Memory Layout Program has many pieces, all in memory Thinks it has all of memory to itself Pieces of a program: Instructions: Called text Just numbers! (next few weeks) Statically allocated data Global variables Dynamically allocated data: Heap new (Java), malloc (C) Grows up (to higher addresses) Local variables, parameters, RA: Stack Grows down 2 n-1 0 Stack Heap Data Text Reserved Typical Address Space 14

Memory Layout: Example int anumber = 3; int factorial (int x) { 2 n-1 Stack if (x == 0) { return 1; else { return x * factorial (x 1); Heap Typical Address Space int main (void) { Data int z = factorial (anumber); printf( %d\n, z); return 0; 0 Text Reserved 15

Let s do a little Java public class Example { public static void swap (int x, int y) { int temp = x; x = y; y = temp; public static void main (String[] args) { int a = 42; int b = 100; swap (a, b); System.out.println( a = + a + b = + b); What does this print? Why? 16

Let s do a little Java public class Example { public static void swap (int x, int y) { int temp = x; x = y; y = temp; public static void main (String[] args) { int a = 42; int b = 100; swap (a, b); System.out.println( a = + a + b = + b); What does this print? Why? Stack main a 42 b 100 17

Let s do a little Java public class Example { public static void swap (int x, int y) { c0 int temp = x; x = y; y = temp; public static void main (String[] args) { int a = 42; int b = 100; swap (a, b); System.out.println( a = + a + b = + b); What does this print? Why? Stack main a 42 b 100 swap x 42 y 100 temp??? RA c0 18

Let s do a little Java public class Example { public static void swap (int x, int y) { c0 int temp = x; x = y; y = temp; public static void main (String[] args) { int a = 42; int b = 100; swap (a, b); System.out.println( a = + a + b = + b); What does this print? Why? Stack main a 42 b 100 swap x 42 y 100 temp 42 RA c0 19

Let s do a little Java public class Example { public static void swap (int x, int y) { c0 int temp = x; x = y; y = temp; public static void main (String[] args) { int a = 42; int b = 100; swap (a, b); System.out.println( a = + a + b = + b); What does this print? Why? Stack main a 42 b 100 swap x 100 y 100 temp 42 RA c0 20

Let s do a little Java public class Example { public static void swap (int x, int y) { c0 int temp = x; x = y; y = temp; public static void main (String[] args) { int a = 42; int b = 100; swap (a, b); System.out.println( a = + a + b = + b); What does this print? Why? Stack main a 42 b 100 swap x 100 y 42 temp 42 RA c0 21

Let s do a little Java public class Example { public static void swap (int x, int y) { int temp = x; x = y; y = temp; public static void main (String[] args) { int a = 42; int b = 100; swap (a, b); System.out.println( a = + a + b = + b); What does this print? Why? Stack main a 42 b 100 22

Let s do some different Java public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); System.out.println( a = + a.data + What does this print? Why? b = + b.data); Stack main a??? b??? 23

Let s do some different Java public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); System.out.println( a = + a.data + What does this print? Why? b = + b.data); Stack main a b??? Heap Ex2 data 42 24

Let s do some different Java public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; a b Stack main Heap Ex2 data 42 x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); Ex2 data 100 System.out.println( a = + a.data + b = + b.data); What does this print? Why? 25

Let s do some different Java c0 public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); System.out.println( a = + a.data + What does this print? Why? b = + b.data); a b Stack main swap x y temp?? RA c0 Heap Ex2 data 42 Ex2 data 100 26

Let s do some different Java c0 public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); System.out.println( a = + a.data + What does this print? Why? b = + b.data); a b Stack main swap x y temp 42 RA c0 Heap Ex2 data 42 Ex2 data 100 27

Let s do some different Java c0 public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); System.out.println( a = + a.data + What does this print? Why? b = + b.data); a b Stack main swap x y temp 42 RA c0 Heap Ex2 data 100 Ex2 data 100 28

Let s do some different Java c0 public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); System.out.println( a = + a.data + What does this print? Why? b = + b.data); a b Stack main swap x y temp 42 RA c0 Heap Ex2 data 100 Ex2 data 42 29

Let s do some different Java public class Ex2 { int data; public Ex2 (int d) { data = d; public static void swap (Ex2 x, Ex2 y) { int temp = x.data; a b Stack main Heap Ex2 data 100 x.data = y.data; y.data = temp; public static void main (String[] args) { Example a = new Example (42); Example b = new Example (100); swap (a, b); Ex2 data 42 System.out.println( a = + a.data + b = + b.data); What does this print? Why? 30

Java has references: References and Pointers Any variable of object type is a reference Point at objects (which are all in the heap) Under the hood: is the memory address of the object Cannot explicitly manipulate them (e.g., add 4) 31

Java has references: References and Pointers Any variable of object type is a reference Point at objects (which are all in the heap) Under the hood: is the memory address of the object Cannot explicitly manipulate them (e.g., add 4) Some languages (C, assembly) have explicit pointers: Hold the memory address of something Can explicitly compute on them Can de-reference the pointer (*ptr) to get thing-pointed-to Can take the address-of (&x) to get something s address Can do very unsafe things, shoot yourself in the foot 32

address of operator & Pointers don t confuse with bitwise AND operator (later today) Given int x; int *p; p = &x; Then *p = 2; and x = 2; produce the same result What happens for p = 2?; On 32-bit machine, p is 32-bits x 0x26cf0... p 0x26d00 0x26cbf0 33

Java: C: Arrays int [] x = new int [nelems]; int data[42]; //if size is known constant int data = malloc (nelem * sizeof(*data)); C++: malloc takes number of bytes sizeof tells how many bytes something takes Has new int[nelems] like Java And malloc(size) like C 34

Address Calculation 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) 35

More Pointer Arithmetic 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? 0 1 15 16 42 43 char * a = new char[44]; char * begin = a; char * end = a + 44; while (begin < end) { *begin = z ; begin++; 36

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 37

Array Example #include <stdio.h> main() { int *a = malloc (100 * sizeof(*a)); int *p = a; int k; for (k = 0; k < 100; k++) { *p = k; p++; printf( entry 3 = %d\n, a[3]) 38

Memory Manager (Heap Manager) malloc / new (C/C++) Library routines that handle memory management for heap (allocation / deallocation) Java has garbage collection (reclaim memory of unreferenced objects) C/C++ must use free/ delete, else memory leak Stack Text Memory Available Memory Allocated Memory (part of this is data structures for managing memory 39

Strings as Arrays s t r i g \0 0 1 15 16 42 43 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) 40

Strlen() strlen() returns the # of characters in a string same as # elements in char array? int strlen(char * s) // pre: \0 terminated // post: returns # chars { int count=0; while (*s++) count++; return count; 41

Vector Class vs. Arrays Vector Class insulates programmers array bounds checking automagically growing/shrinking when more items are added/ deleted How are Vectors implemented? Arrays, re-allocated as needed Arrays can be more efficient but be leery of claims that C-style arrays required for efficiency 42

Memory Outline Memory holds numbers Everything (floats, colors, sound, letters) is a number Bit Manipulations 43

Bit Manipulation: Motivating Example Suppose I want to track 20 Million people 30 properties per person (e.g., IS_MALE, IS_STUDENT, ) Each may or may not apply to any given person How could I represent this? 44

Bad Idea 1: class Person { static final int IS_MALE = 0; static final int IS_STUDENT = 1;. vector<integer> props; boolean hasproperty(int p) { for (int I =0; I < props.size(); i++){ if (props.at(i).intvalue() == p) return true; return false; P.S. Undergrad friend of mine did this, code was very slow 45

Okish Idea class Person { bool is_male; bool is_student; Why is this not so great? 46

Okish Idea class Person { bool is_male; bool is_student; Why is this not so great? Byte is smallest addressable size: typically bool = 1 byte Wastes space As we will see later, wasting space can hurt performance (caches) 47

Great idea class Person { static final int IS_MALE = 0x1; //0000 0001 static final int IS_STUDENT = 0x2; //0000 0010 static final int KNOWS_JAVA = 0x4; //0000 0100 static final int KNOWS_C = 0x8; //0000 1000 int props; bool hasproperty(int p) { return (props & p)!= 0; Use one int, and manipulate/test individual bits Fast, and only need 1 32-bit int (4 bytes) for 32 properties! 48

Bit-wise operations: AND Previous slide used bit-wise & (AND): Do not confuse AND (a & b) with address-of (& a) Do not confuse logical AND (a && b) Each result bit will be 1 iff corresponding bit in a AND b is 1: 1101010 & 0100100 0100000 Using bits as sets, corresponds to intersection 49

Bit-wise OR Bit-wise operations: OR Each result bit will be 1 iff corresponding bit in a OR in b is 1: 1101010 0100100 1101110 Using bits as sets, corresponds to union Add p to props: props = props p; 50

Bit-wise XOR ^ Bit-wise operations: XOR OR is inclusive (either or both) XOR is exclusive-or (either, but not both): 1101010 ^ 0100100 1001110 51

Bit-wise negation ~ (NOT) Flip each bit Bit-wise operations: NOT ~1101010 0010101 Can combine NOT and AND for removal: Remove p from props: props = props & ~p; Suppose props is 11011101 and p is 00010000: ~p = 11101111 Props & ~p: 11011101 11101111 11001101 52

Left shift (<<) Bit-wise operations: SHIFT Moves left, bringing in 0s at right, excess bits fall off 10010001 << 2 = 01000100 x << k corresponds to x * 2 k Logical (or unsigned) right shift (>>) Moves bits right, bringing in 0s at left, excess bits fall off 10010001 >> 3 = 00010010 x >>k corresponds to x / 2 k for unsigned x Arithmetic (or signed) right shift (>>) Moves bits right, brining in (sign bit) at left 10010001 >> 3= 11110010 x >>k corresponds to x / 2 k for signed x 53

Summary Homework #1 Feb 6 Computer memory is linear array of bytes Pointers contain address of other data Bitwise operations Next Time Dive into C Reading Finish Ch 1, Start on Ch 2 54