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

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

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

Bits and Bytes January 13, 2005

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

CS429: Computer Organization and Architecture

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Lecture 5-6: Bits, Bytes, and Integers

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

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Hardware: Logical View

CSCI2467: Systems Programming Concepts

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

Bits, Bytes, and Integers Part 2

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Bits, Bytes, and Integers

Arithmetic and Bitwise Operations on Binary Data

BITS, BYTES, AND INTEGERS

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

Arithmetic and Bitwise Operations on Binary Data

Bits, Bytes, and Integers August 26, 2009

Computer Organization: A Programmer's Perspective

Bits, Bytes and Integers Part 1

Bits, Bytes and Integers

Bits, Bytes, and Integers

Data III & Integers I

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

Data III & Integers I

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

CSE351: Memory, Data, & Addressing I

Computer Systems CEN591(502) Fall 2011

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

Systems Programming and Computer Architecture ( )

Instruction Set Architecture

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

Binary Values. CSE 410 Lecture 02

Data III & Integers I

Bitwise Data Manipulation. Bitwise operations More on integers

Representing and Manipulating Integers Part I

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

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

Integer Encoding and Manipulation

Manipulating Integers

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

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

Foundations of Computer Systems

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

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

Bits, Words, and Integers

Integer Representation Floating point Representation Other data types

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

Representation of Information

JAVA OPERATORS GENERAL

Representing and Manipulating Integers. Jo, Heeseung

CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

History of Computing. Ahmed Sallam 11/28/2014 1

Where we are going (today)

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

Beginning C Programming for Engineers

REMEMBER TO REGISTER FOR THE EXAM.

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

CS 107 Lecture 2: Bits and Bytes (continued)

Systems Programming and Computer Architecture ( )

Computer Organization and Programming

Lecture (04) Boolean Algebra and Logic Gates

Lecture (04) Boolean Algebra and Logic Gates By: Dr. Ahmed ElShafee

Topic Notes: Bits and Bytes and Numbers

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Computer Organization and Levels of Abstraction

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

Memory, Data, & Addressing II CSE 351 Spring

Agenda EE 224: INTRODUCTION TO DIGITAL CIRCUITS & COMPUTER DESIGN. Lecture 1: Introduction. Go over the syllabus 3/31/2010

17. Instruction Sets: Characteristics and Functions

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Outline. x86 Architecture

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

Run time environment of a MIPS program

Data Representa+on in Memory

Systems Programming and Computer Architecture ( )

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

Lecture 4: Instruction Set Architecture

Logic, Words, and Integers

Computer Organization MIPS ISA

Computer Systems A Programmer s Perspective 1 (Beta Draft)

Course Syllabus [1/2]

CS 3330 Introduction. Daniel and Charles. CS 3330 Computer Architecture 1

CSC Discrete Math I, Spring Sets

Computer Organization and Levels of Abstraction

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Chapter Overview. Welcome to Assembly Language

Logic Design: Part 2

Arab Open University. Computer Organization and Architecture - T103

Static Analysis I PAOLO PALUMBO, F-SECURE CORPORATION

Topic Notes: Bits and Bytes and Numbers

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

Few reminders and demos

EE292: Fundamentals of ECE

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Transcription:

