9 ms·
Some Assembly Required: An approachable introduction to assembly
- deleted 4y ago[deleted]
- sunpazed 4y agoGreat work! There’s a typo on the 6502 page, DNC should be DEC for the Accumulator. Started coding asm on the c64 as 10 year old kid. Would have loved resources like this! https://github.com/hackclub/some-assembly-required/tree/main/code/6502#common-instructions-explained https://github.com/hackclub/some-assembly-required/tree/main...
- dingosity 4y agoI was worried this was only going to be a discussion of x86 assembly. Pleasantly surprised it included 6502 and RISC-V. It's worth reading even if you're not a teenager.
- quackduck 4y agoI'm glad you liked it! By a teenagers' guide I meant it was written by teens (including me: I'm 17). What do you think of the warehouse analogy?
- alisonatwork 4y agoCan't speak for the OP but I really liked it. It reminded me a bit of the computer game Human Resource Machine[0], which creates a mini assembly language that you use to handle a "production line" of incoming numbers. [0] https://store.steampowered.com/app/375820/Human_Resource_Machine/ https://store.steampowered.com/app/375820/Human_Resource_Mac...
- quackduck 4y agoooh thanks for sharing that game
- sargun 4y agoI love it! There's also a mobile version apparently.
- djmips 4y agoThis is a great way to accidentally learn assembly language! I was so amused that a designer friend of mine who swore they could never learn to program solved this game and in certain cases, more optimally than myself, and I have a fair amount of experience in assembly.
- DantesKite 4y agoWhat a remarkable group of people you are. Also I didn’t realize Rollercoaster Tycoon was primarily written in Assembly. That’s nuts.
- criddell 4y agoSteve Gibson (grc.com) uses assembly for most (all?) of the software he distributes. For example, his DNS Nameserver Benchmarking utility[1] is 160 KB and when you download it, it's a single uncompressed executable and has a decent win32 UI. It will run any version of Windows going back to Windows 95. Could you even run a mid-90's era macOS or KDE+Linux executable on a current Mac or Linux machine? (I don't think Gnome even existed yet). [1]: https://www.grc.com/dns/benchmark.htm https://www.grc.com/dns/benchmark.htm
- mhh__ 4y agoNot great not terrible. Analogies aren't that useful IMO, so it's cute but might not be a useful pedagogical aid. Conceptual flaws (mainly down the line rather than introductory): One issue with it down the line is that processors spend a lot of their design budgets avoiding carting memory around. You don't have to mention that explicitly but in my skim reading I would've (if I were writing it) tried to emphasize something more akin to a postal sorting room than a warehouse as per se. Maybe that's too advanced, but I understood it when I was 18 or 19 so YMMV. One interesting demo is to see how much assembly you can fit in a single memory access latency.
- themadturk 4y agoThe warehouse analogy was great. You and your co-writers did a fantastic job.
- tejasag 4y agoThis is amazing! Love the simplicity and the art used to explain things.
- jessicard 4y agoIt was so fun working with Hack Clubbers on this project! Hack Club is a group of teenagers from all over the world, and we decided to learn assembly together from scratch and see what happened. We ended up making this guide as we learned, since a lot of the resources we were working with weren't the easiest to parse. Happy to answer any questions :)
- neverartful 4y agoBravo! Great to see stuff like this! Hopefully you made some new friends too.
- jessicard 4y agoThank you so much! It was so fun working with the teenagers in the community.
- djmips 4y agoI only skimmed but it's looking good so far! Would you like feedback or suggestions? And if so, using git issues or other? Thanks!
- jessicard 4y agoIssues and PRs are preferred - thank you so much for reading! :D
- mixedmath 4y agoThis is really wonderful. And I like how simple and clean the pages look, even through markdown+github. Cheers.
- jessicard 4y agoThank you so much!! We really wanted to try to make it live alongside the code examples in GitHub, but still feel almost e-book like. We definitely hacked around the GH Markdown a bit to achieve this. Thanks for noticing!
- howenterprisey 4y agoGood stuff. A fine reminder that illustrations go a long way; well-done on those, and the guide as a whole.
- dj_mc_merlin 4y agoGood job. Some constructive criticism: There's quite a big difficulty spike so to speak in the later section as you go towards the code examples. The beginning explains relatively simple concepts like binary, then basically jmps into an instruction table which won't make sense to anyone who hasn't done some form of programming (they would know binary then, right?). The tone is also a bit off putting, although I might be out of touch with today's teenagers. When I was learning asm as a teen I learned it from grungy 1337 h4x0r sites (well, they didn't actually use 1337speak except ironically, much like today) which were at least a great deal.. meaner? less nice?.. than the tone of the article. From what I wrote I gather my main criticism is that it doesn't know its audience. However, it seemingly _was_ produced by an actual group of teenager as per the author's HN comment: > Hack Club is a group of teenagers from all over the world, and we decided to learn assembly together from scratch and see what happened. Although it probably means a trained adult was lightly helped by some teenagers in producing it. I hope this didn't sound too harsh, the effort is very good and so are the illustrations. It needs some fleshing and perhaps a little less "yay!!". Learning assembly is not actually that fun, it's rather painful. It's what you do with it that is fun.
- mhh__ 4y ago
- parafactual 4y agoWhat do you mean by "pronoun-hedonism"?
- mhh__ 4y agoThere is a style of hedonistic lifestyle/culture that usually comes with an anime profile picture and pronouns in the twitter bio. If that is someone's true "form" then go for it, but when its fake yuck.
- zozbot234 4y agoGratuitous anime pictures are also common in 4chan tech culture, where folks just don't pay much attention to Twitter or to the pronouns fad. So you can see that either way, it just suggests a kind of non-serious/poser hedonism all around. BBS/early web culture was a lot more focused than that.
- mhh__ 4y agoI basically learned C++ and assembly programmer at the same time when I was 16 by just watching the output of gcc.godbolt.org (yes ok zoomer)
- amelius 4y agoAssembly language is actually simple stuff. You just need to read the datasheet. No difficult type systems to deal with. In the old days programming assembly language was nasty because one mistake could mean your computer had to be rebooted and you lost your work. Not anymore.
- Salgat 4y agoIt's simple in the same way C is simple. Sure it's a very small language with pretty straight forward commands, but once you try doing anything of significance it becomes extremely complicated and it's easy to do nasty things with it.
- davidhbolton 4y agoIt may be simple but that simplicity is its downfall. How many databases, compilers, modern games. OOP classes or even file handling routines do you see in assembler? Few or none at all. Writing assembly language is slow and painstaking work. You can easily get 10x the productivity by switching to C or even better in higher languages. Games were written in assembler back then because Basic was too slow. And C compilers were only starting to appear on personal computers from the late 1980s. Writing an assembler for 8-bit CPUS like 6502/Z80 was doable; I wrote a 6502 cross assembler (in Z80) so a Z80 development machine could output 6502 in 1985. It got harder with 8086 and 68000 chips.
- weinzierl 4y agoThis is like saying chess is simple stuff because you can learn how the pieces move in five minutes. If all you want is a basic understanding of how things work, that level is ok. If you want to play/program, it is a completely different story. I'd even go so far to say: Why do assembly if your goal is not to be better than the compiler, at least in some respect? Beating the compiler, now tell me that it's simple stuff.
- astrange 4y agoA compiler’s main virtue is patience, not smarts. That’s because there’s no value in smarts; the systems like Itanium designed to need them turned out to be unusable.
- faisalsayed10 4y agothis is damn cool!
- kup0 4y agoMy favorite part is the heavily commented code examples. Those are much more useful for increasing my understanding than most other resources. To me, that level of information density just works better for my brain than articles or sites that try to explain things. Something about being _in the code_ while stepping through lines one by one with explanations is just so much easier to follow. I wish more learning resources took this approach.
- jessicard 4y agoAhhhh thank you for mentioning this! This was _really_ important to us, because it was hard for us to find real code examples that worked, and when we did, they were hard to parse. It's also why we put it in GitHub, so it could live alongside the code examples :)
- kup0 4y agoYes! I think two things with code examples have a significant impact, at least for me personally- 1) the examples are complete and work and 2) line-by-line explanation. A lot of resources show a snippet of code, followed by an impenetrable paragraph that tries to explain multiple concepts from the snippet all at once. Maybe that works for others, but for me it makes it impossible to parse and overwhelms me, so nothing sticks.
- NationOfJoe 4y agoThis 100%, i often find alot of blogs and documentation really difficult to follow because they just show a snippet and no where in the doc does it cover the prerequisites to get to that point. I get you don't want to clutter the point, but start with a link to working code or an article that explains the basics. I would be happy if more articles/docs explained how this code is being called.
- peterangular 4y ago> Something about being _in the code_ while stepping through lines one by one with explanations is just so much easier to follow. I just go straight to the code. Same with SO - I'm not reading words, I'm reading code. Even with zero familiarity I find code is easier to read with inline docs vs. digging through someone's word salad. Truth is most of us aren't great technical writers. That being said I did skim the content for this resource and found it to be pretty to the point and clear-cut. Even the metaphors and extra information were applicable to providing a good primer. I wish more devs would write with the same succinct technical clarity as this and maybe I wouldn't skip the word salad as much ha
- djmips 4y agoI'd like to share an old Dylan Cuthbert blog post on his hypothesis that Assembly is actually easier for kids to learn programming. https://www.gamesindustry.biz/articles/2011-03-30-machine-code-is-for-kids-article https://www.gamesindustry.biz/articles/2011-03-30-machine-co...
- 6510 4y agothat was my experience too. An abstraction like "add" is not less complicated than "1" if both are equally new things. The number actually gives additional features. The next instruction is called "2" while in abstraction to words it is unguessable and hard to remember: unGuessAble? un_guessable? ungu? ung?
- trollied 4y agoLove this! Must admit, my favourite part of doing a CS degree was creating a CPU core from scratch (a cut down ARM, running on a FPGA), then writing asm to run on it. So satisfying knowing I made the actual CPU (at gate level) and ran my own code on it. I’d be happy to do a YouTube series covering how to do this if anyone is interested.
- throwaway_dcnt 4y agoYes please
- alisonatwork 4y agoThe article links to a cool project I never heard of that does a similar thing - From Nand to Tetris[0]. [0] https://www.nand2tetris.org/ https://www.nand2tetris.org/
- throw34 4y agoThis MIT OCW takes a student from basically nothing to an almost compliant RV32i, complete with videos, worked examples and lecture notes. https://ocw.mit.edu/courses/6-004-computation-structures-spring-2017/pages/syllabus/ https://ocw.mit.edu/courses/6-004-computation-structures-spr...
- iueotnmunto 4y agoCheck out Ben Eaters youtube channels https://www.youtube.com/channel/UCS0N5baNlQWJCUrhCEo8WlA https://www.youtube.com/channel/UCS0N5baNlQWJCUrhCEo8WlA - He builds similar, and offers a kit for sale at (from what I can tell) a reasonable price, so you can also DIY.
- 50yearsold 4y agoVery cool! Amazing work you all!
- jcims 4y agoThere was a coding competition at a local podunk BBS back in ~’91. I figured I would use it as an excuse to learn some assembly. The goal of the challenge i entered was to find all of the prime numbers between one and a million. Of course my goal with the assembly approach was speed and executable size. In both of those I smoked the competition by 3 to 4 orders of magnitude. But the scoring mechanism was heavily weighted towards lines of code and the turbo Pascal entry one even though it took about six minutes to complete. That pissed me off so much I never really messed with assembly again.
- TylerE 4y agoWell, if LoC didn’t count you could just write a gigantic multi-megabyte print statement :)
- jcims 4y agoThat's thinking outside the box! xD
- TylerE 4y agoThese days you could use a language with compile-time macro execution like nim or D and have it both ways…
- throw34 4y agoOr maybe have the object code for the executable as one static constant and jump to it on the one and only line of the program: speed, size and one line of code (maybe two depending on how the counting is done)
- jamal-kumar 4y agoGet this, an old greybeard I worked with told me managers used to harsh on LoC at companies like IBM back in those days as a measure of productivity. Like people got fired over what you lost the contest on. Insane, I can see why that would tick you off
- egypturnash 4y agoThis is making me wonder just how raw Sawyer’s code for Rollercoaster Tycoon was. The source doesn’t seem to be out there but a page on his site (http://www.chrissawyergames.com/faq3.htm http://www.chrissawyergames.com/faq3.htm) says he used “MS Macro Assembler V6.11c” so it’s probably safe to assume he didn’t type out every single instruction in the game by hand. Depending on how many macros he’d built up at this point in his career it might have felt more like programming in C, except with a lower conceptual barrier to switching to inline assembly functions when stuff needed to be optimized.
- shadowofneptune 4y agoMASM and the Intel assembler were already rather high-level compared to other assemblers, with a type system, procedures, records, etc. Those later versions of MASM added things like automatic allocation of structs on the stack and for loops. Depends on what features Sawyer used, but that assenbler in particular is a pretty advanced tool.
- darkgray 4y agoPardon, but the assembly code on the "Loop de Loop" chapter currently seems broken. "rax" is acting as all three variables somehow.
- jessicard 4y agoNice catch! Just to be clear, it was that we were incrementing our exponent but not our counter, is that right? I pushed up a fix, but let me know if there were other issues.
- darkgray 4y agoIt's a bit more involved than that. mul rcx, rax ; multiply our result by our base, save into rcx This should probably be using rbx, since that's the designated base, not rax, which is the exponent. In the explanation just above, there's a 2*8 step missing, while this one is incorrect: 4. result = 16 * 2, result is now 16 Since 2*16 is not 16.
- jessicard 4y agoAhhhhh thank you! Do you mind opening a PR or issue so I can get to that? Thank you for noticing!!
- seanalltogether 4y agoSo probably a really dumb question, but given that assembly has a limited number of operations and a limited number of registers, what are processors doing with the ever growing number of transistors added to them? What is processed with billions of transistors that can't be done with thousands?
- MasterIdiot 4y agoAmong other things - larger caches, more instructions, more cores and more busses in between them.
- ithkuil 4y ago1. Modern CPUs have a larger internal register file and dynamically remap the logical registers referenced by machine instructions into physical instructions. 2. Modern CPUs have a very complicated instruction reordering mechanism and data dependency analysis mechanism that allows CPUs to execute multiple instructions in parallel, when it's possible. 3. A lot of transistors are used for caches (instruction and data caches, and other kinds of buffers like the one used for branch prediction etc). 4. Even a single core has multiple copies of the execution units which allow to execute some instructions in parallel (see above).
- teraflop 4y agoIt's not a dumb question, but it's so broad that it would take at least an entire textbook to fully answer it. Just off the top of my head, some possibilities are: * More cores * More numerous/powerful execution units that can do e.g. vectorized math * Architectural features such as virtualization and security defenses * Bigger and more flexible caches * Complex pipelining/out-of-order/speculation logic that allows more instructions to be executed (on average) per clock cycle * Special-purpose functional units that dramatically accelerate particular applications (e.g. AES encryption, video encoding/decoding) and are idle the rest of the time * Replacing "deep" networks of logic gates with equivalent "wider" ones, which occupy more die area but have a shorter critical path, enabling faster clock speeds
- themadturk 4y agoMultiple CPUs, Pipelining, multithreading, onboard graphics, Apple's efficiency and machine learning cores... and multiple CPU cores, as well. The code demonstrated in these examples is essentially executed as a single thread in a single core. Modern CPUs are faster not only because of faster clock speeds, but more CPU cores and peripheral stuff, like cache memory and more, on each chip. Packing the transistors closer together means there's less distance to travel between, for example, the CPU and the arithmetic logic unit, or between the CPU and the on-board cache memory. And I'm sure I've barely scratched the surface.
- whoomp12342 4y agovery nice! I wouldnt give this to a programming novice but its very nice for people who know their stuff and just havent done asm yet.
- jessicard 4y agoThanks! Totally agree - you need to understand some programming basics in order to understand the examples in the code section. Things like variables and for loops.
- Existenceblinks 4y agoThe most proud of assembly program I made was Voracity game (2008). The learning process was so smooth because there are only few things to use. Mind blowing moment is when I made chain jump to workaround out of range jump.
- davb 4y agoThose illustrations are gorgeous.
- tech_tuna 4y agoLearning assembly has been on my wishlist forever. . . excited to give this repo a look see.
- itsmemattchung 4y agoDefinitely do it! Working with assembly demystified a lot of questions I had working at higher level languages (like C and Python).
- olah_1 4y agoI think Pico-8 recently added support for editing assembly operations. Also there is a book called Programming from the Ground Up that starts with assembly and moves up to C and beyond.
- cptnapalm 4y agoAnd it's inexpensive, too! Will be ordering this. Thanks!
- HPGBeans 4y agothis is really cool
- marai2 4y agoThank you!! Just yesterday I was googling trying to find a good introduction to learning assembly and today here it is!!!
- zoomablemind 4y agoGreat work, very methodical! In using assembler I find the hardest part is to represent the task at hand into a form of a sequence of elementary operations over the dynamic set of data pieces and their locations. Perhaps, when the problem scope is on a lower level with a limited sets of data and operations, this maps easier. But when the task is on a macro level, that extra step of mapping the context onto a lower level scope is just too much effort. In a way, in a higher level scope the programmers create their specific 'assembler' with a different set of registers and opcodes and sequences of operation, and even busses.
- martin1975 4y ago"Since forever ago, I've wanted to try writing assembly, even if just to understand why the Rollercoaster Tycoon creator would write 99% of the game in it. To be fair, even after all of this, I still don't understand why they did that." Hubris?
- ddingus 4y agoI doubt hubris. Frankly, depending on their experience, assembly may well be familiar, efficient and effective. Some people who got going early on in games, in the 8 bit era, went right for assembly language because that is where the speed necessary to make magic happen on the screen was. Action / arcade games pretty much demanded it, unless they sharply limited what happens on screen. You might find some of the early stories informative. Nasir, for example, did his first dozen or so games with the mini line assembler in his Apple 2. No source code, no labels, etc... https://youtu.be/S3EmtTpoEa8 https://youtu.be/S3EmtTpoEa8 On some of the projects I did on 8 bit computers, it was all about assembly. One planned out a few things, worked around where the screen was, or needed to be (depending on hardware capability), on screen graphics, other data needed, and then build in chunks, saving stuff off to disk. Every so often, load several in, tie them together, save that off, then run it. It is a different style of thinking for sure. I bet that developer knew how to build something up that way and went with what worked, was performant, and that was probably the easiest path for them.
- mNovak 4y agoI enjoyed learning assembly in college -- to me, many of the non-intuitive aspects of abstract languages make more sense when you know something like C, and C makes a lot of sense once you know assembly. It all sort of clicks in a very satisfying way.
- greyhair 4y agoHaving started my career at Bell Labs in 1984, the fact that you included AT&T X86 syntax cracked me up. If you are still interested in learning more, ARM and PowerPC are also both interesting assembly targets. Prior to Bell Labs (college) my assembly experience was 8085 bare metal (no OS) or Z80 under CP/M and MP/M. I wrote a couple console management applications for MP/M that had to dig into BIOS/BDOS routines. It was fun, and that was the thing that made my EE self realize that I wanted to work on software in embedded systems.
- Linda703 4y ago[dead]
- zzo38computer 4y agoThey mentioned Z80 but do not have the examples of Z80; maybe in future might be added. I have used some assembly language stuff, including 6502, Knuth's MIX and MMIX, and virtual codes such as Z-code and Glulx. I have written small amounts of old x86 assembly code (for the older 16-bit PC), too. (I think that the new x86 is too messy and too complicated, compared with the old x86 which is not bad.) I have been trying to learn TRON assembly code too, although I cannot find enough information. I found some documents, but they are in Japanese and some important diagrams seem to be missing. There is some English documentation too but it does not explain much.
- aasasd 4y ago> why the Rollercoaster Tycoon creator would write 99% of the game in it Not sure how true it is, but I heard that keeping track of a lot of objects required plenty of optimization, and apparently such levels of detail may have not been in games before. RCT was the second Tycoon game by Chris Sawyer, after Transport Tycoon from 1994—which is also in assembly and which keeps track of buses and trucks each with its own parameters, trains with each car, signals on the roads; of every passenger and every batch of goods: where it's from, how long it's on the way, etc. (At least if OpenTTD recreates the mechanics faithfully.) You can also pop up several windows with views to different parts of the map or following various vehicles.
- bilekas 4y agoFinished?
- tomcam 4y agoWonderful. Love the rich comments in the code.
- banashark 4y agoThis is great! As a developer who didn't learn via a scholastic setting, taking the time to sit down and learn a bit of assembly was something I see as a critical point in my career. Starting with a high level scripting language, my brain had a hard time dealing with so much abstraction and taking what felt like a crazy amount of axioms as given. I took a few months to sit down and go through the excellent book Programming From the Ground Up. After doing some bit twiddling, writing a basic allocator, and a handful of the other exercises from the book, I felt way more prepared to handle abstractions higher up. Anything that I couldn't infer based on the underlying knowledge was something I could google, then quickly form the missing links between starting from the bottom. I feel like learning C could give the same benefits, though I do hear some struggle with pointers, and in assembly you get to look face first at what they actually are. Everyone has different levels of abstraction that they're comfortable with as well. Assembly was enough to fill the seeming void of knowledge that I felt was holding me back, but for others it may be higher depending on which aspects are puzzling (memory management, intermediate representations, etc). I also appreciate how much information modern VM authors publish about the inner workings of their platforms. It helps a lot in the same manner to reason about the systems (v8, JVM, CLR, etc).
- GaNuongLaChanh 4y agoThanks for good resources about Assembly, I’m learning source code of AsmBB [1] - a forum written in assembly x86 :D [1] https://board.asm32.info/ https://board.asm32.info/
- MR4D 4y agoThis is fantastic! I’m definitely not an assembly guy, but it would be fun if there was a REPL to play with this. Not sure if that would accidentally invert the universe or it’s too low level for a REPL, but conceptually it would be fun to play with in a sandbox.