Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is data from the RAM fetched?

Tags:

c

memory

x86

ram

In C each byte is individually addressable. Suppose an integer (say which uses 4 bytes) has an address 0xaddr (which is 32 bits, assuming that we have a 32 bit processor with 32 bit address bus and 32 bit data bus) and suppose the value of the integer is 0x12345678. Now if I am fetching this value from memory, how does the processor do this ? Does the processor place 0xaddr (which is 32bit address) on the address lines and then fetch 8 bit data say 0x12. And then processor will pace 0xaddr+1 on address lines and then fetch another 8 bit data 0x34 and so on for the 4 bytes of an integer? Or does the processor just place 0xaddr and read the 4 bytes at once thus utilizing its full 32 bit data bus?

like image 759
mezda Avatar asked Mar 17 '12 14:03

mezda


1 Answers

This is a well known article by the GNU C library lead that describes memory access (particularly in x86 - current PC - systems). It goes into far more detail than you can ever possibly need.

The entire article is spread across many parts:

  1. Introduction
  2. CPU Caches
  3. Virtual Memory
  4. NUMA Support
  5. Programmers
  6. More Programmers
  7. Performance Tools
  8. Future
  9. Appendices

one thing i'd add to gbulmer's answer is that in many systems getting a stream of data is faster than you would expect from getting a single word. in other words, selecting where you want to read from takes some time, but one you have that selected, reading from that point, and then the next 32 or 64 or whatever bits, and then the next... is faster than switching to some unconnected place and reading another value.

and what dominates modern programming is not the behaviour of fetching from memory on the motherboard, but whether the data are in a cpu cache.

like image 139
andrew cooke Avatar answered Oct 20 '22 19:10

andrew cooke