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 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 X (1.5213e4)

3 Why Don’t Computers Use Base 10?
Implementing Electronically Hard to store ENIAC (First electronic computer) used 10 vacuum tubes / digit IBM 650 used 5+2 bits (1958, successor to IBM’s Personal Automatic Computer, PAC from 1956) Hard to transmit Need high precision to encode 10 signal levels on single wire Messy to implement digital logic functions Addition, multiplication, etc.

4 Binary Representations
Base 2 Number Representation Represent as Represent as [0011]…2 Represent X 104 as X 213 Electronic Implementation Easy to store with bistable elements Reliably transmitted on noisy and inaccurate wires 0.0V 0.5V 2.8V 3.3V 1

5 Encoding Byte Values Byte = 8 bits Binary 000000002 to 111111112
Decimal: to First digit must not be 0 in C Octal: to Use leading 0 in C Hexadecimal to FF16 Base 16 number representation Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ Use leading 0x in C, write FA1D37B16 as 0xFA1D37B Or 0xfa1d37b 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 Hex Decimal Binary

6 Bases Every number system uses a base Decimal numbers use base 10
Other common bases used in computer science: Octal (base 8) Hexadecimal (base 16) Binary (base 2) Do examples of base 10, base 8 numbers, base 16 Show how to translate base 8 to base 10, base 16 to base 10

7 Decimal x x x 100

8 Octal x x x 80

9 Counting in octal

10 Converting Converting octal to decimal:
753 = 7 x x x 80 = = 491

11 Translating to octal writing 157 in base 8
remainder: 2 x x x 80 = = 157 Divisor

12 Hexadecimal x x x 160

13 Hexadecimal A B C D E F A 1B 1C 1D 1E 1F A 2B 2C 2D 2E 2F A 3B 3C 3D 3E 3F 40 41 A 4B 4C 4D 4E 4F 50 A 5B 5C 5D 5E 5F A 6B 6C 6D 6E 6F A 7B 7C 7D 7E 7F A 8B 8C 8D 8E 8F A 10B 10C 10D 10E 10F 120 …

14 Translating to hex 365 remainder: 16 22 D 1 6 0 1 1 6 D
1 6 D 1 x x D x 160 = = 365

15 Binary x x x 20

16 Counting in binary

17 Translating to binary 4 remainder: 1 0 0 1 Divisor

18 Translating to binary remainder: 2 1 1 0 0 1

19 Literary Hex Common 8-byte hex filler: 0xdeadbeef
Can you think of other 8-byte fillers?

20 Relation between hex and binary
Hexadecimal: 16 characters, Each represents a number between 0 and 15 Binary: Consider 4 places Represent numbers between 0000 and 1111 Or between 0 and 15 Result: 1 hex digit represents 4 binary digits

21 Relation between hex and binary
Decimal 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111 16

22 Converting main() { octal_print(0); /* result: 00 */
octal_print( ); /* result: since 0 at */ /* beginning indicates octal num */ octal_print( ); /* result: */ octal_print(999); /* result: */ octal_print(-999); /* result: */ octal_print(1978); /* result: */ }

23 See /home/barr/Student/Examples/baseConversion/octal.c
Converting #define DIG 20 void octal_print(int x) { int i = 0, od; char s[DIG]; /* up to DIG octal digits */ if ( x < 0 ) /* treat negative x */ putchar ('-'); /* output a minus sign */ x = - x; } do od = x % 8; /* next higher octal digit */ s[i++] = (od + '0'); /* octal digits in char form */ } while ( (x = x/8) > 0); /* quotient by integer division */ putchar('0'); /* octal number prefix */ putchar(s[--i]); /* output characters on s s*/ } while ( i > 0 ); putchar ('\n'); See /home/barr/Student/Examples/baseConversion/octal.c

24 Memory Contents Address What is memory (RAM)?
To the processor, it’s just a big array of cells Each cell has an address Each cell contains a specific value 15213 “John” Sunday ‘a’ 1 2 3 4 Address Contents