Lecture 2 Bits and Bytes Topics! Why bits?! Representing information as bits " Binary/Hexadecimal " Byte representations» numbers» characters and strings» Instructions! Bit-level manipulations " Boolean algebra " Expressing in C Why Don t Computers Use Base 10? Base 10 Number Representation! That s why fingers are known as digits! Natural representation for financial transactions " Floating point number cannot exactly represent $1.20! Even carries through in scientific notation " 1.5213 X 10 4 Implementing Electronically! Hard to store " ENIAC (First electronic computer) used 10 vacuum tubes / digit! Hard to transmit " Need high precision to encode 10 signal levels on single wire! Messy to implement digital logic functions " Addition, multiplication, etc. F2-2 - Datorarkitektur 27 Binary Representations Base 2 Number Representation! Represent 15213 10 as 11101101101101 2! Represent 1.20 10 as 1.11111111[11] 2! Represent 1.5213 X 10 4 as 1.1101101101101 2 X 2 13 Electronic Implementation! Easy to store with bistable elements! Reliably transmitted on noisy and inaccurate wires! Straightforward implementation of arithmetic functions 3.3V 2.8V 0.5V 0.0V 0 1 0 F2-3 - Datorarkitektur 27 Byte-Oriented Memory Organization Programs Refer to Virtual esses! Conceptually very large array of bytes! Actually implemented with hierarchy of different memory types " SRAM, DRAM, disk " Only allocate for regions actually used by program! In Unix and Windows NT, address space private to particular process " Program being executed " Program can clobber its own data, but not that of others Compiler + Run-Time System Control Allocation! Where different program objects should be stored! Multiple mechanisms: static, stack, and heap! In any case, all allocation within single virtual address space F2-4 - Datorarkitektur 27

Encoding Byte Values Byte 8 bits! Binary 2 to 11111111 2! Decimal: 0 10 to 255 10! Hexadecimal 16 to 16 " Base 16 number representation " Use characters 0 to 9 and A to F " Write FA1D37B 16 in C as 0xFA1D37B» Or 0xfa1d37b Hex Decimal Binary 0 0 1 1 01 2 2 10 3 3 11 4 4 01 5 5 0101 6 6 0110 7 7 0111 8 8 10 9 9 11 A 10 1010 B 11 1011 C 12 11 D 13 1101 E 14 1110 F 15 1111 Machine Words Machine Has Word Size! Nominal size of integer-valued data " Including addresses! Most current machines are 32 bits (4 bytes) " Limits addresses to 4GB " Becoming too small for memory-intensive applications! High-end systems are 64 bits (8 bytes) " Potentially address 1.8 X 10 19 bytes! Machines support multiple data formats " Fractions or multiples of word size " Always integral number of bytes F2-5 - Datorarkitektur 27 F2-6 - Datorarkitektur 27 Word-Oriented Memory Organization esses Specify Byte Locations! ess of first byte in word! esses of successive words differ by 4 (32-bit) or 8 (64-bit) 32-bit Words?? 04?? 08?? 12?? 64-bit Words?? 08?? Bytes. 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 F2-7 - Datorarkitektur 27 Data Representations Sizes of C Objects (in Bytes)! C Data Type Compaq Alpha Typical 32-bit Intel IA32 Sun SPARC " int 4 4 4 4 " long int 8 4 4 4 " long long int 8 " char 1 1 1 1 " short int 2 2 2 2 " float 4 4 4 4 " double 8 8 8 8 " long double 8 8 10/12 16 " char * 8 4 4 4» Or any other pointer F2-8 - Datorarkitektur 27

Byte Ordering How should bytes within multi-byte word be ordered in memory? Conventions! Sun s, Mac s are Big Endian machines " Least significant byte has highest address! Alphas, PC s are Little Endian machines " Least significant byte has lowest address Byte Ordering Example Big Endian! Least significant byte has highest address Little Endian! Least significant byte has lowest address Example! Variable x has 4-byte representation 0x01234567! ess given by &x is 0x1 Big Endian 0x1 0x101 0x102 0x103 01 23 45 67 Little Endian 0x1 0x101 0x102 0x103 67 45 23 01 F2-9 - Datorarkitektur 27 F2-10 - Datorarkitektur 27 Reading Byte-Reversed Listings Representing Integers Disassembly! Text representation of binary machine code! Generated by program that reads the machine code int A 15213; long int C 15213; Decimal: 15213 Binary: 11 1011 0110 1101 Hex: 3 B 6 D Example Fragment on IA32 ess Instruction Code Assembly Rendition 8048365: 5b pop %ebx 8048366: 81 c3 ab 12 add $0x12ab,%ebx 804836c: 83 bb 28 cmpl $0x0,0x28(%ebx) Deciphering Numbers! Value: 0x12ab! Pad to 4 bytes: 0x12ab! Split into bytes: 12 ab IA32/Alpha A SPARC A IA32 C Alpha C SPARC C! Reverse: ab 12 F2-11 - Datorarkitektur 27 F2-12 - Datorarkitektur 27

