Eli5: how does assembly code work?

466 views

Eli5: how does assembly code work?

In: 24

11 Answers

Anonymous 0 Comments

Let’s begin by looking at a simpler, older computer component — [a 6502 processor](https://www.the8bitguy.com/wp-content/uploads/2018/06/IMG_0239.jpg). This is a pretty famous little chip. It, or some closely related version of it was included in some of the most famous early computers and video game systems, including the Apple II, Nintendo’s NES, the Commodore 64, and Atari 2600.

This funny little chip has a lot of spindly little legs. The idea with these legs is that you’re intended to, for lack of a simpler term, “shock” them with electricity. Generally, for each pin, you can either choose to shock it or leave it be. And the thing is, it’s very cleverly designed in such a way that, if you shock certain combinations of pins all at once, the chip will react by sending out shocks on some of its other legs. The combination of shocks you pump in will dictate what it pumps out in a very predictable way. This is a very crude description of how a CPU works.

When you buy one of these chips, it will come with a big fat user manual explaining to you which pins are designed to be shocked, and all the various combinations of shocks that the chip will be designed to recognize, and what behaviors they’ll trigger. Armed with this knowledge, and some kind of setup that can deliver the necessary shock combinations to the necessary pins, you can start ordering up combinations of shocks you want to send to the CPU, one after the other, in some specific order that will cause some overall effect to happen.

For example, with our little 6502 chip here, there are 8 dedicated pins that you are supposed to send shocks to that tell the chip specifically what action you want it to do. 16 more of those legs can be used to send additional data in, or get data out, if the command needs it. Two of these commands are, essentially, “remember” and “playback”. You can send a certain shock pattern to the 16 data pins, and at the same time, send the specific shock pattern to the 8 control pins that corresponds to the command, “remember”. Later, sending the chip a different control pattern that corresponds to “playback” will cause the chip to shock the 16 data pins in exactly the same way you did before.

You could queue up two “remember” commands in a row, each one storing a different pattern, then send it the command “add”, where it will take the two patterns it remembered and add them together. Then you can “playback” the result. Maybe you could extrapolate from here how this could spiral into much more complex tasks.

Lists of these simple commands are the backbone of how all computers function. I used a really old CPU for the example, but the reality is that most computer processors are just bigger, beefier, more complicated versions of this same idea. The 6502 only has 40 pins you can shock. A modern CPU like the one that’s in the device you’re using to read this probably has well over a hundred.

As for assembly, assembly is mostly a way to take these funny pin-shock combinations and writing them down in a way that’s easy for humans to read. It would be horrendously annoying to have to remember something like, “If you want to run the ‘remember’ command, then shock the first pin, not the second, not the third, shock the fourth, shock the fifth…” etc. You could condense it down to a binary representation, like “10011010” where “1” indicates a pin you shock and “0” is a pin you leave alone, but that’s still really annoying to read. It’d be much simpler if you just gave that pattern a distinct name, like “LD”, short for “load”. That would at least make the written code read (somewhat) like spoken language. You could physically read aloud all the commands and it would sound (roughly) like a spoken description of what the program does.

So, tl;dr, a computer program written in assembly language is mostly just a very glorified list of shock patterns that you intend to send to a computer chip, written in a shorthand that makes it easier for humans to read.

You are viewing 1 out of 11 answers, click here to view all answers.