How a computer runs a program. Binary Representa8on and Opera8ons on Binary Data. Opera8ng System 9/17/13. You can view binary file contents

Similar documents
CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

Binary Representations and Arithmetic

Sign & Magnitude vs 2 s Complement ( Signed Integer Representa:ons)

Lecture 2: Number Representa2on

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

More about Binary 9/6/2016

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

UNIT IV: DATA PATH DESIGN

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

Main Memory Organization

Bits, Bytes, and Integers

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Digital Data

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

Outline. Integer representa+on and opera+ons Bit opera+ons Floa+ng point numbers. Reading Assignment:

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

COMP2611: Computer Organization. Data Representation

Inf2C - Computer Systems Lecture 2 Data Representation

Floa=ng- Point Numbers

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Data Representa+on in Memory

Representa7on of integers: unsigned and signed Conversion, cas7ng Expanding, trunca7ng Addi7on, nega7on, mul7plica7on, shiaing

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

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

Writing a Fraction Class

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

CS101 Lecture 04: Binary Arithmetic

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

CS 101: Computer Programming and Utilization

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

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

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

Chapter 3: Arithmetic for Computers

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Number systems & Bit opera1ons. h#p://xkcd.com/99/

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

CS 261 Fall Mike Lam, Professor Integer Encodings

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

Number System. Introduction. Decimal Numbers

Integer Representation

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

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

MACHINE LEVEL REPRESENTATION OF DATA

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

CSCI 402: Computer Architectures

Bits, Words, and Integers

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

EE292: Fundamentals of ECE

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

COMP Overview of Tutorial #2

Arithmetic and Bitwise Operations on Binary Data

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

Signed Multiplication Multiply the positives Negate result if signs of operand are different

EE 109 Unit 6 Binary Arithmetic

Chapter 2 Bits, Data Types, and Operations

Final Labs and Tutors

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

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

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

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

DEPARTMENT OF MATHS, MJ COLLEGE

Data Types. Data Types. Integer Types. Signed Integers

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Microcomputers. Outline. Number Systems and Digital Logic Review

Binary representation of integer numbers Operations on bits

Lec-6-HW-3-ALUarithmetic-SOLN

Chapter 10 Binary Arithmetics

UNIT V: CENTRAL PROCESSING UNIT

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

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

Chapter 2 Bits, Data Types, and Operations

Instruc=on Set Architecture

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

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

SIGNED AND UNSIGNED SYSTEMS

Chapter 2 Bits, Data Types, and Operations

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

World Inside a Computer is Binary

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

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

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

Computer System and programming in C

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

COMP 2001/2401 Test #1 [out of 80 marks]

Lecture 03 Bits, Bytes and Data Types

Data Representa+on: Floa+ng Point Numbers

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

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

Informatics Ingeniería en Electrónica y Automática Industrial

Computer Organisation CS303

Transcription:

