COMP 21000 Comp Org Assembly Lang Bits and
COMP 21000 Comp Org & Assembly Lang Bits and Bytes Topics n n Why bits? Representing information as bits l Binary / Hexadecimal l Byte representations » Numbers » Characters and strings » Instructions n Bit-level manipulations l Boolean algebra l Expressing in C
Byte-Oriented Memory Organization Programs Refer to Virtual Addresses n Conceptually very large array of bytes n Actually implemented with hierarchy of different memory types l SRAM, DRAM, disk l Only allocate for regions actually used by program n In Unix and Windows NT, address space is private to particular “process” l Program being executed l Program can clobber its own data, but not that of others – 2– COMP 21000, S’ 16
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) n n n – 3– Where different program objects should be stored Multiple mechanisms: static, stack, and heap In any case, allocation within single virtual address space COMP 21000, S’ 16
Machine Words Machine (or processor) Has “Word Size” n Nominal size of integer-valued data l Including addresses n Last generation machines used 32 bits (4 bytes) words l Limits addresses to 4 GB l Becoming too small for memory-intensive applications l We’ll use 32 bits as the word size in this class n Most current systems use 64 bits (8 bytes) words l Potential address space = 264 1. 8 X 1019 bytes n Machines support multiple data formats l Fractions or multiples of word size l Always integral number of bytes – 4– COMP 21000, S’ 16
Word-Oriented Memory Organization 32 -bit 64 -bit Words Addresses Specify Byte Locations n n Address of first byte in word Addresses of successive words differ by 4 (32 -bit) or 8 (64 -bit) Addr = 0000 ? ? Addr = 0004 ? ? Addr = 0008 ? ? Addr = 0012 ? ? – 5– Addr = 0000 ? ? Addr = 0008 ? ? Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 COMP 21000, S’ 16
Data Representations Sizes of C Objects (in Bytes) n C Data Type Alpha (RIP) Typical 32 -bit Intel IA 32 4 int 4 long int 8 char 1 short 2 float 4 double 8 long double 8/16† char * 8 » Or any other pointer 4 4 4 1 2 4 8 8 4 4 1 2 4 8 10/12 4 l unsigned l l l l († Depends on compiler&OS, 128 bit FP is done in software) – 6– COMP 21000, S’ 16
Byte Ordering How should bytes within a multi-byte word be ordered in memory? Conventions n Sun’s, Power. PC Mac’s are “Big Endian” machines l Least significant byte has highest address n Intel machines (including PC’s and Mac’s) and most ARM are “Little Endian” machines l Least significant byte has lowest address n n n Some architecture is “bi-endian”, i. e. has a switch. Eg, ARM Most networking hardware is “Big-Endian” See http: //en. wikipedia. org/wiki/Endianness for a history of “endian-ness” Aside: “endian” is from Gulliver’s Travels , a book written by Jonathan Swift, in 1726. – 7– COMP 21000, S’ 16
Byte Ordering Example Big Endian n Least significant byte (e. g. , 67) has highest address Little Endian n Least significant byte has lowest address Endian is a function of hardware Example n n Variable x has 4 -byte representation 0 x 01234567 Address given by &x is 0 x 100 Big Endian 0 x 100 0 x 101 0 x 102 0 x 103 01 Little Endian 45 67 0 x 100 0 x 101 0 x 102 0 x 103 67 – 8– 23 45 23 01 COMP 21000, S’ 16
Why does endian-ness matter? Web Server What is Lady Gaga’s age? (solaris box) 28 0 x 00 00 00 1 C Web Client (browser) (intel box) 469, 762, 048 Displayed as 0 x 1 C 00 00 00 Interpreted as 0 x 00 00 00 1 C – 9– COMP 21000, S’ 16
Why does endian-ness matter? Your machine (solaris box) Spreadsheet created on big-endian machine Final cost: $ 28 Stored as 0 x 00 00 00 1 C Your client’s machine (intel box) Spreadsheet read on bigendian machine Final cost: $ 469, 762, 048 Displayed as 0 x 1 C 00 00 00 Interpreted as 0 x 00 00 00 1 C – 10 – COMP 21000, S’ 16
Reading Byte-Reversed Listings Disassembly n Text representation of binary machine code n Generated by program that reads the machine code Example Fragment Address 8048365: 8048366: 804836 c: Instruction Code 5 b 81 c 3 ab 12 00 00 83 bb 28 00 00 Assembly Rendition pop %ebx add $0 x 12 ab, %ebx cmpl $0 x 0, 0 x 28(%ebx) Deciphering Numbers n n – 11 – Value: Pad to 4 bytes: Split into bytes: Reverse: 0 x 12 ab 0 x 000012 ab 00 00 12 ab ab 12 00 00 COMP 21000, S’ 16
Examining Data Representations Code to Print Byte Representation of Data n Casting pointer to unsigned char * creates byte array typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int len) { int i; This program is for (i = 0; i < len; i++) described in printf("0 x%pt 0 x%. 2 xn", section 2. 1. 4 of start+i, start[i]); the textbook printf("n"); } Don’t need the 0 x for pointers in some compilers See – 12 – /home/barr/Student/comp 210/examples/show. Bytes. c Printf directives: %p: Print pointer %x: Print Hexadecimal %. 2 x means print only 2 digits of hex COMP 21000, S’ 16
show_bytes Execution Example int a = 15213; printf("int a = %d; n”, a); show_bytes((byte_pointer) &a, sizeof(int)); Real representation: Result (Linux): 15213 int a = 15213; Address of bytes – 13 – 0 x 11 ffffcb 8 0 x 6 d 0 x 11 ffffcb 9 0 x 3 b 0 x 11 ffffcba 0 x 00 0 x 11 ffffcbb 0 x 00 0011 1011 0110 1101 3 B 6 D Content stored at the address COMP 21000, S’ 16
show_bytes Execution Example int a = 15213; printf("int a = %d; n”, a); show_bytes((byte_pointer) &a, sizeof(int)); float b = 15. 213; 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)); – 14 – COMP 21000, S’ 16
Summary of the Main Points It’s All About Bits & Bytes n Numbers n Programs Text n Different Machines Follow Different Conventions for n n n Word size Byte ordering Representations Boolean Algebra is the Mathematical Basis n n Basic form encodes “false” as 0, “true” as 1 General form like bit-level operations in C l Good for representing & manipulating sets – 15 – COMP 21000, S’ 16
- Slides: 15