25 Memory Contents Address
Of course, addresses and content are actually in binary Memory is byte addressable i.e., every byte has an address. Address 1 000 001 010 011 100 Contents

26 Memory Contents Address
When we look at memory in a debugger (like gdb), usually use hex notation Address 9B 39 C3 5F 09 000 001 002 003 004 Contents

27 Memory What we see (gdb) x/20b 0x7fffffffe45c
0x7fffffffe45c: 0x5 0x0 0x0 0x0 0x1 0x0 0x0 0x0 0x7fffffffe464: 0x0 0x0 0x0 0x0 0xffffffffffffffdd 0x5 0x40 0x0 0x7fffffffe46c: 0x0 0x0 0x0 0x0

28 32-bit machine means 32 wires on each bus
Addressing Memory CPU must send an address to RAM how many bits in the address? depends on how many wires there are! 32-bit machine means 32 wires on each bus

29 Memory size limited by number of bits in address: 3 bits so 8 cells
Contents 000 001 010 011 100 101 110 111 How do we talk about the size of memory? in terms of powers of 2 (addresses are binary) Bus must have 3 wires Memory size limited by number of bits in address: 3 bits so 8 cells

30 Range An address has a fixed number of bits. The range is the numbers that will fit in that many bits. Examples: = 0 = 7 range is 0 to 7 = 0 = 15 range is 0 to 15

31 Range In general range is 0 to 2n – 1 where there are n bits in the representation Intuition: 111 = 1 x x x 20 Which is 1 less than 1000 = 1 x x x x 20 Number of numbers represented: n bits can represent 2n different numbers i.e., from 0 to 2n – 1

32 Size In general Size is 2n where there are n bits in the representation Intuition: Range of 3 bits: = 0 = 7 range is 0 to 7 number of numbers is 8 = 23 or 1 more than the largest number! Number of numbers represented: n bits can represent 2n different numbers i.e., from 0 to 2n – 1

33 Memory How do we talk about the size of memory?
in terms of powers of 2 (addresses are binary) based on the number of bits in the address 20 1 21 2 22 4 23 8 24 16 25 32 26 64 27 128 28 256 29 512 210 1024 211 2048 212 4096 213 8192 214 16384 210 x 210 = 220 1,048,576 210 x 210 x 210 = 230 1,073,741,824 210 x 210 x 210 x 210 = 240 1,099,511,627,776

34 Memory How do we talk about the size of memory? 1 bit 8 bits 1 byte
in bytes (how may bytes of memory do you have?) in terms of powers of 2 (addresses are binary) 1 bit 8 bits 1 byte 210 bytes 1024 bytes 210 x 210 = 220 1,048,576 bytes 210 x 210 x 210 = 230 1,073,741,824 bytes 210 x 210 x 210 x 210 = 240 1,099,511,627,776 bytes 1 bit 1 byte 1 Kilobyte (Kb) 1 Megabyte (Mb) 1 Gigabyte (Gb) 1 Terabyte (Tb)

35 Byte-Oriented Memory Organization
Programs Refer to Virtual Addresses 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 is private to particular “process” Program being executed Program can clobber its own data, but not that of others

36 Byte-Oriented Memory Organization
Virtual memory is organized by the OS VM is implemented by the Compiler + Run-Time System Control Allocation (or process manager) Where different program objects should be stored Multiple mechanisms: static, stack, and heap In any case, all allocation within single virtual address space

37 Machine Words Machine (or processor) Has “Word Size”
Nominal size of integer-valued data Including addresses Last generation machines used 32 bits (4 bytes) words Limits addresses to 4GB Becoming too small for memory-intensive applications We’ll use 32 bits as the word size in this class Most current systems use 64 bits (8 bytes) words Potential address space = 264  1.8 X 1019 bytes Machines support multiple data formats Fractions or multiples of word size Always integral number of bytes

38 Word-Oriented Memory Organization
32-bit Words 64-bit Words Bytes Addr. 0000 Addr = ?? 0001 Addresses Specify Byte Locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) 0002 0000 Addr = ?? 0003 0004 0000 Addr = ?? 0005 0006 0004 0007 0008 Addr = ?? 0009 0010 0008 Addr = ?? 0011 0008 0012 Addr = ?? 0013 0014 0012 0015

