321
play

321 (in decimal) Data Representation 100 10 1 How did we get - PDF document

1/26/2017 321 (in decimal) Data Representation 100 10 1 How did we get these? 10 2 10 1 10 0 Base-10 Positions denote powers of 10 3*100 + 2*10 + 1*1 Digits 0-9 denote position values 101000001 101000001 How did we 256 128 64


  1. 1/26/2017 321 (in decimal) Data Representation 100 10 1 How did we get these? 10 2 10 1 10 0 “Base-10” Positions denote powers of 10 3*100 + 2*10 + 1*1 Digits 0-9 denote position values 101000001 101000001 How did we 256 128 64 32 16 8 4 2 1 get these? 2 7 2 6 2 8 2 5 2 4 2 3 2 2 2 1 2 0 “Base-2” 101 million and one? Actually, 321 in binary (Base-2) 256 +0+ 64 + 0 + 0 + 0 + 0 + 0 + 1 = 321 Why should we care? • Computers use binary (bits) to store all information Positions denote powers of 2 Digits 0 and 1 denote position values 1

  2. 1/26/2017 Binary group activity Find decimal values of binary numbers below 0x 141 16 8 4 2 1 x = 10011 y = 01001 141 in decimal? Actually, 321 in hexadecimal (Base-16) What are the decimal values of x–y x+y Why hexadecimal? Recall binary for 321 000101000001 What is the binary representation of x–y x+y 0001 0100 0001 1 4 1 0x141 Hexadecimal example 0x0CD 256 16 1 How did we get these? 0 0 0000 0 0 0000 16 2 16 1 16 0 “Base-16” 256 16 1 1 1 0001 1 1 0001 2 2 0010 2 2 0010 3 3 0011 3 3 0011 4 4 0100 4 4 0100 1*256 + 4*16 + 1*1 = 321 5 5 0101 0*256 +12*16 + 13*1 5 5 0101 6 6 0110 6 6 0110 192 + 13 = 205 7 7 0111 7 7 0111 8 8 1000 8 8 1000 9 9 1001 9 9 1001 Positions denote powers of 16 A 10 1010 A 10 1010 But, requires 16 digit settings to denote values B 11 1011 B 11 1011 C 12 1100 C 12 1100 • Binary 0,1 D 13 1101 D 13 1101 E 14 1110 E 14 1110 • Decimal 0,1,2,3,4,5,6,7,8,9 F 15 1111 F 15 1111 • Hexadecimal 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 2

  3. 1/26/2017 Hexadecimal activity Converting bases 0x1AF From Base 10 to other bases  Find largest power x of base less than number n  Find largest base digit b where b*x < n  Recursively repeat using n-(b*x) 0 0 0000 256 16 1 1 1 0001 Example 2 2 0010  15213 10 = 1*10 4 + 5*10 3 + 2*10 2 + 1*10 1 + 3*10 0 3 3 0011 4 4 0100 Base 2 (binary) 1*256 +10*16 + 15*1 5 5 0101 x=2 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 6 6 0110 256 + 160 + 15 = 431 15213 10 = 0 1 1 0 1 1 0 1 1 0 1 1 0 1 2 7 7 0111 11101101101101 2 = 1*2 13 + 1*2 12 + 1*2 11 + 0*2 10 + 1*2 9 + etc… 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 Converting bases Practice Example Convert the following  15213 10 = 1*10 4 + 5*10 3 + 2*10 2 + 1*10 1 + 3*10 0  10110111 2 to Base 10 Base 16 (hexadecimal)  11011001 2 to Base 16 » x=16 65536 4096 256 16 1  0x2ae to Base 2 » 15213 10 = 0 3 B 6 D 16 » 3B6D 16 = 3*16 3 + 11*16 2 + 6*16 1 + 13*16 0  0x13e to Base 10 » Written in C as 0x3b6d  150 10 to Base 2 From Base 2 binary to bases that are powers of two  301 10 to Base 16  Done by grouping bits and assigning digits Base 2 (binary) Hex digits » x=2 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 Base 2 a=10=1010 2 » 15213 10 = 0 1 1 1 0 1 1 0 1 1 0 1 1 0 1 2 128 64 32 16 8 4 2 1 b=11=1011 2 » 11101101101101 2 = 1*2 13 + 1*2 12 + 1*2 11 + 0*2 10 + 1*2 9 + etc… c=12=1100 2 Base 16  Example of binary to hex d=13=1101 2 256 16 1  11 1011 0110 1101 = 3 B 6 D e=14=1110 2 f=15=1111 2 3

  4. 1/26/2017 0x333231 0x333231 0x33 0x32 0x31 3,355,185? ‘3’ ‘2’ ‘1’ Actually, “321” in ASCII ASCII maps byte values to characters Humans encode characters in pairs of hexadecimal digits • Used in URLs, web pages, e-mail • Each pair of hex digits is 8 bits or 1 byte • Other representations (Unicode, EBCDIC) • Bytes are the smallest unit of data for computers ASCII activity Byte-Oriented Memory Organization Snippet #1: 54 68 65 20 68 6F 6D 65 20 74 6F Snippet #2: 65 76 65 72 79 6F 6E 65 20 69 73 Memory organized as an array of bytes Snippet #3: 74 6F 20 68 69 6D 20 68 69 73 Snippet #4: 63 61 73 74 6C 65 20 61 6E 64 Addresses Snippet #5: 66 6F 72 74 72 65 73 73 2E Snippet #6: 45 64 77 61 72 64 20 43 6F 6B 65 FF • • • Memory/Data  Byte = 8 bits  Binary 00000000 2 to 11111111 2  Decimal: 0 10 to 255 10  Hexadecimal 00 16 to FF 16  Address is an index into array  Addressable unit of memory is a byte  Recall system provides private address spaces to each “process” 4

  5. 1/26/2017 Machine Words Word-Oriented Memory Organization 64-bit 32-bit Any given computer has a “Word Size” Bytes Addr. Words stored over Words Words  Nominal size of integer-valued data and pointers (addresses) contiguous byte locations 0000  For IA32, word size was 32 bits (4 bytes) Addr  Address of word specifies the 0001 =  Limits addresses to 4GB (2 32 bytes) lowest address 0000 ?? 0002 Addr  Now with x86-64, word size is 64-bit (8 bytes) 0003  E.g. int x with address 0x4 =  Potentially up to 18 PB (petabytes) of addressable memory 0004 is stored in bytes 0x4, 0x5, 0000 ?? Addr  That’s 18.4 X 10 15 bytes of addressable memory 0x6, 0x7. 0005 = 0004 0006 ??  Addresses of successive 0007 words differ by 4 (32-bit) or 8 0008 (64-bit) Addr 0009 = 0010 0008 ?? Addr 0011 = 0008 ?? 0012 Addr 0013 = 0012 0014 ?? 0015 Byte Ordering Endian How are bytes in multi-byte words ( short , int , long , Which way should you store the 4-byte integer x? any pointers) be ordered in memory? Example  Sun, PowerPC Macs, Internet protocols are “Big Endian”  Assume &x is 0x100  Least significant byte has highest address  Suppose  x86 (PC/Mac), ARM (Android/iOS) are “Little Endian” int x = 0x01234567  Least significant byte has lowest address (LLL) Revisiting example  Variable x at 0x100 has 4-byte representation 0x01234567 01 23 45 67 0x100 0x101 0x102 0x103 Big Endian 0x100 0x101 0x102 0x103 01 01 23 23 45 45 67 67 Little Endian 0x100 0x101 0x102 0x103 67 67 45 45 23 23 01 01 5

  6. 1/26/2017 Endian Endian How do you test endian-ness? How do you test endian-ness?  Simple program from book ( show_bytes )  Direct inspection of memory via gdb  endian #include <stdio.h> int main() #include <string.h> #include <stdio.h> { » gdb endian typedef unsigned char *byte_pointer; int i=0x01020304; main() void show_bytes(byte_pointer start, int len) » break 5 float f=2345.6; { { int *ip=&i; » run int i; char *s = "ABCDEF"; int i=0x01020304; for (i = 0; i < len; i++) » p /x &i printf("%d\n",i); printf(" %.2x", start[i]); show_int(i); printf("\n"); } show_float(f); » x/b &i } show_pointer(ip); » x/b … show_bytes(s,strlen(s)); void show_int(int x) } { show_bytes((byte_pointer) &x, sizeof(int));} void show_float(float x) Output: { show_bytes((byte_pointer) &x, sizeof(float));} 04 03 02 01 9a 99 12 45 void show_pointer(void *x) 28 61 61 63 fc 7f 00 00 http://thefengs.com/wuchang/courses/cs201/class/04/endian.c { show_bytes((byte_pointer) &x, sizeof(void*));} 41 42 43 44 45 46 See: man ascii Representing pointers Pointers in memory Recall Given the following code on x86-64… main() A pointer is a variable containing a memory address of an object of a particular data type x86-64 { D8  Contains a “reference” or address for data int B = -15213; F8 char* cp; /* Declares cp to be a pointer to a character */ int* P = &B; int* ip; /* Declares ip to be a pointer to an integer */ FF }  On x86-64, how many bytes is cp ? 07  On x86-64, how many bytes is ip ? Suppose the address of B is 0x007fff8d8 00 and the address of P is 0x007fff8d0 00 At the end of main, write the value of each byte 00 of P in order as it appears in memory. 00 6

Recommend


More recommend