How do machines interpret binary?
Asked Answered
M

4

5

I was just thinking, how do machines interpreter binary code? All I understand is your code get's turned into 1 and 0's so the machine can understand them, but how do they do that? Is it just a normal text to binary translation?

Monorail answered 3/3, 2012 at 15:0 Comment(0)
L
14

First, "binary" doesn't mean what you think it means (any data on the computer, including text is already binary, it just the way we decide to display and handle is different).

Second, compilation is not a simple transformation to funny characters (if it were, we wouldn't need different compilers for different languages). To actually have some understanding of machine code, you need to understand the architecture that it targets. There are many computer architectures, your PC is just one of them. It is a very broad subject and needs firm understanding of computer architecture to grasp.

I will show an example of a MIPS instructions. If you are interested, you can read on and get some actual knowledge about the subject, try the links at the end of my post.

MIPS is a popular introductory subject because its instruction format is one of the more digestible ones. MIPS instructions are 32 bit wide. There are 3 kinds of instructions in MIPS: "R", "I" and "J". We will take a look at the "I" instructions.

When the processor gets an instruction (32 bits of data) it reads it and decides what to do with it. "I" instructions look like this:

|------|-----|-----|----------------|
 opcode   rs    rt    immediate
   6      5     5     16               (the numbers show how wide are each part)

The meaning of these:

  • opcode tells what kind of instruction this is (for example: addition, subtraction, multiplication and many others). All instructions (including "R" and "J" types) start with the 6-bit opcode, and that's how the processor knows which kind it is.
  • rs and rt are registers, a kind of storage in the processor that can hold 32 bit values. MIPS has 32 of these and they are identified by their number. This is not the same as memory, it's inside the CPU itself.
  • immediate is a number. It is called that because the number is "right there" in the instruction, not in a register or memory.

A concrete example of adding an immediate to a number stored in a register:

001000 00001 00010 0000000000000011

In this example, I broke the instruction into parts as above. The meaning of the values is the following:

  • opcode: 001000 means addi or "add immediate".
  • rs: 00001 is 1 in decimal, so this part of the instruction tells the processor that we want to use register 1 as rs.
  • rd: 00010 is 2 in decimal, same idea as with rs.
  • immediate: 0000000000000011 is 3 in decimal.

The addi instruction works like this: it takes the value found in rs and adds the immediate value to it. After that it puts the outcome into rd. So, when the instruction is done, rd will contain 3+2=5.

In a nutshell, compilers parse your text and generate instructions to the target processor that does the same thing that you intended to do with your program. As you can see, there is a huge gap between the textual representation of the program that us programmers write, and the runnable machine code.

A few useful resources on MIPS and computer architecture:

Ladybug answered 3/3, 2012 at 16:2 Comment(0)
F
0

It's a huge subject what you are asking. I would recommend the excellent book The elements of computing systems for an overview on how computers and compilers are constructed in principle. It's pretty easy to follow and the exercises are fun to do. Most of it is available online at the link provided.

This question also has a few good links on the subject.

Focalize answered 3/3, 2012 at 15:8 Comment(0)
G
0

Think of it this way. You have 8 power switches each switch has either off(0) or on(1). The computer will see all 8 switches as 1. It is Now much like a combination lock. Each Symbol on your keyboard has it's on combination.

Glossematics answered 27/10, 2013 at 17:56 Comment(0)
E
0

Well, that is the processor that all those. But it doesn't "interpret" but by physical circuits made of millions of transistors. Bit 0 represent or off state which means the electrical current has low voltage while bit 1 or the on state means its opposite. Each core of processor containing circuits made of those transistors make each state (on or off) produce different output from those circuits because of various possible combinations of circuits possible on the basis of binary states.

This is very simplified explanation. The actual working is more complex.

Ellenaellender answered 14/8, 2023 at 11:43 Comment(1)
In CMOS logic, a 0 is more like connected to ground, not incomplete (floating, not connected to anything). Notice how the NOT gate in the diagram on en.wikipedia.org/wiki/CMOS has transistors that can connect the output to either the supply voltage or ground. (Most other logic families are the same, working with voltage levels not currents, and a logical "low" output involves pulling the output low, not letting it float).Darees

© 2022 - 2024 — McMap. All rights reserved.