handmade.network » Forums » Help learning ASM and understanding the CPU
3 posts
#13161 Help learning ASM and understanding the CPU
3 weeks ago

Would anyone like to share some advice and/or resources for learning assembly language?
Am I correct in assuming that learning ASM is the best way to (as a programmer) build my mental model of what the CPU actually does?

I'm a completely self-taught programmer with no IRL friends that program. I'm quite comfortable with C, have written some non-trivial software, and agree through experience with much of the handmade ideology. Coming from higher-level languages (python, et al), I've wasted a lot of time reading terrible books. I would like to escape the trap of thinking about programming problems in terms of language constructs.
Tristan Dannenberg
7 posts

Simultaneously a dreamer and an early quitter

#13164 Help learning ASM and understanding the CPU
2 weeks, 6 days ago Edited by Tristan Dannenberg on Sept. 27, 2017, 9:30 p.m.

Personally, I learned programming in assembly language quite early - about two years after I started programming in general - and I don't feel like I really understood what CPUs do, or what a clock cycle even is, until going deeper to the electrical engineering level.

Ben Eater's video series on building an 8-bit breadboard computer really helped, however. You don't really have to understand every single thing in there - I blanked out on most of the actual electronics he does, too - but looking at data moving between modules as the clock ticks was invaluable, I think.

This just sets up the basis for learning the higher level constructs in modern chips, though. From there you can go on to learn about pipelining and branch prediction. I read a very similar article about various cache designs, though I can't seem to find it anymore - sorry! Maybe someone else can help out here. (EDIT: Might have been either this or this, actually. I only skimmed those, I'm not sure they're as good as the article I actually mean).

Then there's super-scalar execution and out-of-order execution, which I'm still wrapping my own head around. The latter is especially mind-blowing. This open-source design document for an out-of-order core was recommended to me just today, so I can't speak to its educational quality, but that's what I'll be spending the next couple evenings with, presumably.

Of course, I knew assembly going into all this; it might be the wrong approach for someone who doesn't. If you're having trouble making the connection with programming in a compiled or even interpreted language, don't sweat it. Find an introduction to assembly, toy around with it for a couple days, and try again. This stuff is kind of different from "normal" programming, and kind of hard to learn. You just gotta keep at it.

If you're gonna do that, though, I'd recommend learning an outdated ASM like 6502, which I'm told is very nice, or at least something like ARMv4, as I did. The x86 instruction set is kind of a legacy-ridden mess, and there's two competing syntaxes, which can get very confusing.
2 posts
#13166 Help learning ASM and understanding the CPU
2 weeks, 6 days ago

This is a fantastic 6/12 week course that will show you how logic gates build up to form the various parts of a CPU and Memory, which in turn give you a solid understanding of what assembly and machine code is doing. If you take the second part, you then get to see how to build a compiler and a "higher" level language that compiles down to the assembly.

37 posts
#13171 Help learning ASM and understanding the CPU
2 weeks, 5 days ago

Learning assembly is great fun, and can certainly be an eye-opening experience, although it's worth bearing in mind that modern assemblers are still not a 1-1 mapping onto the hardware, as they can contain composite instructions, assembly directives, and other things that don't necessarily map directly onto the hardware.

Finding good resources for learning assembly can be tricky, and it can be difficult to even find a good setup for experimenting with assembly. The first resource I would suggest is the Godbolt Compiler Explorer, which allows you to type in C (or another compiled language) and see what assembly the compiler generates. This can be a great way of learning how you would write the kinds of structured code you may be used to from higher level languages in assembly. Remember to pass in the O3 flag too to see what your code really boils down to!

If you have a Raspberry Pi, I highly recommend the Baking Pi online tutorial. These lessons take you through writing a simple bare metal OS on the original Raspberry Pi entirely in ARMv6 assembly.

More broadly, regarding resources for building your mental model of how computers work, I highly recommend the book But How Do It Know?. It has nothing to do with learning modern CPU architectures or anything, but is great at presenting the fundamentals in an approachable way. For more specific and practical information, there are various articles online. One that springs to mind as being an excellent resources for learning about memory is What every programmer should know about memory by Ulrich Drepper.
1 posts
#13174 Help learning ASM and understanding the CPU
2 weeks, 5 days ago

I'm currently doing the same thing and I was recommended to get my hands on some second-hand books by Rodnay Zaks (6502, Z80, and 6809.)

I can recommend them, they're a really good read.

There's a really good article here as well where someone implements an 8-bit machine in JavaScript:

1 posts
#13176 Help learning ASM and understanding the CPU
2 weeks, 5 days ago Edited by on Sept. 29, 2017, 12:49 a.m.

I began by learning to build redstone computers:

I learned x64 assembly:

And built a NES emulator:

This is an interesting article an modern microprocessor:

More CPU building:
(Not sure how good a learning resource this one is, as I already knew what he was teaching)
岩倉 澪
113 posts
1 project

riscy.tv host

#13177 Help learning ASM and understanding the CPU
2 weeks, 5 days ago

I'm surprised no one has suggested RISC-V here yet as a simple architecture to learn from. IMHO RISC-V is the perfect architecture to learn ASM and understand the CPU. The architecture is open, and you can not only study the ISA but also study open source hardware implementations to gain a deep understanding of what the hardware is doing on an electrical engineering level. There are already multiple books about RISC-V and the specifications go into detail on why certain design decisions were made as well. You can play around with actual hardware today as well with the HiFive1 from SiFive, or other projects using their FE310 chips.

I recommend reading the RISC-V specifications, picking up a few books like the new RISC-V atlas, the book Code is also great (not RISC-V specific), and for more in-depth study the RISC-V editions of COAD and CA:AQA.
You can also study the disassembly of a compiler that can target RISC-V such as GCC, or play around with an FPGA and/or actual hardware such as the HiFive1.

[Shameless Plug]
On RISCY BUSINESS we have been studying the RISC-V architecture in-depth by reading the software demos included with the Freedom E SDK and making sure we understand every line of code on the deepest level, we reference the specifications heavily, and also read the RISC-V related books on a Book Club side series. When we finish studying the architecture we will be writing software in ASM for quite a while to build up our ASM chops before moving on to more practical tooling-related projects for the architecture.

You can find the series right here on the Handmade Network under the projects section or directly at riscy.handmade.network
[/Shameless Plug]

If you'd like a convenient place to find all the stuff relating to learning about RISC-V, I have a forum post here on the handmade network with a list of useful links, it has all the books, the specs, links to buy FPGAs and the HiFive1, etc. You can reach it at links.riscy.tv (redirects back to the handmade network forum post in question)
6 posts
#13204 Help learning ASM and understanding the CPU
2 weeks ago

It took me a while to "get" it all. From having an understanding of how transistors operate to how those transistors can be used to build logic gates, to understanding how data is moved around etcetera...

I don't often recommend books, but this one I'd recommend in a heartbeat: http://www.buthowdoitknow.com/

This does not teach you assembly, but it will give you a complete understanding of how a processor works and touches on some elementary assembly.

Personally, I learned about assembly before I understood the architecture of processors and I honestly think that it hampered my understanding. I'd highly recommended that book. It's very cheap, and will give you a very solid foundation to build your other knowledge on top of.

Good luck!
3 posts
#13211 Help learning ASM and understanding the CPU
1 week, 5 days ago

Wow, thanks everyone! There's a truckload of interesting resources and advice in this thread now. I've already begun to work my way through.