02-bits-kesden[1]
- 格式:pdf
- 大小:2.61 MB
- 文档页数:25
» Or 0xfa1d37b
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Represented by array of characters Each character encoded in ASCII format
Addr = 0012 ?? –6–
Data Representations
Sizes of C Objects (in Bytes)
C Data Type
char short int long long long float double long double char *
Limits addresses to 4GB
Becoming too small for memory-intensive applications
High-end systems use 64 bits (8 bytes) words
Potential address space 1.8 X 1019 bytes x86-64 machines support 48-bit addresses: 256 Terabytes
Typical 32-bit
1 2 4 4 8 4 8 8 4
Intel IA32
1 2 4 4 8 4 8 10/12 4
x86-64
1 2 4 8 8 4 8 10/16 8
» Or any other pointer
–7–
15-213, S ‘09
Byte Ordering
How should bytes within multi-byte word be ordered in memory? Conventions
0 1 0
Electronic Implementation
3.3V 2.8V
0.5V
0.0V
–2– 15-213, S ‘09
Encoding Byte Values
Byte = 8 bits
Binary 000000002 Decimal: 010
Hexadecimal 0016
to to
0x100 0x101 0x102 0x103
Example
Big Endian
01 Little Endian
23
45
67
0x100 0x101 0x102 0x103
67
–9–
45
23
01
15-213, S ‘09
Reading Byte-Reversed Listings
Disassembly
Different compilers & machines assign different locations to objects
– 14 – 15-213, S ‘09
Representing Strings
Strings in C
char S[6] = "15213";
Linux/Alpha S Sun S 31 35 32 31 33 00 31 35 32 31 33 00
15-213, S ‘09
Machine Words
Machine Has “Word Size”
Nominal size of integer-valued data
Including addresses
Most current machines use 32 bits (4 bytes) words
15-213 “The Class That Gives CMU Its Zip!”
Bits, Bytes, and Integers
Topics
Representing information as bits Bit-level manipulations
Boolean algebra Expressing in C
– 11 –
15-213, S ‘09
show_bytes Execution Example
int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int));
Result (Linux):
int a = 15213; 0x11ffffcb8 0x11ffffcb9 0x11ffffcba 0x6d 0x3b 0x00
Words Words
Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015
15-213, S ‘09
Addresses Specify Byte Locations
Addr = 0000 ??
Program being executed
Program can clobber its own data, but not that of others
Compiler + Run-Time System Control Allocation
–4–
Where different program objects should be stored All allocation within single virtual address space
Text representation of binary machine code Generated by program that reads the machine code
Example Fragment
Address 8048365: 8048366: 804836c: Instruction Code 5b 81 c3 ab 12 00 00 83 bb 28 00 00 00 00 Assembly Rendition pop %ebx add $0x12ab,%ebx cmpl $0x0,0x28(%ebx)
Code to Print Byte Representation of Data
Casting pointer to unsigned char * creates byte array
typedef unsigned char *pointer; void show_bytes(pointer start, int len) { int i; for (i = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } Printf directives: %p: Print pointer %x: Print Hexadecimal
to
111111112 25510
FF16
First digit must not be 0 in C
Base 16 number representation
Use characters „0‟ to „9‟ and „A‟ to „F‟ Write FA1D37B16 in C as 0xFA1D37B
Byte Ordering Example
Big Endian
Least significant byte has highest address
Little Endian
Least significant byte has lowest address Variable x has 4-byte representation 0x01234567 Address given by &x is 0x100
Deciphering Numbers
– 10 –
Value: Pad to 32 bits: Split into bytes: Reverse:
0x12ab 0x000012ab 00 00 12 ab ab 12 00 00
15-213, S ‘09
Examining Data Representations
Addr = 0000 ??
Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)
Addr = 0004 ??
Addr = 0008 ??
Addr = 0008 ??
Representations of Integers
Basic properties and operations Implications for C
class02.ppt
15-213 F ‘08
Binary Representations
Base 2 Number Representation
0x11ffffcbb
– 12 –
0x00
15-213, S ‘09
Representing Integers
int A = 15213; int B = -15213; long int C = 15213;