Representing Pointers int B -15213; int *P &B; Alpha ess Hex: 1 F F F F F C A 0 Binary: 01 1111 1111 1111 1111 1111 11 1010 SPARC P SPARC ess EF Hex: E F F F F B 2 C FB Binary: 1110 1111 1111 1111 1111 1011 10 11 2C IA32 ess Hex: B F F F F 8 D 4 Binary: 1011 1111 1111 1111 1111 10 1101 01 Different compilers & machines assign different locations to objects Alpha P A0 FC 01 IA32 P D4 F8 BF Representing Floats Float F 15213.0; IA32/Alpha F B4 46 SPARC F IEEE Single Precision Floating Point Representation Hex: 4 6 6 D B 4 0 0 01 0110 0110 1101 1011 01 15213: 1110 1101 1011 01 46 B4 Not same as integer representation, but consistent across machines Can see some relation to integer representation, but not obvious F2-13 - Datorarkitektur 27 F2-14 - Datorarkitektur 27 Representing Strings Strings in C! Represented by array of characters! Each character encoded in ASCII format " Standard 7-bit encoding of character set " Other encodings exist, but uncommon " Character 0 has code 0x30» Digit i has code 0x30+i! String should be null-terminated " Final character 0 Compatibility! Byte ordering not an issue " Data are single byte quantities! Text files generally platform independent char S[6] "15213"; IA32/Alpha S 35 32 33 " Except for different conventions of line termination character(s)! SPARC S 35 32 33 F2-15 - Datorarkitektur 27 Machine-Level Code Representation Encode Program as Sequence of Instructions! Each simple operation " Arithmetic operation " Read or write memory " Conditional branch! Instructions encoded as bytes " Alpha s, SPARC s, Mac s use 4 byte instructions» Reduced Instruction Set Computer (RISC) " PC s use variable length instructions» Complex Instruction Set Computer (CISC)! Different instruction types and encodings for different machines " Most code not binary compatible Programs are Byte Sequences Too! F2-16 - Datorarkitektur 27

Representing Instructions int sum(int x, int y) { return x+y; }! For this example, Alpha & SPARC use two 4-byte instructions " Use differing numbers of instructions in other cases! IA32 uses 7 instructions with lengths 1, 2, and 3 bytes " Same for NT and for Linux " NT / Linux not fully binary compatible Alpha sum 30 42 01 80 FA 6B SPARC sum 81 C3 E0 08 90 02 09 Different machines use totally different instructions and encodings IA32 sum 55 89 E5 8B 45 0C 03 45 08 89 EC 5D C3 F2-17 - Datorarkitektur 27 Boolean Algebra Developed by George Boole in 19th Century! Algebraic representation of logic " Encode True as 1 and False as 0! Operations are ~ (not), & (and), (or), ^ (exclusive or, xor), (equal) The operations are defined by the following truth tabels: A ~A 0 1 1 0 A B A&B A B A^B AB 0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 F2-18 - Datorarkitektur 27 Rules for operations and & Properties of &, ^ (xor) and! Commutativity A B B A A & B B & A! Associativity (A B) C A (B C) (A & B) & C A & (B & C)! And distributes over or A & (B C) (A & B) (A & C)! Or distributes over and A (B & C) (A B) & (A C)! Identities A 0 A A 1 1 A & 0 0 A & 1 A! Cancellation of negation ~ (~A) A! Laws of complements A ~A 1 A & ~A 0! Absorption A (A & B) A A & (A B) A! Idempotency A A A A & A A! DeMorgan's Laws ~ (A & B) ~A ~B ~ (A B) ~A & ~B Property! Commutative A ^ B B ^ A! Associative (A ^ B) ^ C A ^ (B ^ C)! Distributive A & (B ^ C) (A & B) ^ (A & C)! 0 A ^ 0 A (A 0) ~A! 1 A ^ 1 ~A (A 1) A! Self A ^ A 0 (A A) 1! Exclusive or using or A ^ B (~A & B) (A & ~B)! A ^ B (A B) & ~(A & B)! ^ related to A ^ B ~(A B)! A ^ ~B ~A ^ B F2-19 - Datorarkitektur 27 F2-20 - Datorarkitektur 27

