I heard that the computer only understands numbers in base two (binary) for years. Recently, I wanted to understand in depth how my machine was working by stopping high level scripting languages and read few Assembly. I understood that the goal of this language was for the CPU to be fast: simple for it, the CPU can easily decode assembly instructions. Sorry if my question is not clear, I will try to clear the doubts: How does the CPU converts assembly to binary ? My question may be senseless, because the computer also plays with electricity, in this case, my question would be: What does it mean: "The computer only understands binary" ?
-
The same way it converts any programming language to binary. It compiles it. – apokryfos Sep 09 '16 at 12:06
-
1http://stackoverflow.com/documentation/assembly/1358/introduction-to-assembly/8901/machine-code#t=201609091238377164881 – Ped7g Sep 09 '16 at 12:39
-
Highly related: http://stackoverflow.com/questions/12007435/how-does-hardware-run-assembly/12008470#12008470 and even more so: [How does an assembly instruction turn into voltage changes on the CPU?](http://stackoverflow.com/questions/3706022/how-does-an-assembly-instruction-turn-into-voltage-changes-on-the-cpu). I'm not sure either of those are an exact duplicate, though, so I didn't actually close this as a duplicate. You seem to have missed the step of running an assembler on asm source code text to produce binary machine code. – Peter Cordes Sep 09 '16 at 14:44
4 Answers
The assembly language is just a middle representation between something a human can read (opcodes) and something the machine can read (binary electrical symbols).
When you write a program in assembly, your assembler program translates it into binary, this binary representation gets stored into some support (hard drive, floppy disk, tapes.... etc).
When the computer executes your binary, here's (basically) what goes on:
- The program in its binary format is fetched from the drive and loaded into RAM ;
- The instructions are loaded one by one into the CPU from RAM, and the binary form of the instruction (
10110 = current, no current, current, current, no current
) triggers some actions into the internal circuitry of the CPU (it might trigger for example an addition, or a load or whatever other operation supported by the CPU circuitry).
The best way to learn about that is trying to build a very simple 4-bits processor (with let's say 4 operations and 16 bytes of RAM). There are plenty of tutorials in the Internet.
What does it means: "The computer only understands binary" ?
It simply means that the CPU only understands in its logical gates "instructions" in the form : {current, no current} and each specific serie of these symbols triggers a specific action into some part of the CPU and provides some result.

- 1,270
- 10
- 24
"The computer only understands binary" is an oversimplification of reality. The CPU (lets limit it to just the CPU for now), is a very complicated piece of hardware. It consists of modules like the arithmetic and logic unit (ALU) and the control unit (CU).
Now a program which the CPU can "understand" is only in binary, now to understand what this means one needs to understand how memory works fundamentally. Memory is constructed using transistors which are used to create logic gates (more complication hidden here). The way memory works is like it's in a constant loop. If you feed current through it (set its value to 1), then it will keep feeding it back to itself as long as there's power. When this happens we say that that piece of memory is holding a value of 1 (because there's current). If there's no current then the memory location has a value of 0 (no current).
Now "running" a program means feeding the memory contents of a block of memory (containing the program) into the CPU. There's an entry point for the program and from that blocks of pulses of power/no power (0s and 1s) start flowing into the CPU based on memory contents. Based on what the CPU is getting in the input, and the CPU architecture internally, the current will follow a different path and on the output of the CPU the result will vary depending on what the input is. How each CPU does this is very long and complicated (https://en.wikipedia.org/wiki/Processor_design for how CPUs look like internally).
Now if you write something in assembly, it goes through a specialised program which translates what you wrote in an equivalent binary representation.
For example if you write something like ADD 2, 3
then the assembler will translate that into the stream of 0s and 1s which when passed through the CPU will result in a 5 (or the binary representation of a 5)

- 38,771
- 9
- 70
- 114
The process of converting assembly language code to a binary file is called encoding. Let's suppose that you program microcontrollers and you have an STM32F0xxxx μC, which embeds a Cortex-M0 processor. This processor's architecture is ARMv6-M. You can refer to a document called Architecture Reference Manual https://static.docs.arm.com/ddi0419/d/DDI0419D_armv6m_arm.pdf for instruction encodings.
Example: mov r0, #10
Open the manual and find the correct chapter. For our instruction, MOV (immediate) it's A6.7.40 - there you can see that the instruction frame looks like this:
bits 15:11 - 0b00100
bits 10:8 - Rd
bits 7:0 - imm8
You have to specify your destination register in Rd field and the value that you want to put in it in imm8 field. In our case Rd is r0, so Rd=0b000 and the value is 10, so imm8=0b00001010 and the whole instruction after encoding is 0b0010000000001010 = 0x200A

- 43
- 2
- 6
I will try to focus on the question that what do we mean when we say computer only understands binary
First let's take this example
Say you have to store a letter 'g' in the machine First we convert it to it's ASCII value that is 103 Now binary equivalent of 103 is 1100111
Now we don't have any mechanism to store this number in a machine.
We figured a way out to remember this using machine or in other words collection of electric wires
We placed 7 parallel wires and let the current flow from the wires where we see 1 and leave other wires with no current specifically in the order of occurrence of bits i.e 0 or 1
Now every time I see this pattern I know this is the English letter g.
Now we can assume our storage devices or processors to be a huge network of such wires that are synergistically arranged to make our logics work
Please note that I have taken a oversimplified approach just it make it more shuttle and clear actual implementation is obviously much more complex as explained in other answers but the base line remains the same
Hope this helps

- 887
- 1
- 7
- 9