Eli5: how does assembly code work?

484 views

Eli5: how does assembly code work?

In: 24

11 Answers

Anonymous 0 Comments

Its a programming language like all the others, its just low level. The means that all the different instructions are pretty close to what the computer is actually doing. The more common languages are high level. They look closer to something a person can read and understand, but have to be translated for the computer to read. Assembly keeps it very close to the level the computer understands.

Anonymous 0 Comments

A computer is a machine that only knows how to follow a list of basic instructions.
It can read values from memory, do basic math on them, and store the results. It can also compare two values and jump to a different place in the instruction list based on the result.

The computer uses numbers to represent these instructions. So for example, 1 means load and 2 means add, etc. A computer program is stored as a list of numbers.

Assembly language is just a simple translation of these numeric instructions into readable words like MOVE and ADD. When you write assembly, you are writing directly in the basic instructions the CPU understands, but with codewords instead of numbers.

On the other hand, high level programming languages let you write English like instructions for advanced operations, like printing to the screen or connecting to another computer. The compiler or interpreter does the job of translating these into the sequence of assembly code that the computer can actually follow.

Anonymous 0 Comments

Assembly code is simply a human- readable version of the computers fundamental instructions.

For example, a single instruction is 16 bits, organized like this ( I’m making this up, it does not match any real cpu):

Operation 4 bits (0000 = get, 0001 = put, 0010 = add, sub, compare, …)

Source register 2 bits (registers 00, 01, 10, 11)

Destination register 2 bits

Memory address 8 bits (any 8 bit number)

So a possible instruction word might be

0010 01 10 00001110

In assembly, this would be written

ADD R1 R2 0E

and what it does is add the number in R1 to the number in R2 and store the result in memory location 0E (hex, which is 14 decimal).

An “Assembler” is a program that can input that ADD instruction (and thousands more in your program) and turn it into the corresponding binary. You end up with a long list of binary instructions, which is basically what your “foo.exe” program is, and the computer can execute those directly.

Anonymous 0 Comments

It works the same way any other programming languages work, just at a lot more basic level.

Assembly language is the closest you can get to how computers work while still being human readable, it directly translates to straight up numbers. Each line in assembly translates to a cpu instruction.

These are extremely basic operations that involve logic like other code (although more basic), basic math, or stuff like moving memory around. Something like adding 2 not numbers together could be as much as 4 lines of code (I’m not exaggerating). You have to do every little thing, like moving information from memory into the cpu and back.

Assembly is cpu specific so different cpus could have different commands.

Anonymous 0 Comments

Every processor has a certain set of operations that it’s hardwired to perform – these operations are known as the processor’s “instruction set”. Internally these instructions are referred to by binary numbers because at the end of the day that’s all a computer is really able to work with. For example, 0011 might be the operation code for addition, 0100 might be the operation for subtraction, etc.

The problem is that referencing everything as a binary address like that is pretty tedious for humans and prone to error. So clever people started making human readable abbreviations for popular instruction sets – 0011 got abbreviated to ADD, 0100 got abbreviated to SUB, etc.

Then people wrote programs called “assemblers” that could take those abbreviations and automatically translate them in to the corresponding binary. And hence the concept of “assembly language” was born.

ADDENDUM: If you want an easy to follow step-by-step example of how a computer actually executes commands like this look up the “Little Man Computer” on Wikipedia.

Anonymous 0 Comments

A computer runs instructions coded in binary (machine code). The available instructions usually involve registers, such as:

– Put the number 53 in the D register
– Add the contents of the A register and the C register, and store the result in the A register
– Store the contents of the E register in memory location 46
– Load the contents of memory location 30 into the B register

This all depends heavily on what CPU you’re talking about. Different manufacturers and different CPU models have different instructions available, how they’re coded in binary, what registers are available, what the registers are called, and so on.

The companies that make CPU’s usually write a (several hundred pages long) manual that tells you all of this information, and they usually put such manuals online for anyone to download a copy for free. (They have incentive to do this: You need to buy the company’s physical product — the CPU they sell — to make practical use of the information in the manual. And if you want this information, you’re a programmer writing a program for their CPU, which means the existence of your program is going to make their CPU more valuable for all of their customers.)

Assembly language is a human readable notation that’s (mostly) a 1-for-1 translation of the machine code of some specific CPU.

Here’s an example:

– Machine code: `10010100`
– Assembly language: `sub a,h`

Both mean “Take the value of the A register and subtract the value of the H register, then store the result in the A register.”

The Machine Code is completely impossible to interpret (unless you’ve memorized the manual), but you can much more easily train yourself to understand Assembly Language. That’s the main benefit of using assembly language.

Every CPU has its own machine code, and therefore its own assembly language.

You can convert machine code to assembly language and vice versa with nothing more than your brain, pencil and paper. You just need to look up how the instructions are coded in the manual.

It’s possible to write programs this way, but extremely tedious. If you have a computer, you would probably much rather have a computer program do the translating for you. This program is called an *assembler* (if it translates from instructions to machine code) or a *disassembler* (if it translates machine code to instructions).

Anonymous 0 Comments

Imagine you have a long tape. The tape consists of boxes that you can write numbers in. Different number represents different actions you can take at each time. Next you have a clock. Starting from the beginning of the tape, for every time the clock ticks, you point your finger at the current box and perform the action as presented in the box. After you perform the action, you move to the next box.

The kind of actions you can take is predetermined by the computer architecture you are on, like x86 or ARM

Anonymous 0 Comments

Longer than an ELI5 answer, but [there’s a really nice set of videos that walk someone with a little bit of technical understanding exactly how it works.](https://www.youtube.com/watch?v=LnzuMJLZRdU&list=PLowKtXNTBypFbtuVMUVXNR0z1mu7dp7eH&index=1)

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.

Anonymous 0 Comments

The CPU of a computer speaks a particular language (machine language) that is hard-wired onto the chip, and is therefore extremely fast, but only a few dozen instructions are available.

Assembly language is a human-friendly (relatively speaking) version of machine language that is easily and directly translatable to it.

The upside of assembly is that you have the full power of the chip at your fingertips, and can accomplish certain tasks at a blazingly fast speed.

The downside is that it can take thousands of lines of code to actually accomplish anything but the very simplest of tasks.

Higher-level languages abstract away from the assembly level, then rely on translation programs to convert programs to machine language, so the chip knows what to do.

Those translation programs may or may not optimize your code in a way that meets your needs, so some higher-level programming languages allow you to write some of your code in assembly, for those times when you want to do things YOUR way.