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

Similar documents
Data Representation and Storage. Some definitions (in C)

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

CS429: Computer Organization and Architecture

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

Section Notes - Week 1 (9/17)

Final CSE 131B Spring 2004

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.

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

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

Learning the Binary System

Computer Organization & Systems Exam I Example Questions

Survey. Motivation 29.5 / 40 class is required

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

CS 11 C track: lecture 5

Final CSE 131B Spring 2005

CS61, Fall 2012 Midterm Review Section

CS C Primer. Tyler Szepesi. January 16, 2013

Computer System and programming in C

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

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

CS367 Test 1 Review Guide

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

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

Computer Systems Programming. Practice Midterm. Name:

Chapter 1 Programming: A General Overview

Integer Encoding and Manipulation

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

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

Computer Science 50: Introduction to Computer Science I Harvard College Fall Quiz 0 Solutions. Answers other than the below may be possible.

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Bitwise Data Manipulation. Bitwise operations More on integers

Hardware: Logical View

Chapter 4. Operations on Data

Expression and Operator

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

17. Instruction Sets: Characteristics and Functions

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

Data structures and libraries

CPS 104 Computer Organization and Programming

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

Compiler Design. Homework 1. Due Date: Thursday, January 19, 2006, 2:00

EC 413 Computer Organization

Learning to Program with Haiku

COMP2611: Computer Organization. Data Representation

CS 253. January 14, 2017

Programming refresher and intro to C programming

1 Dynamic Memory continued: Memory Leaks

EL2310 Scientific Programming

Practical Malware Analysis

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

EL6483: Brief Overview of C Programming Language

Run-time Environments

Run-time Environments

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Data Representation and Storage

CHAPTER 8. Copyright Cengage Learning. All rights reserved.

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Lecture Notes on Memory Layout

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

Recitation #11 Malloc Lab. November 7th, 2017

Eric Roberts Handout #39 CS 106B February 20, 2015 Sets

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

The C Programming Language Guide for the Robot Course work Module

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

This is CS50. Harvard University Fall Quiz 0 Answer Key

Code No: R Set No. 1

SIGNED AND UNSIGNED SYSTEMS

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

ESC101N Fundamentals of Computing

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

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

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

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

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

Reverse Engineering II: The Basics

BASIC COMPUTER ORGANIZATION. Operating System Concepts 8 th Edition

CS3350B Computer Architecture MIPS Instruction Representation

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

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

Chapter 3: Operators, Expressions and Type Conversion

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

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

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

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Static Analysis I PAOLO PALUMBO, F-SECURE CORPORATION

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

CPEG421/621 Tutorial

CENG3420 Lecture 03 Review

Bits and Bytes January 13, 2005

Do not start the test until instructed to do so!

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

EL2310 Scientific Programming

Chapter 10 Error Detection and Correction 10.1

Transcription:

CS 61 Scribe Notes Computer Arithmetic II, Structured Data, Memory Layout (Th 9/13) Renzo Lucioni, Weishen Mead, Kat Zhou Unsigned Computer Arithmetic II Consider the following: We would like to find the value of the right-hand side of this equation. We know that Before forming a hypothesis, we work with small examples and look for patterns. We now have enough information to form a reasonable hypothesis: Let s test our hypothesis with a few more examples, to see if it holds. As we can now tell from the above, Let s try a new, revised hypothesis: When is even, the answer is, and when is odd, the answer is 1. To test this, let s try giving an even value:

Unfortunately, this disproves our revised hypothesis. So what does really equal? It returns the least significant 1 bit of s binary representation (least significant meaning smallest, or the one furthest to the right). Here s the proof: Assume. Say we have, where there are zeroes Then,, where there are ones Finally,, where there are zeroes As can be seen, adding the one causes a sort of ripple effect through the them into zeroes and causing the one to bubble up. ones, turning If we now take, the cancels with ~ to make all zeroes. Anding zeroes and zeroes make more zeroes, leaving only the rightmost one. Multiplication and Division Consider the following: This is because is represented as. After shifting to the left, we are left with represented as, where there are zeroes. More precisely: Similarly, consider this:

Shifts are faster than their regular mathematical counterparts when it comes to division and multiplication, owing primarily to the fact that each bit is affected by one single bit as opposed to each bit being affected by a different bit. Also, the following use of & is faster than modulo: Signed Arithmetic Due to Two s complement, signed addition and unsigned addition produce the same bit pattern. The same is true for signed and unsigned subtraction, and also for signed and unsigned multiplication (since multiplication amounts to nothing more than repeated addition). However, signed and unsigned division do not return the same bit patterns. For example: and have the same bit representations, but they return answers that do not share the same bit pattern. C s Binary Logical Operators C s binary logical operators are:!, &&,, ==!=, <, >, <=, and >=. N.B. Logical operators operate on truth values and only return 1 or 0, where 1 is true and 0 is false. For example,!!x and x!= 0 are equivalent (this is as trick for casting to bool). Data Representation Memory can be thought of as an enormous array of unsigned char. On a 32-bit machine, the largest address possible is. The range of values for an object of type signed short int is -32768 to 32767. An unsigned short int can represent values from 0 to 65535. A short int is 16-bit object (2 bytes). Here s an example of how an object is stored in memory. Pick a short int, say 32767 == 0x7FFF. Let s say we re storing this in the address 0x800000. On the internet, this address

holds 0x7F and the next address holds 0xFF, meaning that more significant bytes are transferred and stored first. This style of ordering the individually addressable sub-components (bytes) within the representation of the larger data item (the short int) is called big endian. On the other hand, in most PCs (in particular those powered by Intel chips, i.e., almost all) 0xFF goes in the lower numbered address and 0x7F goes in the next one, meaning the least significant byte is stored first - this ordering style is referred to as little endian. Let s briefly review the size of a few C data types on a 32-bit machine. An int is 4 bytes, a long long is 8 bytes, and a char * is 4 bytes (8 bytes on a 64-bit machine). Now let s take a look at arrays. As stated previously, memory can be thought of as an enormous array of unsigned char. What is the best way to represent an array of ints? To store 10 ints, allocate a contiguous 40-byte chunk of memory. Why 40 bytes? The amount of memory you must allocate is given by multiplying the number of objects you want to store by the byte size of the object. In this case, an int is 4 bytes long, and you want to store 10 of them, hence you need 40 bytes. Let s say the chunk of memory you have allocated starts at 0x70000; it will end at 0x70028. Such a contiguously allocated subset of memory is referred to as a homogenous collection. An array is an example of a homogenous collection. N.B. Given an array of type, where the address of the array is, the address of item is.

Heterogenous collections are those where every element is not the same (and hence the size of every element is not the same). A good example of a heterogenous collection C is a struct: struct foo { int a; char b; unsigned char c; int *p; Like the array, the struct is also laid out in a contiguous block. However, the objects stored within this block are not of uniform size, as they were in the array. We can inspect the locations of each of these items in memory: int main () { foo p; printf ( %p %p %p\n, &p, &p.a, &p.b, &p.c, &p.p); The struct takes up 12 bytes in memory. The difference between the addresses of p and p.a is 0 bytes (they re at the same location), while the difference between p.a and p.b is 4 bytes, the difference between p.b and p.c is 1 byte, and the difference between p.c and p.p is 3 bytes. Each type has an alignment number that should evenly divide its address. The alignment number of the largest object is always used. In this example, the largest objects are the int and the int * (each of size 4 bytes), and so the alignment number is used is 4. Padding is added at the end of the struct to make its size a multiple of 4, since as we have just discussed, the struct needs to be aligned on a 4-byte boundary. This use of padding explains the 3 byte difference between p.c and p.p (2 bytes of padding are added between them). If we eliminate the int * from the struct, we might reasonably expect the size to be 6 bytes, but in fact the size comes to 8 bytes. This is again due to the fact that the struct must be aligned on a 4-byte boundary, since it still contains an int. Here is an example of our third type of collection, the overlapping collection: union foo{ int a; char b; unsigned char c;

int *p; The type union can be viewed as a variable type that can contain many different variables (like a struct), but only actually holds one of them at a time (not like a struct). The size of a union is equal to the size of its largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest is the most that will be needed. If we print out the addresses of the elements of this union, each element has the same memory address; the union is aligned according to the alignment number of the largest element. Finally, we will inspect the memory usage of the following recursive function used to compute : int factorial(int n){ if (n==0) return 1; else return factorial(n-1)*n; Each time the factorial function is called, a new argument n is generated. Each n has its own address. Where in memory are these n s stored? We can answer this question by printing the address of each n. By doing this, we observe that each subsequent n is located at a lower address than the previous. Also note that each time the program is run anew, new addresses are used for all n s; this is for security purposes. How are functions laid out in memory? As we saw, main s local variables are stored somewhere to the right of center (near, but below, the address 0xC0000000). Function calls move to progressively lower memory addresses, and as function calls return they move back up, reusing the space. Code itself is stored in markedly lower memory addresses, with global variables even lower. The region located in higher addresses, containing local variables and function calls, is called the stack. The region located in lower in lower addresses, between the stack and the code, is called the heap, and is used for dynamic memory allocation.