Comp Org & Assembly Lang

Slides:



Advertisements
Similar presentations
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
Advertisements

University of Washington CSE351 Announcements:  HW0, having fun?  Use discussion boards!  Sign up for mailing list  If you enrolled recently,
CSCE 212 Computer Organization Lecture 2 Data Representation.
Bits and Bytes 4/2/2008 Topics Physics, transistors, Moore’s law Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers.
Bits and Bytes CS 213 Aug. 27, 1998 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and.
Bits and Bytes January 17, 2002 Topics Why bits? Representing information as bits –Binary/Hexadecimal –Byte representations »numbers »characters and strings.
Bits and Bytes Topics Physics, transistors, Moore’s law Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters.
ICS 2005 Instructor: Peter A. Dinda TA: Bin Lin Recitation 2.
Data Representation Kieran Mathieson. Outline Digital constraints Data types Integer Real Character Boolean Memory address.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Bits and Bytes January 15, 2004 Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings.
Bits and Bytes Topics Why bits? Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
Fabián E. Bustamante, Spring 2007 Bits and Bytes Today Why bits? Binary/hexadecimal Byte representations Boolean algebra Expressing in C.
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
CS 270: Computer Organization Bits, Bytes, and Integers
Bits and Bytes Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C.
Lecture 3: Computation and Representation CS 2011 Fall 2014, Dr. Rozier.
Bits and Bytes Topics Why bits? Tell me Representing information as bits Binary/Hexadecimal Byte representations »numbers »characters and strings »Instructions.
Bits, Bytes, and Integers Topics Representing information as bits Bit-level manipulations Boolean algebra Expressing in C Representations of Integers Basic.
1 Saint Louis University Arithmetic and Bitwise Operations on Binary Data CSCI 224 / ECE 317: Computer Architecture Instructor: Prof. Jason Fritts Slides.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Bits and Bytes September 2, 2004 Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations »Numbers »Characters and strings.
– 1 – Number Systems Number of symbols = base of the system Most intuitive -- base 10 (decimal system) counting on fingertips symbols
Bits and Bytes Spring, 2015 Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations »Numbers »Characters and strings.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Bits and Bytes Boolean Algebra ( ) Boolean algebra Expressing in C.
Bits and Bytes Topics Why bits? Representing information as bits
Carnegie Mellon Bits, Bytes, and Integers (1-2) /18-243: Introduction to Computer Systems 2 nd Lecture, 19 May 2011 Instructors: Gregory Kesden.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Carnegie Mellon 1 Saint Louis University Data Representation in Memory CSCI 2400 / ECE 3217: Computer Architecture Instructor: David Ferry Slides adapted.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
The Machine Model Memory
Today’s Instructor: Randy Bryant
Data Representation Binary Numbers Binary Addition
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Instructor: David Ferry
EPSII 59:006 Spring 2004.
Representing Information
Comp Org & Assembly Lang
Announcements VM on the website! Speedometer!
Data III & Integers I CSE 351 Autumn 2016
LING 388: Computers and Language
Data III & Integers I CSE 351 Winter 2017
Introduction to Programming
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Bits and Bytes Topics Representing information as bits
Comp Org & Assembly Lang
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
“The Class That Gives CMU Its Zip!”
Memory, Data, & Addressing II CSE 351 Summer 2018
Computer Architecture
Bits and Bytes Topics Representing information as bits
Comp Org & Assembly Lang
Comp Org & Assembly Lang
Bits, Bytes, and Integers Part 2 3rd Lectures
Operations and Arithmetic
LING 388: Computers and Language
Review In last lecture, done with unsigned and signed number representation. Introduced how to represent real numbers in float format.
“The Class That Gives CMU Its Zip!”
“The Class That Gives CMU Its Zip!”
Lecture 2: Bits, Bytes, Ints
Presentation transcript:

Comp Org & Assembly Lang 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

How then do we represent data? Each different “type” in a programming language has its own representation. Next few slides discuss the representation for the common types: Ints Pointers Floats Strings

Representing Integers Linux/Win/OS X: little endian Solaris: big endian Decimal: 15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D int A = 15213; int B = -15213; long int C = 15213; 6D 3B 00 Linux/Alpha A 3B 6D 00 Sun A 6D 3B 00 Linux C 6D 3B 00 Alpha C 3B 6D 00 Sun C 00 93 C4 FF Linux/Alpha B C4 93 FF Sun B Two’s complement representation (Covered next lecture)

Representing Pointers Linux/Win/OS X: little endian Solaris: big endian Representing Pointers Alpha P A0 FC FF int B = -15213; int *P = &B; Alpha Address Hex: 1 F F F F F C A 0 Binary: 0001 1111 1111 1111 1111 1111 1100 1010 0000 01 00 Sun P Sun Address Hex: E F F F F B 2 C Binary: 1110 1111 1111 1111 1111 1011 0010 1100 FB 2C EF FF Linux P Linux Address Hex: B F F F F 8 D 4 Binary: 1011 1111 1111 1111 1111 1000 1101 0100 FF BF D4 F8 Different compilers & machines assign different locations to objects

Representing Floats Float F = 15213.0; 00 B4 6D 46 Linux/W in/OS X F Solaris F IEEE Single Precision Floating Point Representation Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 0000 15213: 1110 1101 1011 01 IEEE Single Precision Floating Point Representation Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 0000 15213 1110 1101 1011 01 (in binary) IEEE Single Precision Floating Point Representation Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 0000 15213: 1110 1101 1011 01 Not same as integer representation, but consistent across machines Can see some relation to integer representation, but not obvious

