Machine code

Martin McBride, 2017-04-29
Tags none
Categories none

The part of the computer which actually does the work of running a program is the CPU (Central Processing Unit). However, it can't run a Python or Java program directly.

The CPU executes machine code. This is a sequence of instructions stored in memory as byte values - numbers. Each number represents a different instruction, such as read a value, add 2 values together, etc.

A simple CPU typically has:

  • A set of registers, each of which can store a binary number.
  • An accumulator, which is just a special register that is used to store the result of a calculation.
  • A program counter that points to the next instruction in memory.
  • An instruction decoder, which takes a value out of memory and decides which action to perform.
  • An ALU (Arithmetic and Logic Unit) which can perform simple integer arithmetic and bitwise logic on two values, leaving the result in the accumulator.

machine code

The CPU reads an instruction out of memory, from the location contained in the program counter. The decoder looks at the value of the instruction byte and decodes it to decide what to do. This might involve reading a value from memory, writing a value to memory, or performing a calculation using the ALU.

Properties of machine code

Machine code is the most basic type of computer code. It consists of the raw instructions that the CPU can directly understand. We call this a low level language. It is easy for the machine to understand, but a struggle for most humans (unless you spend a lot of time doing it).

Machine code can be used to create highly efficient code which executes quickly, uses as little memory as possible. It can directly access the computer's hardware devices.

The instruction set is specific to the type of processor. So not only is the code difficult to write, if you want to run it on a different type of computer, you might find yourself having to learn a completely new set of instructions, and more or less writing all your code again from scratch.

Machine code is sometimes called a 1st generation language, because it was what people used when they had no other way of programming. These days, it is extremely rare for anyone to write machine code. It has no real advantage over assembly language. Even though assembly language is still quite difficult to write, it is far easier than machine code!

Copyright (c) Axlesoft Ltd 2020