Search code examples
memorymemory-managementcpu-architecture

Fetching data from memory, how is it done?


What part of the computer program fetches the relevant data from memory?

For example:

let arr = [1,2,3,4]    
let x = arr[3]

What is responsible for fetching the data stored at the address ((the address of array arr) + 3), I know a pointer points to the memory address of interest, but that's an abstraction of what's happening, what's really responsible for fetching the stored data in memory, and storing it in a register for an operation, or in a different memory address?


Solution

  • The processor has an address generator, and a read & write interface.  This is true across all processors but the details vary.

    Simple, educational and older processors have an MAR, memory address register, and MBR, memory buffer register, and some control signals. That constitutes the cpu to memory interface.

    To write to memory, the CPU puts a value into the MBR and MAR, and then asserts a write control signal.  The memory responds by performing the requested write operation (and may signal the processor that it is done).

    To do a read operation, the CPU puts a value into the MAR, and asserts a read control signal; the memory responds puttiing a value into the MBR, (and asserts a ready control signal).  The processor takes the value from the MBR and transfers it to where it should go.

    You might look up architectures like LC-3, MARIE, and look for block diagrams having those specific registers. Modern processors introduce caches in between the CPU and memory, so there are multiple memory interfaces.

    The machine code program will use available instruction encodings to tell the processor when and what to read or write. The compiler's job is to identify machine code instruction sequences that will accomplish the C program's intent.