Machine Code
Machine codes are specific to a processor. They are fed to a processor and dictate which operations are performed, how to manipulate registers, and how to read from memory.
In the MIT 2017 6.004 class, a toy machine code is used. Each instruction is 32 bits long:
6 | 5 | 5 | 5 | 11 |
OPCODE | rc | ra | rb | unused |
r_a
and r_b
are source registers locations. r_c
is the destination register.
1. Example
If the OPCODE for ADD is used, then the instruction might correspond with the instruction:
Reg[rc] <-- Reg[ra] + Reg[rb]
Or "add the contents of registers a and b and store them in register c
Other opcodes specify arithemetic, compare, boolean, and shift operators
2. LD and ST
The LD
and ST
instructions specify how to load and store values from registers into memory.
Because machine code is difficult to write by hand, human programmers can instead use an assembly language.