Byte-Oriented Memory Organization
- Programs refer to data by address
- Imagine all of RAM as an enormous array of bytes
- An address is an index into that array
- A pointer variable stores an address
- System provides a private address space to each "process"
- A process is an instance of a program, being executed
- An address space is one of those enormous arrays of bytes
- Each program can see only its own code and data within its enormous array
- "virtual memory" class에서 더 자세하게 배울거다
Machine Words
Any given computer has a "word size"
- Nominal size of integer-value data
- Until recently, most machines used 32 bits(4 bytes) as a word size
- Increasingly, machines have 64-bit word size
Addresses Always Specify Byte Locations
- Address of a word is address of the first byte in the word
- Addresses of successive words differ by 4(32-bit) or 8(64-bit)
Byte Ordering
So, how are the bytes within a multi-byte word ordered in memory?
Conventions
- Big Endian : LSB has higest address
- Little Endian : LSB has lowest address x86
Byte Ordering example
- variable x has 4-byte value of 0x01234567
- address given by &x is 0x100
Examining Data Representations
- Code to print byte representation of Data
typedef unsigned char *pointer;
void show_bytes(pointer start, size_t len) {
size_t i;
for (i = 0; i < len; i++)
printf("%p\t0x%.2x\n", start + i, start[i]);
printf("\n");
}
int a = 15213;
printf("int a = 15213;\n");
show_bytes( (pointer) &z, sizeof(int) );
결과
int a = 15213
0x7fffb7f71dbc 6d
0x7fffb7f71dbd 3b
0x7fffb7f71dbe 00
0x7fffb7f71dbf 00
=>Little Endian
15213
0011 1011 0110 1101
3B6D
Representing Strings
Strings in C
- represented by arrays of characters
- each character encoded in ASCII format
15213
31 38 32 31 33