Consider the following type defini8on and variable declara8ons: struct persont { struct persont p1; char name[32]; int age; struct persont people[40] float heart_rate; }; (1) What type is each of the following expressions? p1 p1.name people p1.heart_rate people.name people[0] people[0].name people[0].name[3] (2) Show the C staments to set the 3rd person s age to 18, heart_rate to 66, and name to Ralph void mystery(int x[], int y); int main() { int i, arr[5]; for(i=0; i < 5; i++) { arr[i] = i; } mystery(arr,4); for(i=0; i < 5; i++) { printf("arr[%d]=%d\n,i,arr[i]); } } /****************************/ void mystery(int x[], int y) { int i; for(i=0; i < y; i++) { if((x[i]%2) == 0) { x[i] = x[i] + 1; } } return; } // STACK: How a computer runs a program Binary Representa8on and Opera8ons on Binary Data Week 2, CS31 Fall 2013 Tia Newhall Program Opera+ng System Computer Hardware Compiler: translates C source to binary executable file (OS/Arch specific) foo.c - - - > gcc - - - - > a.out a.out: binary executable: all 0 s and 1 s Instruc8ons ( e.g. add the value of x and 6) Some data ( e.g. 6, maybe ini8al value of x ) Informa8on so that OS can load and start running./a.out: CPU instr Opera8ng System 3 RAM Process: instrs data stack a.out Disk 1. Loads a.out from Disk into RAM 2. Creates a Process: running program w/own address space: (like an array of addressable contents: data, instruc8ons) 3. Ini8alizes the CPU to start running 1 st instruc8on 2 1 bus You can view binary file contents xxd (or hexdump C) to veiw binary file values: xxd a.out # a binary executable file Address: value of the next 16 bytes in memory 0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 0000010: 0200 3e00 0100 0000 3007 4000 0000 0000 0000020: 4000 0000 0000 0000 084d 0000 0000 0000 (these weird numbers (f,c,e, ), are hexidecimal digits ) xxd myprog.c # binary ascii encoding of C source: 0000000: 2369 6e63 6c75 6465 3c73 7464 696f 2e68 #i nc lu de <s td io.h 0000010: 3e0a 696e 7420 6d61 696e 2829 207b 0a20 >\n in t ma in () { \n 1

This Week: Binary Representa8on of different data types: 6, - 4.6, a bit, byte, word signed and unsigned How opera8ons on binary data work 6 + 12, 15 5, - 9 + 12,.. Opera8ons on bits Logical vs. bit- wise operators Bits and Bytes Bit: a 0 or 1 values HW represents as two different voltages 1: the presence of voltage (high voltage) 0: the absence of voltage (low voltage) Byte: 8 bits, the smallest addressable unit 0: 01010101 1: 10101010 2: 00001111 Word: some number of bytes, depends on architecture (4 bytes is common) How many values? The number of bits determines the range of values 2 values with 1 bit 4 values with 2 bits 8 values with 3 bits 16 values with 4 bits 2^n values with n bits 1 bit: 0 1 / \ / \ 2: 00 01 10 11 / \ / \ / \ / \ 3: 000 001 010 011 100 101 110 111 / \ / \ / \ / \ / \ / \ / \ / \ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 C types and their sizes 1 byte: char, unsigned char 2 bytes: short, unsigned short 4 bytes: int, unsigned int, float 8 bytes: long long, unsigned long long, double 4 or 8 bytes: long, unsigned long unsigned long v1; short s2; unsigned int u1; long long ll; double d1; printf( %lu %u %d %lld %g\n, v1, u1, s2, ll, d1); printf( %lu %lu %lu\n, sizeof(v1), sizeof(s2), sizeof(ll)); // prints out number of bytes Unsigned numbers With N bits, can represent values: 0 to 2 n - 1 4 bits: 0000 0 0001 1 = 1*2 0 0010 2 = 1*2 1 0011 3 = 1*2 1 + 1*2 0 = 2 + 1 0100 4 = 1*2 2... 1111 15 = 1*2 3 + 1*2 2 + 1*2 1 + 1*2 0 = 8 + 4 + 2 + 1 Conver8ng binary to decimal: low order, 0 th bit, counts the number of 2 0 (0 or 1) 1 st bit is number of 2 1 (0 or 1) 2 nd bit is number of 2 2 s Binary: base 2 numbers Decimal, base 10, digits {0,1,2,, 9}: 1703: 1*10 3 + 7*10 2 + 0*10 1 + 3*10 0 = 1000 + 700 + 0 + 3 = 1703 Binary, base 2, digits {0,1}: 10101: 1*2 4 + 0*2 3 + 1*2 2 + 0*2 1 + 1*2 0 = 16 + 0 + 4 + 0 + 1 = 21 Conver8ng binary to decimal: just follow this papern 2

Try unsigned char ch = m ; in binary, ch s value is: 01101101 Convert to decimal (leave as expression): ch+1 (add in binary, then convert to decimal): Adding Binary Values Add Corresponding digits to get either 0 or 1 with a possible carry bit to next place Example adding two 4- bit values (result is 4- bits): 1 0110 6 1100 12 + 0100 + 4 + 1010 +10 1010 10 1 0110 6 ^carry out bit Unsigned overflow: result requires more bits than have Represen8ng Signed Integers int, short, char, long, long long Use 2 s complement encoding High- order bit is sign bit (0:posi8ve, 1: nega8ve) 1xxxxxxx : some nega8ve value 0xxxxxxx: some posi8ve value Posi8ve 2 s compliment encodings are same as their unsigned encodings 0000 is zero signed and unsigned 0110 is six signed and unsigned With N bits, can represent: - 2 N- 1 to 2 N- 1-1 4 bit value can represent: - 8, - 7,, - 1, 0, 1,, 7 2 s Complement 2 s complement of N bit value x is: 2 N x 4- bit value: 0010 (2) its 2 s complement is: 2 4 2 10 borrow bit 10000 011^0 2 4-0010 - 0010-2 1110 1110-2 (borrow minus 1:10-1=1) (there is a much easier way to negate and to subtract) 2 s Complement to Decimal High order bit is the sign bit, otherwise just like unsigned conversion. 4- bit examples: 0110: 0*-2 3 + 1*2 2 + 1*2 1 + 0*2 0 0 + 4 + 2 + 0 = 6 1110: 1*-2 3 + 1*2 2 + 1*2 1 + 0*2 0 Try: 1010 1111-8 + 4 + 2 + 0 = -2 2 s Complement Nega8on Flip the bits and then add 1 (~x + 1): 6: 0110: 1001-3: 1101: 0010 + 0001 +0001 1010 0011 = -8+2 = -6 = 2+1 = 3 Try: negate 1 negate 7 3

2 s Complement Subtrac8on Negate and add: much easier than borrowing 6 3 == 6 + ~3 + 1 6 0110 0110-3 - 0011 1100 + 0001 1 0011 = 2 + 1 = 3 ^ what about carry out bit? It looks like overflow, but the result works out fine if we ignore the carry- out bit (0011 is the correct result) **we can also do unsigned subtrac8on in this way Subtrac8on Negate and add 1 to second operand: Can use the same circuit for add and subtract: 6-7 == 6 + ~7 + 1 input 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > input 2 - - > possible bit flipper - - > ADD CIRCUIT - - - > result possible +1 input- - - - - - - - > Arithme8c Opera8on Overflow Overflow: ~running out of enough bits to store result Signed addi8on (and subtrac8on): 2+-1=1 2+-2=0 2+-4=-2 2+7=-7-2+-7=7 0010 0010 0010 0010 1110 +1111 +1110 +1100 +0111 +1001 1 0001 1 0000 1110 1001 1 0111 0 1 2 7-8 - 7-2 - 1 0000 0001 0010 0111 1000 1001 1110 1111 add pos - - - - - - - - - > <- - - - - - - add neg <- - - - - - - - - add neg add pos - - - - - - > 0 1 2 7-8 - 7-1 0000 0001 0010 0111 1000 1001 1111 ^ overflow when opera8on crosses here Try Out: Signed Overflow Rules? 4 bit signed values ( a- b is a + ~b + 1 ): carry-in carry-out 3 + 7 = 0011 + 0111 = 0 1010 = -6-3 + -6 = 1101 + 1010 = 1 0111 = 7-3 + 6 = 1101 + 0110 = 1 0011 = 3 3-6 = 0011 + 1001 + 1 = 0 1101 = -3 3 + -6 = 0011 + 1010 = 0 1101 = -3 Rule for detec8ng overflow in signed arithme8c? is the carry- out bit meaningful? if values are different signs, can we ever get overflow? Arithme8c Opera8on Overflow Overflow: ~running out of enough bits to store result Unsigned addi8on (and subtrac8on): 2+1=3 2-1=1 2+14=0 2-3=15 0010 0010 0010 0010 +0001 +1111 +1110 1100 0011 1 0001 1 0000 +0001 1111 0 1 2 7 8 9 15 0000 0001 0010 0111 1000 1001 1111 add - - - - - - - - - > <- - - - - - - - - - sub ^ subtrac8on overflow addi8on overflow ^ Try Out: Unsigned Overflow Rules? 4 bit unsigned values (a b is a + ~b +1, a+b is a+b+0): carry-in carry-out 9 + 11 = 1001 + 1011 + 0 = 1 0100 = 4 9 + 6 = 1001 + 0110 + 0 = 0 1111 = 15 3 + 6 = 0011 + 0110 + 0 = 0 1001 = 9 6-3 = 0110 + 1100 + 1 = 1 0011 = 3 3-6 = 0011 + 1001 + 1 = 0 1101 = 13 Rule for detec8ng overflow? is the carry- out bit meaningful? When? 4

Overflow Rules Signed: can only occur when adding two values of the same sign: When sign bits of operands are the same, but the sign bit of result is different Try out some 4- bit examples: (1) signed result? (2) unsigned result? (3)overflow? 0110 + 0010 1001 1010 1001 + 1110 Unsigned: can occur when adding or when subtrac8ng larger from smaller: When carry- in bit is different than carry- out bit C in C out C in XOR C out 0 0 0 0 1 1 1 0 1 1 1 0 During Execu8on what Happens if Overflow? HW: sets flags as side- effect of arithme8c computa8ons, these can be tested for error condi8ons OF: overflow flag: set based on signed overflow CF: set if carry- out is 1, can be used to test for unsigned overflow with carry- in bit What does C do? Nothing: unsigned char s = 255; s = s + 4; // 3, maybe that is what you want? Sign Extension When combining signed values of different num bytes, expanded smaller to equivalent larger size: char y=2, x=-13; short z = 10; z = z + y; z = z + x; 0000000000001010 0000000000000101 00000010 11110011 0000000000000010 1111111111110011 Fill in high- order bits with sign- bit value to get same numeric value in larger number of bytes Let s verify that this works 4- bit signed value, sign extend to 8- bits, is it the same value? 0111 - - - > 0000 0111 obviously s8ll 7 1010 - - - - > 1111 1010 is this s8ll - 6? - 128 + 64 + 32 + 16 + 8 + 0 + 2 + 0 = - 6 yes Different Number Representa8ons Binary: base 2 digits {0,1} Decimal: base 10 digits {0, 1,, 9} Hexidecimal: base 16 digits {0,,9,a,b,c,d,e,f} 2 4 is 16, so 4 binary digits to represent 1 hex: 0101:5, 1100:c Binary to hex: group into 4 bin digits, convert each group: 0011 1010 1100 0101 0011101011000101 3 a c 5 = 0x3ac5 Hex to binary: expand each hex digit into its 4 binary digits: a 1 2 f 0xa12f 1010 0001 0010 1111 = 1010000100101111 hex is easier to read than binary: 0x3efa vs. 0011111011111010 5

Decimal to Binary (or to hex) 543876 in binary? - 34252 in binary? if D nega8ve: convert the posi8ve to b, then negate (~b +1) if D posi8ve: need to find 0 and 1 digits for a 7 - a 0 such that: a 7 *2 7 + a 6 *2 6 + a 5 *2 5 + a 4 *2 4 + a 3 *2 3 + a 2 *2 2 + a 1 *2 1 + a 0 *2 0 = D idea: build up binary value from low to high- order bit if the number D is odd then a 0 1, if even then a 0 is 0 consider the next bit a 1 : its value determined by whether or not D/2 is odd... con8nue un8l D/2/2 /2 is zero Algorithm: decimal value D, binary result b (b i is ith digit): i = 0 while (D > 0) if D is odd set b i to 1 if D is even set b i to 0 i++ D = D/2 idea: example: D = 105 a0 = 1 D = b D = 52 a1 = 0 D/2 = b/2 D = 26 a2 = 0 D/2 = b/2 D = 13 a3 = 1 D/2 = b/2 D = 6 a4 = 0 D/2 = b/2 D = 3 a5 = 1 0 = 0 D = 1 a6 = 1 D = 0 a7 = 0 105 = 01101001 Try 74 Try - 115 Opera8ons on Bits Bit- wise operators: bit operands, bit result & (AND) (OR) ~(NOT) ^(XOR) A B A & B A B ~A A ^ B 0 0 0 0 1 0 0 1 0 1 1 1 1 0 0 1 0 1 1 1 1 1 0 0 01010101 01101010 10101010 ~10101111 00100001 & 10111011 ^ 01101001 01010000 01110101 00101010 11000011 More Opera8ons on Bits Bit- shi operators: << le shi, >> right shi 01010101 << 2 is 01010100 2 high-order bits shifted out 2 low-order bits filled with 0 01101010 << 4 is 10100000 01010101 >> 2 is 00010101 01101010 >> 4 is 00000110 10101100 >> 2 is 00101011 (logical shift) or 11101011 (arithmetic shift) Arithme8c right shi : fills high- order bits w/sign bit How Used? Bit vectors: encode yes/no values in individual bits: (ex) file permissions: ls l directory owner group world d rwx rwx rwx -rw------- foo.c -rwx------ a.out* drwx------ cs31/ Encode in 10 bits: (need a short variable, 2 bytes, to store): - r w r w r w - 0 1 1 0 1 1 0 1 1 0 permission: 666 chmod 620 foo.c: 0 1 1 0 0 1 0 0 0 0 6 2 0 6

Try using bit operators short f = 281; // 0000 0001 0001 1001 (1) C code to see if file is readable by group? drwxrwxrwx 000000 0 100 011 001 (this value) (2) C code to set perms. so that owner can write? prin} to print diff types and reps: %x: hex %u: unsigned %ld: long signed %llu: unsigned long long printf( %c %d %x, a, a, a ); printf( %d %x, 1234, 1234); Floa8ng Point Representa8on 1 bit for sign sign exponent frac8on 8 bits for exponent 23 bits for precision value = (- 1) sign + 1.frac8on * 2 (exponent- 127) let's just plug in some values and try it out 0xc080015a: 1 10000001 00000000000000101011010 sign = 1 exp = 129 fraction = 346 = -1 + 1.346*2 2 = 5.384 I don t expect you know how to do this Summary Know how binary data represented and manipulated: Different sizes depend on C type: 1 byte, 2 bytes, 4 bytes, 8 bytes Unsigned and Signed Representa8ons Arithme8c opera8ons: + and Same rules for performing signed & unsigned ops Different rules for determining if result overflowed Bit- wise opera8ons: &,, ^, ~, <<, >> Different representa8ons: hex, binary, decimal Conver8ng values between these 7