← Back to context

Comment by tsimionescu

1 year ago

That often leaves people with very bad mental models of how programs actually compile in modern optimizing compilers and in modern operating systems (e.g. people end up believing that variables always live on the stack, that function parameters are passed on the stack, that loops are executed in the same way regardless of how you write them, etc).

Think about how far they've come if you get them to have these "misconceived" ideas!

They would understand code and data are in the same place, that all flow control effectively boils down to a jump, and they have a _more_ accurate picture of the inside of a machine than anyone starting out with Python or JavaScript could hope for.

Having spent 25 years to get to assembler, I wish I'd started it sooner. It's truly a lovely way to look at the machine. I'll definitely be teaching my kids how to program in assembly first (probably x86-16 using DOS as a program launcher)

  • They have to want to understand any of those things first.

    Be very careful that you're not going to just kill enthusiasm for programming as an activity entirely with this approach.

    I see this happen a lot (I did a lot of robotics/programming mentoring), and then adults wonder why their kids don't like any of the stuff they like - and the reason is that the adult was really a dick about making them learn the things the adult liked, and ignored most of the fun aspects of the activity, or the wishes of the kid.

    • > and then adults wonder why their kids don't like any of the stuff they like - and the reason is that the adult was really a dick about making them learn the things the adult liked

      This can be done with any programming language.

      The point of teaching assembly isn't for someone to memorize all the details of any particular instruction set. It's about conceiving of the decomposition of problems on that level. It's about understanding what data is, so that when the student later learns a higher-level programming language, it sets expectations for what happens when you open a file, what kind of processing has to be done, etc. It's the basis for understanding abstractions that are built upon all those 1s and 0s, about the way that a program implicitly assigns semantics to them.

      (This is best done with a toy assembly language, not one that comes anywhere near reflecting the complexity of modern CPUs. Anything to do with the practical considerations of modern optimizing compilers is also missing the point by a mile.)

      4 replies →

  • > and they have a _more_ accurate picture of the inside of a machine than anyone starting out with Python or JavaScript could hope for.

    Frankly, a more accurate picture than those starting in C have, too.

After learning asm, teach compilers and have them think about how to generate code stupidly, then think about how to generate efficient code. If you don't want people thinking about the stack, just teach them RISC rather than x86.

  • So you think people should start their programming journey by writing a compiler in assembly? What exactly should it compile, if they haven't learned any other language?

    • It's relatively common is university CS courses to build a compiler after the basic intro and architecture courses. It's one of the simpler projects (yes, really, compilers are rather simple, optimization is the hard part) that involves a lot high-level concepts and exposes a lot of the thought behind things otherwise obscure. A compiler for a simple 4-function calculator is enough to start with, then higher-level constructs can be added easily while introducing them.

      1 reply →

But if they know assembly, they can look at actual compiler output and form the correct mental models...