39 Data Representations Sizes of C Objects (in Bytes)
C Data Type Alpha (RIP) Typical 32-bit Intel IA32 unsigned 4 4 4 int 4 4 4 long int 8 4 4 char 1 1 1 short 2 2 2 float 4 4 4 double 8 8 8 long double 8/16† 8 10/12 char * 8 4 4 Or any other pointer (†: Depends on compiler&OS, 128bit FP is done in software)

40 Byte Ordering How should bytes within a multi-byte word be ordered in memory? Conventions Sun’s, PowerPC Mac’s are “Big Endian” machines Least significant byte has highest address Alphas, intel machines (including PC’s and Mac’s) are “Little Endian” machines Least significant byte has lowest address Most networking hardware is “Big-Endian” See for a history of “endian-ness” Aside: “endian” is from Gulliver’s Travels , a book written by Jonathan Swift, in 1726.

41 Endian is a function of hardware
Byte Ordering Example Big Endian Least significant byte (e.g., 67) has highest address Little Endian Least significant byte has lowest address Example Variable x has 4-byte representation 0x Address given by &x is 0x100 Endian is a function of hardware Big Endian 0x100 0x101 0x102 0x103 01 23 45 67 01 23 45 67 Little Endian 0x100 0x101 0x102 0x103 67 45 23 01 67 45 23 01

42 Why does endian-ness matter?
What is Lady Gaga’s age? Web Server (solaris box) Web Client (browser) 28 (intel box) 0x C 469,762,048 Displayed as 0x 1C Interpreted as 0x C

43 Why does endian-ness matter?
Your machine (solaris box) Your client’s machine (intel box) Spreadsheet created on big-endian machine Spreadsheet read on big-endian machine Final cost: $ 28 Final cost: $ 469,762,048 Stored as Displayed as 0x C 0x 1C Interpreted as 0x C

44 Reading Byte-Reversed Listings
Disassembly Text representation of binary machine code Generated by program that reads the machine code Example Fragment Address Instruction Code Assembly Rendition : 5b pop %ebx : 81 c3 ab add $0x12ab,%ebx 804836c: 83 bb cmpl $0x0,0x28(%ebx) Deciphering Numbers Value: 0x12ab Pad to 4 bytes: 0x000012ab Split into bytes: ab Reverse: ab

45 Examining Data Representations
Code to Print Byte Representation of Data Casting pointer to unsigned char * creates byte array typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int len) { int i; for (i = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } This program is described in section of the textbook Don’t need the 0x for pointers in some compilers Printf directives: %p: Print pointer %x: Print Hexadecimal %.2x means print only 2 digits of hex See /home/barr/Student/Examples/showBytes.c

46 show_bytes Execution Example
int a = 15213; printf("int a = %d;\n”, a); show_bytes((byte_pointer) &a, sizeof(int)); Real representation: 15213 B D Result (Linux): int a = 15213; 0x11ffffcb8 0x6d 0x11ffffcb9 0x3b 0x11ffffcba 0x00 0x11ffffcbb 0x00 Address of bytes Content stored at the address

47 show_bytes Execution Example
int a = 15213; printf("int a = %d;\n”, a); show_bytes((byte_pointer) &a, sizeof(int)); float b = ; printf(”float b = %f;\n”, b); show_bytes((byte_pointer) &b, sizeof(float)); char c[10] = “abcd”; printf(”string c = %s;\n”, c); show_bytes((byte_pointer) &c, 10*sizeof(char));

48 Summary of the Main Points
It’s All About Bits & Bytes Numbers Programs Text Different Machines Follow Different Conventions for Word size Byte ordering Representations Boolean Algebra is the Mathematical Basis Basic form encodes “false” as 0, “true” as 1 General form like bit-level operations in C Good for representing & manipulating sets


Download ppt "Comp Org & Assembly Lang"

Similar presentations


Ads by Google