
Ask HN: Why a Computer Need a “Memory”? - terrycody
sry guys I am a totally newbie on computer science, lately I am learning the basic knowledge of computer science from a Youtube channel Crash Course, in their #6 episode, they talked about the hardware level of how a RAM built, I have a very hard time to get the idea why a computer needs a memory&#x2F;RAM<p>For example, due to billions of transistors inside a CPU, say if we get a number 1110001010101 in somewhere of those transistors connected wires, why not just leave those wires as is (don&#x27;t touch these wires), is this the same way as &quot;store a value&quot; as RAM does?<p>I am trying hard to visualize this hardware in my brain and why it is necessary to build so called &quot;latch&quot; or whatever, compare with just leave those wires (because we got enough wires inside)<p>Could anyone help on this topic? Any life example like play games, type words, watch videos or whatever, to prove a computer has to have a RAM to work?!
======
PaulHoule
If the memory was coded in the connections between the transistors, it would
be a read-only memory, or a ROM.

~~~
terrycody
thanks for your reply, but this is not the answer I am looking, I want to know
the reason why RAM is necessary for a computer, why not just leave those
circuits untouched, because if a number is 10001011, those wires already got
current, I don't need to store this number elsewhere, right?

~~~
pwg
You can't leave wires 'untouched' in an electrical system. Unless something
continuously supplies energy to maintain the charge, the charges on those
'untouched' wires will simply fade away, and with it, your data. [1]

The data is stored in the electrical charges on the wires, not in the actual
wires, which appears to be one source of your confusion.

Further, those wires will get reused on the next cycle for the next piece of
data moving through, which means if you don't save the values represented by
the current charge state of the wires, the data will be gone when the next
cycle happens and new charges are forced onto the wires for the next piece of
data.

RAM (memory) is the place for holding onto those particular charges (which is
what is storing the data) that were on the wires, until a later time when you
want to go pick those pieces of data up again.

[1] ignoring flash memory, which is a very specially designed "charge holder"
that does not need the continuous energy supply to be able to maintain the
charge states.

~~~
AnimalMuppet
Amusing and somewhat related story: I was trying to (programmatically) tell
the difference between two versions of a circuit board. At a certain address,
version 1 had an 8-bit read-write register, and version 2 had a 16-bit read-
write register. So I wrote a 16 bit value to that address, read it back... and
read the correct value, even from the board that only had an 8-bit register
there!

What happened was, when I wrote the value, the bus driver chips drove all 16
bits of the bus to the levels specified by the data. Even if the data wasn't
latched by the register, that bit of the data bus still had the voltage level
corresponding to the data bit. When I read the value, the bus driver chips
reversed direction, and wrote out what the voltage level on the data lines
said. For the 8 bits that were not driven by the register, the voltage level
they had was the voltage level that I just wrote to them (because every chip
on those bits of the data bus was in a high impedance state, both for the read
and for the write). So I was literally using the wires of the data bus as
memory! I found that it could remember data for roughly two seconds this way.

So, why do we need memory? Because we want the computer to remember for more
than two seconds. And because we want it to remember more bits of data than we
have transistors in the CPU chip. And because we want to use the transistors
in the CPU chip for something else - for computing, not for memory.

~~~
terrycody
thank you for your story, I am too early to understand such a case lol, but I
get the idea, which means, a wire can hold current a while, but not forever,
yes this is clear, but why not just keep give the wires current flow? Then a
"1" state stored!

If we want to store another number, just use other wires! We have billions of
transistors and bus wires right? Why a RAM is a must when we have near
unlimited wires already ?

~~~
pwg
> but why not just keep give the wires current flow?

And... On a very abstract level, you've just designed a memory.

A memory is a particular design of "wiring" [1] setup in such a way to keep
the current flowing [2] to store the charge that represents the data.

[1] There is more than just wires, as just wires alone can not store a charge
for very long (example, plug in an extension cord, the extension cord output
will be powered, unplug it, no power on the other end).

[2] This is exactly the design of a SRAM chip. Wires and transistors connected
together to "keep the current flowing" to hold the data represented by the
charges.

------
salawat
So, you seem to be laboring under some drastic misunderstandings.

Let me try to lay out an example that may make things clearer.

I have a CPU.

A CPU consists of a collection of general registers (data is generally read
from and into here), a special set of registers containing metainformation
about the state of the CPU, an arithmetic processing unit, and a clock
circuit. This is a dirt simple CPU.

Now. If you're using a Von Neumann based architecture, your CPU doesn't
distinguish data from instructions. (Modern ones do, but ignore that for now.)

An "instruction" consists of an op-code (basically a number), plus addresses
indicating where input data is being read from, and where it's being written
to.

So a set of commands for this CPU might look like

load $1 1 load $2 1 Add $1 $2 $3

Assuming we're "wired" into some input pin the CPU knows to listen to, we're
running a rudimentary set of computations. We're sending in some number that
the CPU recognizes as the beginning of a "load" command, it looks at the 8
bits or so following, and loads 00000001 into register $1. Done for that
cycle.

Next cycle, same deal, but in register $2.

Next cycle, we send the contents of $1 and $2 to the adder, which outputs the
result into $3.

Easy right?

Now. Let's do some REAL computation. Let's say I have a ridiculous amount of
data to crunch.

