Live data from Hacker News

Should you learn C to “learn how the computer works”?

words.steveklabnik.com

121–130 of 381 posts

Re: Should you learn C to “learn how the computer works”?

#121
Depends on what do you mean by "work" here, which is a very vague term. One can argue that assembly doesn't teach you how computers work either because it "abstracts away" how transistors do their job. The point is: Depending on how deep you want to go in your understanding of the underlying system, there is always a level of abstraction that you have to settle on before building your knowledge on top of it.

Turns out that using C as the programming language for your project puts you at a very balanced spot in the abstraction hierarchy, where you don't have to learn processor specific instructions (well, not all of them) while requiring you to fully understand the "programmer's model" of the target processor. Keep in mind that learning C syntax alone doesn't do jack shit. You HAVE to be able to make full use of the tooling that a C toolchain provides. That means you need to learn things like memory layout (linker scripts, injecting code at a particular memory address / ISRs / faults and handlers), boot process (startup scripts, stack setup / data-segment initialization), kernel/user-mode and what not. So yeah, C is THE best language to learn your target system because of the level of optimization of the abstraction of the hardware that it provides you. Your code may be getting translated from a standard C architecture to the specific machine architecture; nobody's stopping you from learning how that process takes place. You could choose to learn the exact process, which C provides you complete tools for, or you could choose to ignore that process and let your compiler do its job. The beauty lies in the many possible levels of control.

Re: Should you learn C to “learn how the computer works”?

#122
post #87

Earlier quoted context omitted.

RISC-type architectures with a branch-and-link instruction (as opposed to a jsr- or call-type instruction) generally have a stack by convention only, because the CPU doesn't need one to operate. (For handling interrupts and exceptions there is usually some other mechanism for storing the old program counter.)

Can you point me to a RISC architecture that doesn't have push and pop instructions?

Nearly all of them?

ARM's pop is really a generic ldmia with update. You can use the same instruction in a memcpy.

MIPS, PowerPC, and Alpha don't have anything like push and pop, splitting load/stores and SP increment/decrement into separate instructions.

AArch64 has a dedicated stack pointer, but no explicit push pop.

In general the RISC style is to allocate a stack frame and use regular loads and stores off of SP rather than push and pop.

Re: Should you learn C to “learn how the computer works”?

#123

I like this article a lot. There are two ways you can think of looking at the field of programming: * As a continuum from "low level" to "high level". * As a giant bag of topics: strings, heap, hash tables, machine learning, garbage collection, function, instruction set, etc. If your goal is to have a broad understanding of CS, you want to explore the whole continuum and many topics. C is great for that because it ex…

Thank you!

> if your goal is just to get familiar with that region of the continuum and not become an expert in a new language, C has a good price/performance ratio.

I think this is a particularly great point in your post. I wonder what AndyKelley thinks of this, as in my understanding, that's sort of what Zig is trying to do as well. That is, Zig is attempting to be a language on a specific spot on the price/performance ratio, as it were.

Re: Should you learn C to “learn how the computer works”?

#124

C teaches you how a computer works because the C abstract machine is defined such that operations that must be manually performed in assembly language must also be manually performed in C. C doesn't let you write something like: string x = y; ...because to actually create a copy of a string the computer must allocate memory, copy memory, and eventually free the memory. In C each of these steps is manual. Higher-level…

The exact reason why C shouldn't be used to write high level software.

Also, the set of things you can do in standard C is a subset of the things you can do in assembler.

Re: Should you learn C to “learn how the computer works”?

#125

C doesn't necessarily teach you how computers work. But it does teach you how software works. Our modern software empire is built on mountains of C, and deference to C is pervasive throughout higher-level software design. >You may have heard another slogan when talking about C: “C is portable assembler.” If you think about this slogan for a minute, you’ll also find that if it’s true, C cannot be how the computer work…

> there are many kinds of different computers, with different architectures

are there ? how many laptops, desktops, smartphones, tablets, use anything else than the von neumann architecture ?

Re: Should you learn C to “learn how the computer works”?

#126
post #63

I find this article to be disingenuous. Yes, C isnt "how a computer really works". Neither is assembly. The way a computer works is based off of transistors and some concepts built on top of that (ALUs for example). However, there is no need to know about any of that because you're presented with an abstraction (assembly). And thats really what people mean when they say C is closer to how a computer actually works: i…

But in addition to the mismatch between the abstractions provided and the real hardware, C qua C is missing a huge number of abstractions that are how real hardware works, especially if we pick C99 as Steve did, which doesn't have threading since that came later. I don't think it has any support for any sort of vector instruction (MMX and its followons), it doesn't know anything about your graphics card which by raw…

> A computer does not need to implement a stack

C doesnt "have", or require, a stack, either. It has automatic variables, and I think I looked once and it doesn't even explitly require support for recursive functions.

Re: Should you learn C to “learn how the computer works”?

#127
As someone who felt that C was the path to knowledge for how modern computer systems "work", Forth and QEMU have become my "stretch challenge" for those with the motivation to tinker.

For me, working thru the resources on the OSDev wiki by taking jonesforth and linking it with the bootstrap from the "Writing an OS in Rust" tutorial (https://os.phil-opp.com/) really showed me how far C is from the hardware, and how much closer Forth connects to the idiosyncrasies of a computer system. Further, the immediacy of the Forth REPL can help with little experiments and building up the understanding of how much a machine's architecture influences code/data structures, opcode choice, privilege management, etc.

Plus, understanding Forth will bend your brain in a very strange but positive way :)

Re: Should you learn C to “learn how the computer works”?

#128
"In 1972, on a PDP-11, they wrote the first C compiler, and simultaneously re-wrote UNIX in C. Initially, portability wasn’t the actual goal, but C did resonate with a lot of people, and C compilers were ported to other systems."

The fathers of UNIX explicitly said in one of the interviews that they invented C so that they could make UNIX portable.

Re: Should you learn C to “learn how the computer works”?

#129

As someone who felt that C was the path to knowledge for how modern computer systems "work", Forth and QEMU have become my "stretch challenge" for those with the motivation to tinker. For me, working thru the resources on the OSDev wiki by taking jonesforth and linking it with the bootstrap from the "Writing an OS in Rust" tutorial ( https://os.phil-opp.com/ ) really showed me how far C is from the hardware, and how…

I’m convinced that Forth is a valuable intellectual exercise.

Can you elaborate on how you feel Forth better matches how a computer actually works? I’m not yet convinced on that point, but I have no Forth experience.

Re: Should you learn C to “learn how the computer works”?

#130
post #58

C doesn't necessarily teach you how computers work. But it does teach you how software works. Our modern software empire is built on mountains of C, and deference to C is pervasive throughout higher-level software design. >You may have heard another slogan when talking about C: “C is portable assembler.” If you think about this slogan for a minute, you’ll also find that if it’s true, C cannot be how the computer work…

C at the very least teaches the difference between stack and heap memory, a crucial concept obscured by most higher-level languages.

Just playing devil's advocate here... why is this such a "crucial" concept, for someone who is using a higher level language (like Python or Ruby)?

If 99% of what that person does is gluing together APIs and software modules, and they can see their memory usages are well within range, why does it matter?

Post reply on HN