eli5 How do people code with binary?

1.29K viewsOtherTechnology

Like I get that it can be a 1 or a 0, and that stores information, but how do you get information out of that? And how do people code with it? Seems like all it could store is numbers

Edit: Thank you all, this is very helpful

In: Technology

26 Answers

Anonymous 0 Comments

So, all computer chips aka CPUs actually come with a list of valid instructions that the CPU understands. These commands are very, very simple instructions like:

– ADD – two numbers together
– LEA – calculate an address in memory
– MOV – move data from memory into the CPU, or from the CPU to memory
– CMP – compare two numbers
– JMP – go to another spot in memory and start reading the instructions from there
– CALL – start a procedure
– RET – go back to where you were before the last CALL

And lots, lots more.

Now, each of these instructions has an “opcode”, basically, a number that that represents the operation. So ADD can be operation 0, LEA can be operation 141, and so on.

Now finally, each instruction can also have arguments. So the people who make CPUs also document the format to specify the arguments that tell the CPU how to do that instruction.

On a 64 bit computer like the one you have, each instrument is 64 bits long. Aka each instruction is a string of 64 1s and 0s. This is also 16 hexadecimal digits.

This is totally made up, but let’s say want to LEA 32 + the address in register 2, into register 1. Registers are basically like scratch paper your CPU gets to do math with. That instruction may look like:

8D 00 00 02 00 00 01 20

where 8D is the opcode, “00 00 02” is register 2, “00 00 01” is register 1, and “20” is 32 in hex. In binary that’s:

1000110100000000000000000000001000000000000000000000000100100000

When you run the program, these 0s and 1s will be loaded into the CPU. It’ll read it and go “oh okay calculate a memory address by taking 32, adding it to..”

Now if you just stack operations like that together you’ll be writing entire programs in binary.

Back in the SUPER olden times, people used to do this by hand. They’d get cards of rows of squares, where each row has 64 squares (okay back then it would’ve been, like, 8). They would write their code by hand, calculate the opcode and arguments, then manually punch 10110010 or whatever into the card. And they would end up with a big stack of cards and shove them through the computer. If you mispunched a card, or got them out of order, or an actual bug flew into your computer and jammed the holes (that’s where we get the word bug from) the program would break.

Then they started making assembly languages. Basically you typed MOV, LEA, CMP, JMP, ADD into a file on a computer, and an *assembler* would turn it into the numbers automatically.

Then, they started making programming languages like FORTRAN, Lisp, and C, that would take code you could write once, and compile it to the assembly language for your specific machine.

Then they used those programs to make virtual machines. And they made languages like Java or C# or Python which are programming languages that compile to a fake, universal assembly language for a virtual machine, and then another program runs the virtual assembly.

And then they made languages like Typescript, which compiles to another language called Javascript, which runs in a sandboxed engine in your web browser, which is itself a virtual program run by a program run by your operating system run by your CPU.

So we have built a lot of layers over the years but at the end of the day it’s binary that’s running

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