CPSC 213. Introduction to Computer Systems. Numbers and Memory. Unit 1a

Similar documents
CPSC 213. Introduction to Computer Systems. Scalars and Arrays. Unit 1b

CPSC 213. Introduction to Computer Systems. Static Scalars and Arrays. Unit 1b

Hardware: Logical View

CS 11 java track: lecture 1

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

Memory, Data, & Addressing I

Memory, Data, & Addressing II CSE 351 Spring

Survey. Motivation 29.5 / 40 class is required

CS 107 Lecture 2: Bits and Bytes (continued)

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

CSE351: Memory, Data, & Addressing I

Programming refresher and intro to C programming

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

Lec 3. Compilers, Debugging, Hello World, and Variables

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

Representation of Information

Topic Notes: Bits and Bytes and Numbers

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

CS 61C: Great Ideas in Computer Architecture Introduction to C

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

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Topic Notes: Bits and Bytes and Numbers

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

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

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

Memory, Data, & Addressing II

Getting started with Java

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

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

Pointer Casts and Data Accesses

Work relative to other classes

COMP2121: Microprocessors and Interfacing. Number Systems

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

Lecture 03 Bits, Bytes and Data Types

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Arithmetic and Bitwise Operations on Binary Data

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

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS240: Programming in C. Lecture 2: Overview

Pointers (1A) Young Won Lim 2/6/18

Bits and Bytes January 13, 2005

Do not start the test until instructed to do so!

Outline. Welcome to C Syntax Variables Operations Function calls Control statement More data type Timer examples

CS367 Test 1 Review Guide

Final CSE 131B Spring 2004

Bits, Bytes and Integers

Motivation was to facilitate development of systems software, especially OS development.

Getting started with Java

CS Programming In C

CompSci 125 Lecture 02

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Pointers (1A) Young Won Lim 2/10/18

EL2310 Scientific Programming

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 11 Introduction to Programming in C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Computer Organization & Systems Exam I Example Questions

Midterms Save the Dates!

Introduction to C An overview of the programming language C, syntax, data types and input/output

C-string format with scanf/printf

Bits, Bytes, and Integers Part 2

Reversing. Time to get with the program

Machine Language, Assemblers and Linkers"

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapter 11 Introduction to Programming in C

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CPS 104 Computer Organization and Programming

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18. with slides by Kip Irvine

assembler Machine Code Object Files linker Executable File

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

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

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Chapter 11 Introduction to Programming in C

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Lecture Notes Companion CPSC 213

C - Basics, Bitwise Operator. Zhaoguo Wang

Programming Project 2

Chapter 11 Introduction to Programming in C

Lecture 1: Overview of Java

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Topic 6: A Quick Intro To C

In case you didn t get it (or you were too lazy to check)

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

6.096 Introduction to C++ January (IAP) 2009

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

PROGRAMMING FUNDAMENTALS

CS 231 Data Structures and Algorithms, Fall 2016

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

Programming in C - Part 2

17. Instruction Sets: Characteristics and Functions

Embedded programming, AVR intro

Transcription:

CPSC 213 Introduction to Computer Systems Unit 1a Numbers and Memory 1

The Big Picture Build machine model of execution for Java and C programs by examining language features and deciding how they are implemented by the machine What is required design an ISA into which programs can be compiled implement the ISA in the hardware simulator Our approach examine code snippets that exemplify each language feature in turn look at Java and C, pausing to dig deeper when C is different from Java design and implement ISA as needed The simulator is an important tool machine execution is hard to visualize without it this visualization is really our WHOLE POINT here 2

In the Lab... write a C program to determine Endianness prints Little Endian or Big Endian get comfortable with Unix command line and tools (important) compile and run this program on two architectures IA32: lin01.ugrad.cs.ubc.ca Sparc: galiano.ugrad.cs.ubc.ca you can tell what type of arch you are on - % uname -a SimpleMachine simulator load code into Eclipse and get it to build write and test MainMemory.java additional material available on the web page at lab time 3

The Main Memory Class The SM213 simulator has two main classes CPU implements the fetch-execute cycle MainMemory implements memory The first step in building our processor implement 6 main internal methods of MainMemory CPU fetch execute read readinteger write writeinteger MainMemory isaligned bytestointeger integertobytes get set 4

The Code You Will Implement /** * Determine whether an address is aligned to specified length. * @param address memory address * @param length byte length * @return true iff address is aligned to length */ protected boolean isaccessaligned (int address, int length) { return false; } 5

