Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp Org & Assembly Lang

Similar presentations


Presentation on theme: "Comp Org & Assembly Lang"— Presentation transcript:

1 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

2 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

3 Representing Integers
Linux/Win/OS X: little endian Solaris: big endian Decimal: 15213 Binary: Hex: B D int A = 15213; int B = ; 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)

4 Representing Pointers
Linux/Win/OS X: little endian Solaris: big endian Representing Pointers Alpha P A0 FC FF int B = ; int *P = &B; Alpha Address Hex: F F F F F C A 0 Binary: 01 00 Sun P Sun Address Hex: E F F F F B C Binary: FB 2C EF FF Linux P Linux Address Hex: B F F F F D Binary: FF BF D4 F8 Different compilers & machines assign different locations to objects

5 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: D B Binary: 15213: IEEE Single Precision Floating Point Representation Hex: D B Binary: (in binary) IEEE Single Precision Floating Point Representation Hex: D B Binary: 15213: Not same as integer representation, but consistent across machines Can see some relation to integer representation, but not obvious

6 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

7 ASCII Chart ASCII ‘0’ is 0x30 Reference:

8 Characters Other popular encoding standards: Unicode ISO Latin-8
16 bit code Allows more characters: most languages can be encoded See 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

9 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!

10 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

11 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)

12 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

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

14 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

15 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

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

17 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 & | ^ ~

18 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 ~ > ~0x00 --> 0xFF ~ > 0x69 & 0x55 --> 0x41 & > 0x69 | 0x55 --> 0x7D | >

19 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


Download ppt "Comp Org & Assembly Lang"

Similar presentations


Ads by Google