3

If we want to access element i of an array, we would do: i * (size of each element) + starting address of array = memory location of the element we need to access. The CPU then directly accesses that location in the RAM. How does this happen?

Say if the memory address I want to locate is 5064, I imagine that the CPU have to search sequentially from 1 to 5064, and when it reaches 5064, return the item in the memory address. But this apparently isn't how the RAM works because this isn't a direct access per se.

  • a program is given a range of addresses to work with. each item has its own address in this range. when the program wants to access an item, it just loads the item's memory address and then reads from/writes to it. then you have to take into account the stack, the different ranges that specific items lie, and so forth. but **no**, the program does not have to count from address `0x0` to whatever the address is to access it. – absoluteAquarian Dec 10 '18 at 17:44
  • if you *really* want to understand how programs access memory addresses and manipulate them, then i would suggest looking into **ASM** (assembly language). – absoluteAquarian Dec 10 '18 at 17:47
  • 1
    @Tau I'm really asking about about the hardware level i.e. how does the cpu access that memory address of the RAM, given that it knows which address to access already. Imagine search for a book item in a library. You know the book number you want to find, so you search through the shelves which have labels of the book numbers contained in each shelf. Does accessing the RAM work in a similar way? – user10772016 Dec 12 '18 at 20:04
  • Well, no. The program already knows where everything is. If it wants to get the value at, let's say address `0x00B38FD2`, then it can go directly to that address and retrieve the value from it (minus a few other underlying mechanics, e.g. "can this program access that address?"). Imagine it like looking at a shelf of a lot of colours (like the ones you'd find at Home Depot). If you want to get the colour that's **Gray**, then you can go directly to it and take it out. – absoluteAquarian Dec 13 '18 at 14:08
  • The letters RAM stand for "random access memory". What you describe is sequential access memory (like tape drives). – Raymond Chen Jan 13 '19 at 17:12
  • Possible duplicate of [Why does RAM access any memory address in O(1) time?](https://stackoverflow.com/questions/51813009/why-does-ram-access-any-memory-address-in-o1-time) – Raymond Chen Jan 13 '19 at 17:14
  • Does this answer your question? [What Every Programmer Should Know About Memory?](https://stackoverflow.com/questions/8126311/what-every-programmer-should-know-about-memory) – Peter Cordes Aug 22 '20 at 02:55

1 Answers1

0

In the hardware level It uses circuit like: Line Decoder to achieve instant location of memory cell in RAM.

enter image description here

The above circuit can represent 8 memory cells by using three bits (A0, A1, A2). each bit has two states 0 or 1, E.G. 101 will activate A0, A2. and the corresponding output will be selected let's say Z2 in this case. All the outputs Z0, Z1 .. Z7 could be circuit to a memory cell. This way the selection of memory address is constant operation.

reference: 3-Line to 8-Line Decoder and Demultiplexer

didxga
  • 5,935
  • 4
  • 43
  • 58