/** * Convert an sequence of four bytes into a Big Endian integer. * @param byteataddrplus0 value of byte with lowest memory address * @param byteataddrplus1 value of byte at base address plus 1 * @param byteataddrplus2 value of byte at base address plus 2 * @param byteataddrplus3 value of byte at base address plus 3 * @return Big Endian integer formed by these four bytes */ public int bytestointeger (UnsignedByte byteataddrplus0, UnsignedByte byteataddrplus1, UnsignedByte byteataddrplus2, UnsignedByte byteataddrplus3) { return 0; } /** * Convert a Big Endian integer into an array of 4 bytes * @param i an Big Endian integer * @return an array of UnsignedByte */ public UnsignedByte[] integertobytes (int i) { return null; } 6

** * Fetch a sequence of bytes from memory. * @param address address of the first byte to fetch * @param length number of bytes to fetch * @return an array of UnsignedByte */ protected UnsignedByte[] get (int address, int length) throws... { UnsignedByte[] ub = new UnsignedByte [length]; ub[0] = new UnsignedByte (0); // with appropriate value // repeat to ub[length-1]... return ub; } /** * Store a sequence of bytes into memory. * @param address address of the first memory byte * @param value an array of UnsignedByte values * @throws InvalidAddressException if any address is invalid */ protected void set (int address, UnsignedByte[] value) throws... { byte b[] = new byte [value.length]; for (int i=0; i<value.length; i++) b[i] = (byte) value[i].value(); // write b into memory... } 7

Reading Companion previous module: 1, 2.1 new: 2.2 (focus on 2.2.2 for this week) Textbook A Historical Perspective, Machine-Level Code, Data Formats, "New to C", Data Alignment. 2ed: 3.1-3.2.1, 3.3, "New to C" sidebar of 3.4, 3.9.3 - (skip 3.2.2 and 3.2.3) 1ed: 3.1-3.2.1, 3.3, "New to C" sidebar of 3.4, 3.10 8

Numbers in Memory 9

Binary, Hex, and Decimal Refresher B H D Hexadecimal notation number starts with 0x, each digit is base 16 not base 10 e.g.: 0x2a3 = 2x16 2 + 10x16 1 + 3x16 0 a convenient way to describe numbers when binary format is important each hex digit (hexit) is stored by 4 bits: (0 1)x8 + (0 1)x4 + (0 1)x2 + (0 1)x1 0000 0001 0010 0011 0100 0101 0110 0111 1000 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Examples 0x10 in binary? in decimal? 0x2e in binary? in decimal? 1101 1000 1001 0110 in hex? in decimal? 102 in binary? in hex? 1001 1010 1011 1100 1101 1110 9 a b c d e 9 10 11 12 13 14 1111 f 15 10

Memory and Integers Memory is byte addressed every byte of memory has a unique address, numbered from 0 to N N is huge: billions is common these days (2-16 GB) Integers can be declared at different sizes byte is 1 byte, 8 bits, 2 hexits short is 2 bytes, 16 bits, 4 hexits int or word or long is 4 bytes, 32 bits, 8 hexits long long is 8 bytes, 64 bits, 16 hexits Integers in memory reading or writing an integer requires specifying a range of byte addresses 0 1 2 3 4 5 6 7 8 9... N... 11

Making Integers from Bytes Memory Our first architectural decisions assembling memory bytes into integer registers i Consider 4-byte memory word and 32-bit register it has memory addresses i, i+1, i+2, and i+3 we ll just say it's at address i and is 4 bytes long e.g., the word at address 4 is in bytes 4, 5, 6 and 7. Big or Little Endian (end means where start from, not finish) we could start with the BIG END of the number (most everyone but Intel) i 2 31 to 2 24 2 23 to 2 16 2 15 to 2 8 2 7 to 2 0 i + 1 i + 2 i + 3 Register bits or we could start with the LITTLE END (Intel x86, some others)... i + 1 i + 2 i + 3... i + 3 2 31 to 2 24 2 23 to 2 16 2 15 to 2 8 2 7 to 2 0 i + 2 i + 1 i Register bits 12

Aligned or Unaligned Addresses we could allow any number to address a multi-byte integer or we could require that addresses be aligned to integer-size boundary address modulo chunk-size is always zero Power-of-Two Aligned Addresses Simplify Hardware - smaller things always fit complete inside of bigger things * disallowed on many architectures * allowed on Intel, but slower * SM213 alignment: 4-byte words word contains exactly two complete shorts - byte address from integer address: divide by power to two, which is just shifting bits j / 2 k == j >> k (j shifted k bits to right) 13

Computing Alignment B H D boolean align(number, size) 0000 0 0 does a number fit nicely for a particular size (in bytes)? 0001 1 1 0010 2 2 divide number n by size s (in bytes), aligned if no remainder easy if number is decimal otherwise convert from hex or binary to decimal check if n mod s = 0 mod notation usually '%'. same as division, of course... check if certain number of final bits are all 0 0011 0100 0101 0110 0111 1000 1001 1010 3 4 5 6 7 8 9 a 3 4 5 6 7 8 9 10 pattern? 1011 b 11 - last 1 digit for 2-byte short 1100 c 12 - last 2 digits for 4-byte world - last 3 digits for 8-byte longlong last k digits, where 2 k =s (size in bytes) easy if number is hex: convert to binary and check 1101 1110 1111 d e f 13 14 15 14

Question Which of the following statement (s) are true [A] 6 == 1102 is aligned for addressing a short [B] 6 == 1102 is aligned for addressing a long [C] 20 == 101002 is aligned for addressing a long [D] 20 == 101002 is aligned for addressing a long long (i.e., 8-byte int) 15

Interlude A Quick C Primer 16

Java Syntax... vs. C Syntax source files.java is source file including packages in source import java.io.* printing System.out.println("blah blah"); compile and run javac foo.java java foo source files.c is source file.h is header file including headers in source #include <stdio.h> printing printf("blah blah\n"); compile and run gcc -o foo foo.c./foo do this at a Unix shell prompt (Linux, Mac Terminal, Sparc, Cygwin on Windows) 17

Java Hello World... import java.io.*; public class HelloWorld { public static void main (String[] args) { System.out.println("Hello world"); } } C Hello World... #include <stdio.h> main() { printf("hello world\n"); } 18

Java and C: Similarities declaration, assignment int a = 4; control flow (often) if (a == 4)... else... for (int i = 0; i < 10; i++) {...} while (i < 10) {...} casting int a; long b; a = (int) b; 19

New in C: Pointers pointers: addresses in memory locations are first-class citizens in C can go back and forth between location and value! pointer declaration: <type>* int* b; // b is a POINTER to an INT getting address of object: & int a; // a is an INT int* b = &a; // b is a pointer to a de-referencing pointer: * a = 10; // assign the value 10 to a *b = 10; // assign the value 10 to a type casting is not typesafe char a[4]; // a 4 byte array *((int*) a) = 1; // treat those four bytes as an INT 0x00000000 0x00000001 0x00000002 0x00000003 0x00000004 0x00000005 0x00000006 0x3e47ad40 0x3e47ad41 0x3e47ad42 0xffffffff...... 20

Back to Numbers... 21

Determining Endianness of a Computer #include <stdio.h> int main () { char a[4]; *((int*)a) = 1; } printf("a[0]=%d a[1]=%d a[2]=%d a[3]=%d\n",a[0],a[1],a[2],a[3]); 22

Which of the following statements are true [A] memory stores Big Endian integers [B] memory stores bytes interpreted by the CPU as Big Endian integers [C] Neither [D] I don t know 23

Which of these are true [A] The Java constants 16 and 0x10 are exactly the same integer [B] 16 and 0x10 are different integers [C] Neither [D] I don't know 24

What is the Big-Endian integer value at address 4 below? [A] 0x1c04b673 Memory [B] 0xc1406b37 [C] 0x73b6041c [D] 0x376b40c1 [E] none of these [F] I don t know 0x0: 0xfe 0x1: 0x32 0x2: 0x87 0x3: 0x9a 0x4: 0x73 0x5: 0xb6 0x6: 0x04 0x7: 0x1c 25

What is the value of i after this Java statement executes? int i = (byte)(0x8b) << 16; [A] [B] [C] [D] [E] [F] 0x8b 0x0000008b 0x008b0000 0xff8b0000 None of these I don t know 26

What is the value of i after this Java statement executes? i = 0xff8b0000 & 0x00ff0000; [A] 0xffff0000 [B] 0xff8b0000 [C] 0x008b0000 [D] I don t know 27