General Boolean Algebras Operate on Bit Vectors! Operations applied bitwise 011011 011011 & 01010101 01010101 0101 01111101 011011 ^ 01010101 1111 ~ 01010101 0101 01111101 1111 10101010 All of the Properties of Boolean Algebra Apply F2-21 - Datorarkitektur 27 Representing and Manipulating Sets Representation! Width w bit vector represents subsets of {0,, w 1}! a j 1 iff j A 011011 { 0, 3, 5, 6 } A 76543210 01010101 { 0, 2, 4, 6 } B 76543210 Operations! A&B Intersection 0101 { 0, 6 }! A B Union 01111101 { 0, 2, 3, 4, 5, 6 }! A^B Symmetric difference 1111 { 2, 3, 4, 5 }! A&~B Difference 1010 { 3, 5 }! ~B Complement 10101010 { 1, 3, 5, 7 } F2-22 - Datorarkitektur 27 Bit-Level Operations in C Operations &,, ~, ^ Available in C! Apply to any integral data type " long, int, short, char! View arguments as bit vectors! Arguments applied bit-wise Examples (char data type)! ~0x41 --> 0xBE ~0101 2 --> 10111110 2! ~0x --> 0x ~ 2 --> 11111111 2! 0x69 & 0x55 --> 0x41 011011 2 & 01010101 2 --> 0101 2! 0x69 0x55 --> 0x7D Contrast: Logic Operations in C Contrast to Logical Operators! &&,,! " View 0 as False " Anything nonzero as True " Always return 0 or 1 " Early termination Examples (char data type)!!0x41 --> 0x!!0x --> 0x01!!!0x41 --> 0x01! 0x69 && 0x55 --> 0x01! 0x69 0x55 --> 0x01! p && *p (avoids null pointer access) 011011 2 01010101 2 --> 01111101 2 F2-24 - Datorarkitektur 27 F2-23 - Datorarkitektur 27

Shift Operations Left Shift: x << y! Shift bit-vector x left y positions Right Shift: " Throw away extra bits on left " Fill with 0 s on right x >> y! Shift bit-vector x right y positions " Throw away extra bits on right! Logical shift " Fill with 0 s on left! Arithmetic shift " Replicate most significant bit on right " Useful with two s complement integer representation, see next lecture Argument x << 3 Log. >> 2 Arith. >> 2 Argument x << 3 Log. >> 2 Arith. >> 2 011010 01 0110 0110 101010 01 1010 111010 Cool Stuff with Xor! Bitwise Xor is form of addition! With extra property that every value is its own additive inverse A ^ A 0 Begin 1 2 3 End *x A A^B A^B (A^B)^A B B void funny(int *x, int *y) { *x *x ^ *y; /* #1 */ *y *x ^ *y; /* #2 */ *x *x ^ *y; /* #3 */ } *y B B (A^B)^B A A A F2-25 - Datorarkitektur 27 F2-26 - Datorarkitektur 27 Main Points It s All About Bits & Bytes! Numbers! Programs! Text Different Machines Follow Different Conventions! Word size! Byte ordering! Representations Boolean Algebra is Mathematical Basis! Basic form encodes false as 0, true as 1! General form like bit-level operations in C " Good for representing & manipulating sets F2-27 - Datorarkitektur 27