Representing Strings Strings in C Compatibility char S[6] = "15213"; Represented by array of characters Each character encoded in ASCII format Standard 7-bit encoding of character set 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 Text files generally platform independent Except for different conventions of line termination character(s)! Unix (‘\n’ = 0x0a = ^J) Mac (‘\r’ = 0x0d = ^M) DOS and HTTP (‘\r\n’ = 0x0d0a = ^M^J) Linux/Win/ OS X S Solaris S 32 31 35 33 00 32 31 35 33 00

ASCII Chart ASCII ‘0’ is 0x30 Reference: http://jimprice.com/jim-asc.htm

Characters Other popular encoding standards: Unicode ISO Latin-8 16 bit code Allows more characters: most languages can be encoded See http://en.wikipedia.org/wiki/Unicode The most commonly used encodings are UTF-8, UTF-16 and the now-obsolete UCS-2 UTF-8 uses one byte for any ASCII character, These bytes have the same code values in both UTF-8 and ASCII encoding, and up to four bytes for other characters ISO Latin-8 8-bit encoding standard with dotted consonants International standard for Latin letters

Machine-Level Code Representation Encode Program as Sequence of Instructions Each instruction is a simple operation Arithmetic operation Read or write memory Conditional branch Instructions encoded as bytes Alpha’s, Sun’s, Mac’s use 4 byte instructions Reduced Instruction Set Computer (RISC) PC’s (and intel Mac’s) use variable length instructions Complex Instruction Set Computer (CISC) Different instruction types and encodings for different machines Most code is not binary compatible Programs are Byte Sequences Too!

Representing Instructions int sum(int x, int y) { return x+y; } 00 30 42 Alpha sum 01 80 FA 6B E0 08 81 C3 Solaris sum 90 02 00 09 E5 8B 55 89 PC sum 45 0C 03 08 EC 5D C3 For this example, Alpha & Sun use two 4-byte instructions Use differing numbers of instructions in other cases PC uses 7 instructions with lengths 1, 2, and 3 bytes Same for NT, OSX (intel) and for Linux NT / Linux/OSX not fully binary compatible Different processors use totally different instructions and encodings

What do we know now? A computer is a processor and RAM. Everything else is peripheral Everything is 1’s and 0’s. A processor executes instructions Instructions are encoded in 1’s and 0’s each instruction has binary representation RAM stores 1’s and 0’s We interpret these 1’s and 0’s based on context Every type is interpreted from binary integers: binary float: special representation in binary char: binary (ASCII encoding)

How do we get to bits? We write programs in an abstract language like C How do we get to the binary representation? Compilers & Assemblers! We write x = 5. The compiler changes it into mov 5, 0x005F The assembler changes it into: 80483c7: 8b 45 5F

Manipulating bits Since all information is in the form of bits, we must manipulate bits when programming at low levels We did some of this with the show_bytes program. To understand how to do more, we must understand Boolean algebra We can manipulate bits from C; this is where C and hardware meet.

Boolean Algebra Developed by George Boole in 19th Century And Or Not Algebraic representation of logic Encode “True” as 1 and “False” as 0 And A&B = 1 when both A=1 and B=1 Or A|B = 1 when either A=1 or B=1 Not ~A = 1 when A=0 Exclusive-Or (Xor) A^B = 1 when either A=1 or B=1, but not both

Application of Boolean Algebra Applied to Digital Systems by Claude Shannon 1937 MIT Master’s Thesis Reason about networks of relay switches Encode closed switch as 1, open switch as 0 A&~B Connection when A&~B | ~A&B A ~A ~B B ~A&B = A^B

Representing & Manipulating Sets Representation Width w bit vector represents subsets of {0, …, w–1} aj = 1 if j  A 01101001 { 0, 3, 5, 6 } 76543210 01010101 { 0, 2, 4, 6 } Operations & Intersection 01000001 { 0, 6 } | Union 01111101 { 0, 2, 3, 4, 5, 6 } ^ Symmetric difference 00111100 { 2, 3, 4, 5 } ~ Complement 10101010 { 1, 3, 5, 7 }

General Boolean Algebras Operate on Bit Vectors Operations applied bitwise Let x = 105 and y = 85 (think about how this is actually stored) x & y: x | y: x ^ y ~y All of the Properties of Boolean Algebra Apply 01101001 & 01010101 01000001 01101001 | 01010101 01111101 01101001 ^ 01010101 00111100 ~ 01010101 10101010 01000001 01111101 00111100 10101010

Bit-Level Operations in C Operations &, |, ~, ^ Available in C Apply to any “integral” data type long, int, short, char, unsigned View arguments as bit vectors Arguments applied bit-wise Examples (Char data type) ~0x41 --> 0xBE ~010000012 --> 101111102 ~0x00 --> 0xFF ~000000002 --> 111111112 0x69 & 0x55 --> 0x41 011010012 & 010101012 --> 010000012 0x69 | 0x55 --> 0x7D 011010012 | 010101012 --> 011111012

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) c = ‘A’ then !c is !0x41 --> 0x00 c = ‘\0’ then !c is !0x00 --> 0x01 c = ‘A’ the !!c is !!0x41 --> 0x01 0x69 && 0x55 --> 0x01 0x69 || 0x55 --> 0x01 p && *p++ (avoids null pointer access) Or p && (*p = 10) also protects Works because of short-circuit evaluation in C