ⓘ Machine code is a computer program written in machine language. It uses the instruction set of a particular computer architecture. It is usually written in bina ..


ⓘ Machine code

Machine code is a computer program written in machine language. It uses the instruction set of a particular computer architecture. It is usually written in binary. Machine code is the lowest level of software. Other programming languages are translated into machine code so the computer can execute them.

An instruction tells the process what operation to perform. Each instruction is made up of an opcode operation code and operands. The operands are usually memory addresses or data. An instruction set is a list of the opcodes available for a computer. Machine code is what assembly code and other programming languages are compiled to or interpreted as.

Program builders turn code into another language or machine code. Machine code is sometimes called native code. This is used when talking about things that work on only some computers.


1. Writing machine code

Machine code can be written in different forms:

  • Using a Hex editor. This allows the use of opcodes instead of the number of the command.
  • Using a High-level programming language allows programs that use code that is easier to read and write. These programs are translated into machine code. The translation can happen in many steps. Java programs are first optimized into bytecode. Then it is translated into machine language when it is used.
  • Using a number of switches. This generates a sequence of 1 and 0. This was used in the early days of computing. Since the 1970s, it is no longer used.
  • Using an Assembler. Assembly languages are simpler than opcodes. Their syntax is easier to understand than machine language but harder than high level languages. The assembler will translate the source code into machine code on its own.

2. Typical instructions of machine code

There are many kinds of instructions usually found in an instruction set:

  • Operations that act on program flow: jump to some address.
  • Operations that compare two values: bigger than, smaller than, equal.
  • Operations that combine other operations: add, compare, and copy if equal to some valueas one operation, jump to some point in the program if a register is zero.
  • Logical operations: Conjunction, disjunction, negation.
  • Operations acting on memory: copying a value from one register to another.
  • Operations acting on single bits: Shifting bits to the left or right.
  • Arithmetical operations: Addition, subtraction, multiplication, division.
  • Operations that convert data types: e.g. convert a 32-bit integer to a 64-bit integer, convert a floating point value to an integer by truncating.

Many modern processors use microcode for some of the commands. More complex commands tend to use it. This is often done with CISC architectures.


3. Instructions

Every processor or processor family has its own instruction set. Instructions are patterns of bits that correspond to different commands that can be given to the machine. Thus, the instruction set is specific to a class of processors using mostly the same architecture.

Newer processor designs often include all the instructions of a predecessor and may add additional instructions. Sometimes, a newer design will discontinue or alter the meaning of an instruction code typically because it is needed for new purposes, affecting code compatibility; even nearly completely compatible processors may show slightly different behavior for some instructions, but this is rarely a problem.

Systems may also differ in other details, such as memory arrangement, operating systems, or peripheral devices. Because a program normally relies on such factors, different systems will typically not run the same machine code, even when the same type of processor is used.

Most instructions have one or more opcode fields. They specify the basic instruction type. Other fields may give the type of the operands, the addressing mode, and so on. There may also be special instructions that are contained in the opcode itself. These instructions are called immediates.

Processor designs can be different in other ways. Different instructions can have different lengths. Also, they can have the same length. Having all instructions have the same length can simplify the design.


4. Example

The MIPS architecture has instructions which are 32 bits long. This section has examples of code. The general type of instruction is in the op operation field. It is the highest 6 bits. J-type jump and I-type immediate instructions are fully given by op. R-type register instructions include the field funct. It determines the exact operation of the code. The fields used in these types are:

6 5 6 bits 2 1024 decimal 000010 00000 10000 000000 binary
Free and no ads
no need to download or install

Pino - logical board game which is based on tactics and strategy. In general this is a remix of chess, checkers and corners. The game develops imagination, concentration, teaches how to solve tasks, plan their own actions and of course to think logically. It does not matter how much pieces you have, the main thing is how they are placement!

online intellectual game →