Machine code

By 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!

See also

Sign up to the Creative Coding Newletter

Join my newsletter to receive occasional emails when new content is added, using the form below:

Popular tags

555 timer abstract data type abstraction addition algorithm and gate array ascii ascii85 base32 base64 battery binary binary encoding binary search bit block cipher block padding byte canvas colour coming soon computer music condition cryptographic attacks cryptography decomposition decryption deduplication dictionary attack encryption file server flash memory hard drive hashing hexadecimal hmac html image insertion sort ip address key derivation lamp linear search list mac mac address mesh network message authentication code music nand gate network storage none nor gate not gate op-amp or gate pixel private key python quantisation queue raid ram relational operator resources rgb rom search sort sound synthesis ssd star network supercollider svg switch symmetric encryption truth table turtle graphics yenc