News for hackers. Stuff that matters.
Re:Next steps for Rust in the kernel by dijit on Wednesday September 21, 2022 @09:03AM (#32924535)
C will teach you how computers think.

Rust is it's own thing; I personally really like it but going from C to Rust is harder than just going to Rust as you have to "unlearn" C paradigms.

You'll probably be "better" if you go C first, but it will take a lot of time and energy.

In the end its completely up to you.

Re:Next steps for Rust in the kernel by Twisol on Wednesday September 21, 2022 @09:06AM (#32924595)
> C will teach you how computers think.

It will teach you how a PDP-11 thinks, at best. The cache hierarchy and speculative execution are completely hidden at the level of C, to start with.

You want to learn how computers think, pick up an assembly language. (Any one will do.) You want to build portable low-level software, learn Rust, Zig, C, or C++ (in my personal order of preference).

Re:Next steps for Rust in the kernel by rob74 on Wednesday September 21, 2022 @09:41AM (#32925013)
Cache hierarchy, branch prediction, speculative execution and all of the other performance enhancements of modern CPUs are hidden from assembly too. If you are a real whiz, you may be able to hand-tune your assembly code so that it takes better advantage of one of these features and runs 0.1% faster, but otherwise these features are transparent even to assembly programmers.
Re:Next steps for Rust in the kernel by Twisol on Wednesday September 21, 2022 @02:28PM (#32929215)
Just to mention it here, too (I wrote more in a cousin comment), Intel's own Software Development Manuals [0] explicitly talks about all three of these things. I don't consider these features hidden when the primary reference for programming this platform clearly documents and provides advice on them.

[0] https://www.intel.com/content/www/us/en/developer/articles/t...

Re:Next steps for Rust in the kernel by nextaccountic on Wednesday September 21, 2022 @05:06PM (#32931532)
> 0.1% faster,

The performance gains of fine-tuned code is typically two orders of magnitude higher

Re:Next steps for Rust in the kernel by kjeetgill on Wednesday September 21, 2022 @09:19AM (#32924762)
The cache hierarchy and speculative execution are completely hidden at the level of assembly too, so I'm not sure what you're getting at.
Re:Next steps for Rust in the kernel by Twisol on Wednesday September 21, 2022 @02:23PM (#32929128)
> The cache hierarchy [...] are completely hidden at the level of assembly too

A quick glance at Intel's Software Developer's Manuals [0] falsifies this:

>> TLB and Cacheability control: CLFLUSH, CLFLUSHOPT, CLWB, INVD, WBINVD, INVLPG, INVPCID, and memory instructions with a non-temporal hint (V/MOVNTDQA, V/MOVNTDQ, V/MOVNTI, V/MOVNTPD, V/MOVNTPS, V/MOVNTQ, V/MASKMOVQ, and V/MASKMOVDQU).

> [..] speculative execution are completely hidden at the level of assembly too

Section 18.1.13 specifically mentions side-channels for speculative execution, and there are more than 100 other matches for "speculative" across the document (some of which also refer to load barriers).

So no, these things are not completely hidden at the assembly level, and at least in the case of the (or one of the?) most popular consumer CPU architecture in the world, they are actively documented in the primary reference for an assembly programmer.

[0] https://www.intel.com/content/www/us/en/developer/articles/t...

Re:Next steps for Rust in the kernel by cbm-vic-20 on Wednesday September 21, 2022 @10:24AM (#32925572)
This is true even on later PDP-11 models with memory caches.
Re:Next steps for Rust in the kernel by packetlost on Wednesday September 21, 2022 @09:36AM (#32924956)
You're much more likely to get bitten by those 2 things at the ASM level... I guess.
Re:Next steps for Rust in the kernel by kjeetgill on Wednesday September 21, 2022 @09:42AM (#32925031)
So I don't work at that level, I'd love to hear more. What situations do they leak through in?
Re:Next steps for Rust in the kernel by packetlost on Wednesday September 21, 2022 @09:50AM (#32925137)
you'll mostly notice performance problems if you're not aware of cache lines when writing concurrent software, though it's relevant whenever you read from main memory. As for speculative execution, you probably won't really encounter issues related to that unless you go out of your way to. I don't believe it can actually cause problems in programs (though it is helpful for exfiltrating otherwise secret data).
Re:Next steps for Rust in the kernel by marcosdumay on Wednesday September 21, 2022 @10:10AM (#32925399)
> It will teach you how a PDP-11 thinks, at best. The cache hierarchy and speculative execution are completely hidden at the level of C, to start with.

And error recovery (that is fucking crazy on x86), and wide instructions, and I/O, and stack management, and... I am not sure C is even a good approximation for the PDP-11.

Re:Next steps for Rust in the kernel by dboreham on Wednesday September 21, 2022 @10:20AM (#32925526)
All computers are pdp-11s.
Re:Next steps for Rust in the kernel by antegamisou on Wednesday September 21, 2022 @09:09AM (#32924634)
> You want to learn how computers think, pick up an assembly language.

For which you'd need to be familiar with a low-level language like C.

Re:Next steps for Rust in the kernel by ImprobableTruth on Wednesday September 21, 2022 @09:20AM (#32924768)
... no? In fact I think learning assembly before C makes a lot more sense than the other way around.
Re:Next steps for Rust in the kernel by orobinson on Wednesday September 21, 2022 @09:45AM (#32925072)
This. The computer science degree I did started off in the first term with us building computers on an FPGA by mapping out the logic gates, then programming it in assembly. Learning how to program in C followed this.

It's a good way of helping people with no programming knowledge get past the stage of "what are those magic colourful words you type that somehow make the computer do things?".

Re:Next steps for Rust in the kernel by stjohnswarts on Wednesday September 21, 2022 @10:07PM (#32934062)
I think this is an awful way of learning for beginning CS students. It will scare off too many good kids who think this is all that CS will ever be. Top down is better. Bottom up is only going to be good for people who adore computer architecture guts and want to start at the bottom from first principals. Most kids want to use computers as a tool and not learn it's atomic structure.
Re:Next steps for Rust in the kernel by stjohnswarts on Wednesday September 21, 2022 @10:05PM (#32934051)
Unless you're experienced well in some other language then assembly is going to scare you off. I learned assembly by looking at -O0 output from c compiler code. Then I went ahead and dived in. Just doing bare assembly is not somewhere a beginner should start unless they are just absolutely in love with low level programming.
Re:Next steps for Rust in the kernel by jcranmer on Wednesday September 21, 2022 @09:17AM (#32924738)
No, you don't. In the curriculum I've taught, assembly was taught before C. As long as you have any programming language experience, you should be fine with assembly--I've explained it in terms of even MATLAB before.
Re:Next steps for Rust in the kernel by Filligree on Wednesday September 21, 2022 @09:19AM (#32924759)
Assembly was my first language. I had no trouble picking that up, and in fact didn't meet a compiler until ten years later. They were much too expensive at the time.
Re:Next steps for Rust in the kernel by HideousKojima on Wednesday September 21, 2022 @10:27AM (#32925616)
I learned CHIP-8 asm and Gameboy asm before ever touching C
Re:Next steps for Rust in the kernel by pwpw on Wednesday September 21, 2022 @10:36AM (#32925786)
I want to learn how to build emulators for CHIP-8 and Gameboy and am having the person tug-of-war over assembly, C, or Rust. With hindsight, do you recommend doing them in assembly? And what resources did you use to get started?
Re:Next steps for Rust in the kernel by HideousKojima on Wednesday September 21, 2022 @10:58AM (#32926095)
I wrote my CHIP-8 emulator in C#, actually. I used the Godot game engine for rendering and sound (though I never actually got around to implementing sound). The language you write it in really doesn't matter too much unless you really care about performance, or if you want to do something fancier than an interpreter (like a JIT recompiler). I used this guide:

https://tobiasvl.github.io/blog/write-a-chip-8-emulator/

I haven't made a Gameboy emulator, just learned the assembly so I could modify some roms.

Re:Next steps for Rust in the kernel by gotlou on Thursday September 22, 2022 @09:16AM (#32938381)
I wrote a CHIP-8 emulator[0] a while back in C using SDL2 for rendering everything. It still has a couple issues with the timers and taking input. I was new to C back then so I did this project to become more familiar with it.

You can check out my blog post[1] if you're interested how I went about it (there are a couple resources at the bottom I used to build it, including the one that HideousKojima recommended).

As for language, in hindsight, C was fine. CHIP-8 is so basic that you don't really need to worry about performance or to implement a JIT compiler unless you want to learn how to do those things specifically. Just pick any one out of the three (assembly is a bit of a weird choice though, why not write a CHIP-8 emulator and then a brand new program to run on that emulator in CHIP-8 assembly if you want to learn assembly?)

As for Gameboy, it would have more instructions, a different graphics system, sound etc. and overall be more complicated than CHIP-8. Try it out if you either feel a bit more adventurous or have implemented CHIP-8.

[0](https://sr.ht/~gotlou/chip8-emulator) [1](https://gotlou.srht.site/chip8-emulator.html)

Re:Next steps for Rust in the kernel by nindalf on Wednesday September 21, 2022 @09:07AM (#32924599)
I don’t think this is true.

Check this post out - Should you learn C to "learn how the computer works"? (https://steveklabnik.com/writing/should-you-learn-c-to-learn...). Spoiler alert - no.

Re:Next steps for Rust in the kernel by kjeetgill on Wednesday September 21, 2022 @09:15AM (#32924710)
> C is not “how the computer works.” I don’t think most people mean this phrase literally, so that is sort of irrelevant. Understanding the context means that learning C for this reason may still be a good idea for you, depending on your objectives.

I think summarizing that article as "no" really undercuts how diplomatic and reasonable Steve Klabnik is in it.

Re:Next steps for Rust in the kernel by steveklabnik on Wednesday September 21, 2022 @11:26AM (#32926466)
Thank you for the kind words. I got enough heat for it at the time that it took me a full year to return to the topic, so it's nice that some folks still remember it fondly.
Re:Next steps for Rust in the kernel by cannabis_sam on Wednesday September 21, 2022 @01:29PM (#32928398)
Your views on programming languages are deeply appreciated, regardless of whether I agree or not.
Re:Next steps for Rust in the kernel by nindalf on Wednesday September 21, 2022 @09:18AM (#32924741)
Of course learning widely used technologies may be a good idea for some people, depending on their objectives. That’s a truism.

But is it necessary to understand now a computer works? No.

You can also check out "C is how the computer works" is a dangerous mindset for C programmers (https://steveklabnik.com/writing/c-is-how-the-computer-works...)

Re:Next steps for Rust in the kernel by dijit on Wednesday September 21, 2022 @09:11AM (#32924662)
Steve Klabnik has done a lot for systems programming and that post is a lot more even-handed than you're suggesting.

If, however, we're talking about the kernel then perhaps we should listen to what Torvalds says about this topic.

"People who designed C, designed it at a time where the language had to be geared towards the output, so when I read C I can think about the Assembly that it will create":

http://www.youtube.com/watch?v=MShbP3OpASA&t=20m45s

Consider that he said this in a time with C++, even if Rust was in its infancy: it is not as tied to the assembly it produces. Not by a long shot.

Re:Next steps for Rust in the kernel by TrickardRixx on Wednesday September 21, 2022 @09:19AM (#32924761)
Sounds to me like if I want to know "how computers think" I should learn assembly.
Re:Next steps for Rust in the kernel by dijit on Wednesday September 21, 2022 @09:26AM (#32924832)
I know you're trying to be a smart-arse; but by virtue of using C you will learn Assembly.

The issue with Assembly directly is that it's absurdly non-portable, too low level and too unstructured to do anything reasonable with it.

There's a reason that Linux is written in C and not ASM Directly.

Re:Next steps for Rust in the kernel by jcranmer on Wednesday September 21, 2022 @10:22AM (#32925545)
> by virtue of using C you will learn Assembly.

Even interpreting this as charitably as possible--that you don't mean literally learning assembly but rather learning everything besides specific assembly syntax that you would have picked up from learning assembly--this claim doesn't hold water.

For starters, C isn't that much closer to hardware than, say, Java. The main differences you have with C are that a) C has a pointers-are-almost-integers model [1] and b) C has a more limited runtime than other languages. C is still a fundamentally based on an abstract machine semantics model, and that abstract machine doesn't have a close bearing on modern hardware.

In particular, C does not distinguish between registers and memory, and if I had to pick the single most salient feature of modern hardware you need to understand well to be able to say you understand how computers work, it's the register/memory distinction. If you think processors are mostly like they were in the 1970s--when you could say "add 1 to this memory location"--then C's shrugging off the register/memory distinction makes sense, but this is a situation that hasn't been true for several decades.

Another failure of C is that it's far less expressive than assembly. There are features in other languages that are impossible to express in C. How do you write a function with multiple entry points, as you can in Fortran? Or a coroutine, as you can in Algol? Or nested functions, as in Pascal? Or try-catch, as in C++? Or functions with multiple return values, as in Matlab? Indeed, how do you write something like JMP *reg in C, a useful primitive for state machines?

No, C does not come close to being a good description of modern processors, where "modern" means "anything that came out since I was born."

[1] Not going to open up the can of worms that is pointer provenance.

Re:Next steps for Rust in the kernel by cesarb on Wednesday September 21, 2022 @10:41AM (#32925852)
> C does not distinguish between registers and memory

Just nitpicking, but doesn't C have the register keyword? (I know it does nothing at all nowadays, but AFAIK it did make a slight difference back in the dark ages.)

Re:Next steps for Rust in the kernel by tialaramex on Wednesday September 21, 2022 @12:57PM (#32927958)
C's register keyword is a storage specifier, it actually does do something, what it does is announce "This might not have an address" (your CPU registers don't have addresses) and so you can't make a pointer to it with the unary & address operator.

Your C compiler might conclude - even if it doesn't decide to put the variable in a register - that since the variable notionally doesn't have an address some optimisations are available which it can't otherwise be sure are safe.

Re:Next steps for Rust in the kernel by jcranmer on Wednesday September 21, 2022 @10:50AM (#32925988)
Considering that `volatile register _Atomic int x;` is a totally legal statement, and two [1] of those keywords have semantics that are only relevant for memory operations, I'd say that the "register" keyword isn't sufficient to distinguish register from memory.

[1] Okay, the official semantics of "volatile" strictly speaking are completely orthogonal to memory, but the practical effect that everyone agrees on (don't optimize this memory access) is inherently oriented towards memory, and the fact that the semantics are so vague about what it actually does is partially a reflection of the fact the semantics themselves don't distinguish between register and memory.

Re:Next steps for Rust in the kernel by UncleMeat on Wednesday September 21, 2022 @09:46AM (#32925086)
> by virtue of using C you will learn Assembly.

I don't believe this at all. You won't learn how to deal with limited numbers of registers. You won't learn about method prologues and epilogues. You won't learn about oodles of stuff.

Re:Next steps for Rust in the kernel by Jensson on Wednesday September 21, 2022 @10:02AM (#32925301)
> You won't learn how to deal with limited numbers of registers. You won't learn about method prologues and epilogues.

Those things takes like an hour to learn, translating C to assembly is trivial, it takes time and will be verbose but it isn't hard at all. So even though you don't learn all the details you still learn all the higher level stuff related to assembly programming when you program in C.

This isn't true for other languages, translating arbitrary C++ or Java or Javascript to assembly is really hard, so they don't teach you to think like an assembly programmer.

Re:Next steps for Rust in the kernel by dijit on Wednesday September 21, 2022 @10:05AM (#32925337)
> deal with limited numbers of registers

I mean; you will likely end up stepping through debuggers where you will see registers being used. At least if you learned C like I learned C.

Similarly for Prologue and Epilogue.

"why does a function call have a cost" is a common question and it's solved forever after an hour googling.