Memory Organization

 < Day Day Up > 



Most modern computer systems have similar memory organizations and as a programmer you should be aware of how computer memory is organized and accessed. The best way to get a good feel for how your computer works is to poke around in memory and see what’s in there for yourself. This section provides a brief introduction to computer memory concepts to help get you started.

Memory Basics

A computer’s memory stores information in the form of electronic voltages. There are two general types of memory: volatile and non-volatile. Volatile memory will lose any information stored there if power is removed for any length of time. Main memory and cache memory, two forms of Random Access Memory (RAM), are examples of volatile memory. Auxiliary storage devices such as CD ROMs, DVDs, hard disk drives, floppy disks, and tapes, are examples of non-volatile memory.

Memory Hierarchy

Computer systems contain several different types of memory. These memory types range from slow and cheap to fast and expensive. The proportion of slow cheap memory to fast expensive memory can be viewed in the shape of a pyramid commonly referred to as the memory hierarchy as shown in figure 4-8.

click to expand
Figure 4-8: Memory Hierarchy

The job of a computer system designer with regards to memory subsystems is to make the whole computer perform as if all the memory were fast and expensive. Thus they utilize cache memory to store frequently used data and instructions and buffer disk reads to memory to give the appearance of faster disk access. Figure 4-9 shows a block diagram of the different types of memory used in a typical computer system.

Faster memory is checked for the requested data or instruction first. If it is not there, a performance penalty is extracted in the form of longer overall access times required to retrieve the information from a slower memory source.

click to expand
Figure 4-9: Simplified Memory Subsystem Diagram

Bits, Bytes, Words

Program code and data are stored in main memory as electronic voltages. Since I’m talking about digital computers the voltages levels represent two discrete states depending on the level. Usually low voltages represent no value, off, or 0, while a high voltage represents on, or 1.

When program code and data is stored on auxiliary memory devices, electronic voltages are translated into either electromagnetic fields (tape drives, floppy and hard disks) or bumps that can be detected by laser beam (CDs, DVDs, etc.).

Bit

The bit represents one discrete piece of information stored in a computer. On most modern computer systems bits cannot be individually accessed from memory. However, after the byte to which a bit belongs is loaded into the processor the byte can be manipulated to access a particular bit.

Byte

A byte comprises 8 bits. Most computer memory is byte addressable although as processors become increasingly powerful and can manipulate wider memory words, loading bytes by themselves into the processor becomes increasingly inefficient. This is the case with the G4 processor and for that reason the fastest memory reads can be done a word at a time.

Word

A word is a collection of bytes. The number of bytes that comprise a word is computer system dependent. If a computer’s data bus is 32 bits wide and its processor’s registers are 32 bits wide then the word size would be 4 bytes long. Bigger computers will have larger word sizes meaning they can manipulate more information per unit time than a computer with a smaller word size.

Alignment and Addressability

C++ programmers can expect to find the memory on their systems to be byte addressable and word aligned. Figure 4-10 shows a simplified diagram of a main memory divided into bytes and the different buses connecting it to the processor.

click to expand
Figure 4-10: Simplified Main Memory Diagram

The memory is byte addressable in that each byte can be individually accessed although the entire word that contains the byte is read into the processor. Data in memory can be aligned for efficient manipulation. Alignment can be to natural or some other boundary. For example, on a PowerPC system, contents of memory assigned to instances of structures is aligned to natural boundaries meaning a one byte data element will be aligned to a one byte boundary. A two byte element would be aligned to a two byte boundary. Individual data elements not belonging to structures are usually aligned to four byte boundaries.

Understanding alignment will come in handy when you start trying to understand where your compiler is putting your program variables. Figure 4-11 shows a project settings window for Metrowerks CodeWarrior™. Notice the tab pointed to by the arrow that says Struct Alignment. The options for this tab are listed in table 4-3.

click to expand
Figure 4-11: CodeWarrior Code Generation Settings Window

Table 4-3: CodeWarrior Structure Alignment

Tab Selected...

Results In...

68K

(meaning a 68000 processor)

2-byte boundaries, unless a field is only 1-byte long. This is the standard alignment for 68K Macintosh computers.

68K 4-byte

4-byte boundaries

PowerPC

Its natural boundary. For example, it aligns a 1-byte character on a 1-byte boundary and a 16-bit integer on a 2-byte boundary. The compiler applies this alignment recursively to structured data and arrays containing structured data. So, for example, it aligns an array of structured types containing an 8-byte floating point member on an 8-byte boundary. This is the standard alignment for Mac OS computers.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net