Computer Science › Section C · Data manipulation › Topic C.2

Machine language

A CPU understands only bit patterns. Its machine language is the fixed list of patterns it can decode and obey, and reading a few instructions by hand shows exactly what a program looks like to the hardware.

Instructions as bit patterns

The stored-program concept, from "Computer architecture: CPU and stored programs", only works if instructions can be written as bits. So every CPU is built to recognise a set of bit patterns, each meaning one elementary action, such as "add these two registers". That set of instructions, together with the rules for encoding them, is the CPU's machine language, also called its instruction set. One encoded instruction is a machine instruction. Different processor families have different machine languages, which is why a program compiled for one family will not run directly on another.

A short list is enough

A machine language can be surprisingly small. Once a CPU can copy data, do basic arithmetic and logic, and jump conditionally, extra instructions add convenience and speed but no new power: anything a bigger instruction set can do, a small one can do in more steps. This led to two design philosophies. A reduced instruction set computer (RISC) offers a small set of simple, fast instructions, usually all the same length. A complex instruction set computer (CISC) offers many instructions, some doing in one step what would take a RISC machine several, and its instructions vary in length.

RISC and CISC today

The x86-64 instruction set used by Intel and AMD processors is the classic CISC design, and it runs most desktop PCs and servers. ARM designs are RISC. Built for low power use, they run almost every smartphone, and since 2020 Apple's Mac processors have used ARM too. RISC-V is a newer RISC instruction set that anyone may use without a licence fee. The dividing line has blurred: modern x86 processors translate complex instructions into simpler internal steps before carrying them out.

Three groups of instructions

Whatever the design, machine instructions fall into three groups. Data transfer instructions copy bit patterns from one place to another. Copying from a memory cell into a register is called a LOAD, and from a register into a memory cell a STORE; the original is not erased, so "copy" describes them better than "move". Instructions that talk to devices such as printers are usually counted here too. Arithmetic/logic instructions ask the ALU to compute: add, subtract, AND, OR, XOR, shift and rotate. Control instructions direct the program itself, rather than data: HALT stops the machine, and JUMP makes the CPU continue somewhere other than the next instruction, either always (unconditional) or only when a test is true (conditional).

Meet Sparrow, a teaching machine

This course uses a small imaginary machine called Sparrow. It has 16 general-purpose registers, R0 to RF, each holding 8 bits, and 256 memory cells of 8 bits each, addressed 0x00 to 0xFF in hexadecimal. Every instruction is 16 bits long, so it fills two consecutive cells and is written as four hex digits. The first digit is the op-code, which says which operation to perform. The other three digits are the operand field, which supplies the details: which registers, which address, or which value. Why exactly 256 cells? An address in the operand takes 8 bits, and 8 bits can name exactly \(2^8 = 256\) different cells.

The Sparrow instruction table

Here R, S and T stand for register numbers, XY for a two-digit hex address or value, and N for a single digit. 0x1RXY LOAD: copy memory cell XY into R. 0x2RXY SET: put the value XY itself into R. 0x3RXY STORE: copy R into cell XY. 0x40RS COPY: copy register S into R. 0x5RST ADD and 0x6RST SUB: R gets S + T or S − T, in two's complement. 0x7RST AND, 0x8RST OR and 0x9RST XOR: R gets S combined with T bit by bit. 0xAR0N ROTATE R right N bits. 0xBR0N and 0xCR0N SHIFT R left or right N bits, filling with 0s. 0xDRXY: if R holds 0, jump to XY. 0xE0XY: jump to XY. 0x0000 HALT.

Worked example: decoding instructions

Translate four Sparrow instructions into English. 1. 0x14B2: op-code 1 is LOAD, so copy the contents of memory cell 0xB2 into register R4. 2. 0x2E7F: op-code 2 is SET, so place the value 0x7F, which is 127, in RE. 3. 0x5A3C: op-code 5 is ADD, so RA gets R3 + RC. 4. 0xD460: op-code D tests R4, so if R4 holds 0 the next instruction comes from address 0x60; otherwise the program carries on in order. Check: 0x14B2 and 0x24B2 differ only in the op-code, yet the first fetches whatever is stored at 0xB2 while the second loads the number 0xB2 itself.

Worked example: writing a program

Write a Sparrow program that subtracts the value in cell 0x41 from the value in cell 0x40 and stores the answer in cell 0x42. 1. Load the first value: 0x1240 puts cell 0x40 into R2. 2. Load the second: 0x1341 puts cell 0x41 into R3. 3. Subtract: 0x6423 sets R4 to R2 − R3. 4. Store: 0x3442 copies R4 into cell 0x42. 5. Stop: 0x0000. Check with data: if cell 0x40 holds 0x2D (45) and cell 0x41 holds 0x13 (19), a simulated run leaves 0x1A, which is 26, in cell 0x42. As stored bits, step 3 is 0110 0100 0010 0011.

Bits mean what the instruction says

Registers and memory cells do not record what kind of data they hold. The pattern 0x9C is 156 if read as an unsigned number and −100 if read in two's complement, and only the instruction that uses it decides which. That is why real machines often have separate instructions for integer addition and floating-point addition: the ALU must do different work for each format. Instruction length is another design choice. Sparrow's are all two bytes, a RISC-like feature that makes fetching simple. x86-64 instructions range from 1 byte up to as many as 15, so the CPU must read the first bytes to learn how many more to fetch.

Assembly language

Writing hex digits by hand is error-prone, so programmers give each op-code a short name, a mnemonic. The subtraction program above could be written as LOAD R2, [40]; LOAD R3, [41]; SUB R4, R2, R3; STORE R4, [42]; HALT. This is assembly language. A program called an assembler translates it, almost line for line, into machine instructions. Each assembly language belongs to one machine language, so it is still low level. The topic "Language implementation: translation and compilers" shows how high-level languages are translated much further.

Common mistakes to avoid

First, mixing up LOAD and SET: 0x1RXY copies what is stored at address XY, while 0x2RXY uses XY itself as the value. Second, reading the operand before the op-code; the same three digits mean registers in one instruction and an address in another. Third, believing a bigger instruction set can compute things a small one cannot; it is only more convenient. Fourth, forgetting that each Sparrow instruction takes two memory cells, so consecutive instructions sit two addresses apart.

Key takeaways

A machine language is the set of bit-pattern instructions a CPU can decode. RISC designs such as ARM and RISC-V use a few simple, uniform instructions; CISC designs such as x86-64 use many, of varying length. Instructions form three groups: data transfer, arithmetic/logic and control. A Sparrow instruction is four hex digits: a one-digit op-code and a three-digit operand field. An 8-bit address field can name \(2^8 = 256\) cells. A bit pattern's meaning depends on the instruction that uses it. Assembly language gives instructions readable names.

Flashcards

Flashcard

Machine language

The set of encoded instructions a CPU can decode and execute

Also called the instruction set.

Test your understanding

Question 1 of 5

In Sparrow, what does the instruction 0x37A5 do?

Question 2 of 5

What is the difference between the Sparrow instructions 0x1385 and 0x2385?

Question 3 of 5

Which statement about RISC and CISC is correct?

Question 4 of 5

A machine's instructions give memory addresses in a 10-bit field. How many different memory cells can they name?

Question 5 of 5

To which group does an instruction such as "if register R4 holds 0, continue from address 0x60" belong?

← Previous topicNext topic →