I have 30 8 bit registers for general use, and my 5 or 6 special registers,
and my input pin.

But I have 300000000000000 8 bit numbers that I want to add!

I can make this work with just the CPU logic. I can do a bunch of serial
computations, then record the results by hand, and re-input that result as an
input to the next computation over and over again, But dear God, that's
boring!

Wouldn't it be great if somehow, I could stage all of my information ahead of
time, and somehow have the processor look up that information to do it's
calculating, with out me having to remember everything for it?

The answer to that conundrum is Random Access Memory. It's a separation of
concerns if you will.

CPU's do computation. That's what they are for. Doing the stuff that might
cause a state change. We don't want values sticking around in the processor
that aren't changing if we can help it. Once something is done we want it out
of the processor so we can accommodate the next state changes.

In CS, we tend to have two main axes on which we measure the footprint of
computation: CPU time, and space (memory footprint).

If I have high space constraints, I can set up the CPU to handle all the
state, but for every new computation, I have to reset everything back to
basics, and still keep track of all my information. This quickly turns a
simple one cycle operation into many cycles worth of operation and bookkeeping
about where everything is.

Or, If I have memory to write to, I can do my computation, write it out to
somewhere that's memory managed, and let the magic Memory Management Unit feed
me new data and instructions while also cleaning up results, and holding onto
them elsewhere on the motherboard so I can use my full CPU die to do
computations more quickly.

Look at it like having a helper to get books for you at a library while you're
sitting at home reading books and doing math problems.

The library is RAM, you are the CPU, and your home is the CPU cache. Without
your helper fetching books for you, you have to stop all the time, and take
the books you're done with back, check out new ones, go home, and only then
start doing math again. (You're not independently wealthy, or psychic, so
absurdly big house, or magical access to all of the books by telekinesis is
not allowed, sorry!)

With your helper, you're doing math (yay!), and he/she's swapping out books
to/from the library to keep you running smoothly.

Try not to get caught up thinking about wires. Circuits have about as much to
do with Computer Science as a scalpel does to Surgery. They're a tool. Nothing
more. The important thing is to understand how all the pieces work together to
create a functional whole.

If you're still confused about why RAM is a thing, let me introduce you to

[https://en.m.wikipedia.org/wiki/ENIAC](https://en.m.wikipedia.org/wiki/ENIAC)

A computer that, quite literally, was absolutely dependent on the people
around it to play the role of "memory" since each computation required
rewiring the damn thing.

Ain't nobody got the patience for that anymore!

~~~
terrycody
hey sir, first thank you so much for your typing, this must cost you some
times, really appreciated actually trying to help a stupid guy!

I googled another several hours till now, yes still confusing as hell, but I
indeed learned a lot new things regard "Memory", I think my problem lies in PC
architecture, why engineers design a PC like this bla bla, and I indeed know
its because practical usage to do this lol, just can't get the real CORE of
WHY this.

I understand most part of what u said, yes its all about efficiency and best
practices.

What I still confused about is am I right about the wires things? For example,
I don't care about the efficiency or engineering reasons to have "individual
Memory" for computers, now I want to build my own PC, I just want to use the
wires inside a CPU to do everything!

Say, we have a ton of wires now in my CPU, fist input 2 8 bits number,
00000011 and 00100101, because the wires got voltage when it got "1", this
wire will always keep at "1" if I don't rewrite any of these wires to another
value.

So now let's send this 2 number to an adder, so maybe adder need locate the
wire numbers (in this case, #1-16), or adder need physically wired onto those
16 lines or whatever,(I think this way is impossible, because if connected,
the charges will flow through already, make it harder to add them together?! )

Anyway, if first method is doable, then in my CPU, I done the calculation
without any Memory system lol.

If I need more numbers, just use more wires! (sry I don't know how many such
circuits inside a CPU, I think its limited though)

\---------------------------------------------------------------- I think
there must be errors of this such logic, just I don't where :( I can't find
the exact thing online, I think I am just a fool, because maybe nobody think
like this, I fall into some wrong circle.

Maybe at the very end of day, just try to convince myself like this: why PC
need memory system? because we need store data. Why not just use wires inside
a CPU? Because its not efficient lol.

Thank you for your help again, I think I will face more problems along the
way, what a though journey to self taught CS :(

~~~
salawat
Hmmm.

Well. You're getting closer at least, and you're at least poking the right
places of the field for answers, as computer architecture should get you a
great deal more insight into what constitutes a CPU.

One thing that you might help is to look into Integrated Circuits as well.

One thing many don't actively realize is that what we call a CPU in a computer
in not the only "processor" in a computer. There are other, more specialized
processors which are responsible for providing interfaces between the CPU and
hardware peripherals, but which are far more tightly constrained in what they
can do.

Best I can tell you for now though is that for the most part, you'll rapidly
run into non-computing related problems the more you Try to cram into a
processor die. Physics still apply, and computation creates heat. Heat
degrades the performance of the system, and the more transistors you pack into
the same area, the harder it is to keep cool, fabricate accurately, and
guarantee the life time of. Also, on die memory opens you up to problems in
term of actual circuit layouts.

There have been designs where certain alignments of values in the physical
chunk of Silicon can have undesired side effects on values stored near them.
Rowhammer I believe the phenomena is called.

So, specialization ANF separation of concerns won the day.

Hope you find what you're looking for!

