Learn
4.2.2 | ASSEMBLY AND MACHINE CODE
01 | TWO REPRESENTATIONS OF PROCESSOR INSTRUCTIONS
Assembly language is a low-level language that uses short, memorable names called mnemonics for processor instructions.
Machine code consists of encoded instructions that the target processor can execute directly. These encodings are commonly shown as binary values, although hexadecimal may be used to make them easier for people to read.
Assembly is easier for a person to interpret than raw machine code, but the processor does not directly execute the assembly source text. An assembler is needed to translate it.
02 | FIRST, UNDERSTAND THE INSTRUCTION
An instruction specifies an operation for the processor to carry out. Some instructions also specify information needed for that operation.
| Part of a teaching instruction | Meaning |
|---|---|
| ADD | Add a value to the accumulator |
| 21 | Use the value stored at memory address 21 |
The whole assembly instruction is ADD 21. It means “add the value at address 21 to the current accumulator value”. It does not mean “add the literal number 21”.
Different processors and addressing modes have different rules. Here we use one simple, explicitly defined model to avoid mixing those rules.
03 | MNEMONIC, OPCODE AND OPERAND
| Term | Purpose | Example in our teaching model |
|---|---|---|
| Mnemonic | Readable assembly name for an operation | ADD |
| Opcode | Machine-instruction field identifying the operation | 0010 means ADD |
| Operand | Information identifying data or a location to use | Address 21, encoded as 00010101 |
The mnemonic is a name used in assembly source. The encoded opcode tells the processor which operation to perform. The operand here identifies where to obtain the data.
An operand is not always an address: other instruction formats can use a literal value or a register. Some instructions have no explicit operand, while others have several. Always follow the format given in the question.
04 | DEFINE OUR TEACHING PROCESSOR
For the worked examples, imagine a processor with an accumulator (ACC), a register that holds working values and calculation results.
| Mnemonic | Invented opcode | Effect |
|---|---|---|
| LOAD | 0001 | Copy the value at the operand address into ACC |
| ADD | 0010 | Add the value at the operand address to ACC |
| STORE | 0011 | Copy ACC to the operand address; leave ACC unchanged |
Each instruction is 12 bits: a 4-bit opcode followed by an 8-bit address operand. The operand can encode addresses 0 to 255.
This model is for understanding the relationship between representations. Real processors can use different instruction lengths, encodings and register arrangements.
05 | SEE THE TWO FIELDS
OPCODE | 4 BITS
0010ADD operation
OPERAND | 8 BITS
00010101Address 21
Together, the fields form 0010 00010101. The space separates the fields for readability; it is not an extra bit.
The operand value is 21 because 00010101 represents 16 + 4 + 1. The opcode’s meaning comes from the processor’s instruction set, not from treating 0010 as a quantity to add.
06 | WHAT THE ASSEMBLER DOES
An assembler translates assembly source into the corresponding machine code for the target processor.
LOAD 20 ADD 21 STORE 22
| Assembly source | Opcode field | Address field | Machine instruction |
|---|---|---|---|
| LOAD 20 | 0001 | 00010100 | 0001 00010100 |
| ADD 21 | 0010 | 00010101 | 0010 00010101 |
| STORE 22 | 0011 | 00010110 | 0011 00010110 |
The assembler recognises each mnemonic, selects its opcode and encodes the operand according to the target format.
Translation is not execution. Producing these encodings does not itself add the numbers. The processor executes the resulting instructions when the program runs.
In this simple model, each assembly instruction corresponds to one machine instruction. Real assemblers may also support directives or pseudo-instructions, so not every line of an assembly source file must create exactly one instruction.
07 | TRACE THE PROGRAM
Initially, ACC = 0. Memory address 20 contains 7, address 21 contains 5, and address 22 contains 0.
| Step | Instruction | What happens | ACC afterwards | Memory 22 afterwards |
|---|---|---|---|---|
| Start | None yet | Initial state | 0 | 0 |
| 1 | LOAD 20 | Copy the value 7 from memory 20 into ACC | 7 | 0 |
| 2 | ADD 21 | Add the value 5 from memory 21 to ACC | 12 | 0 |
| 3 | STORE 22 | Copy ACC to memory 22 | 12 | 12 |
The result is 12, not 41. Addresses 20 and 21 tell the processor where the numbers are; they are not the numbers being added.
STORE changes the value in memory 22. It does not alter the earlier values at addresses 20 and 21 in this model.
08 | CONNECT IT TO FETCH, DECODE, EXECUTE
When the program runs, its machine instructions are stored in memory. The processor fetches an instruction, decodes it and executes it.
For our encoded ADD 21, the instruction register holds 0010 00010101. The control unit interprets the opcode as ADD and the operand as address 21. The value at that address is obtained and added to ACC using the ALU.
The assembler created the encoding before execution. The control unit interprets the encoding during execution. These are different jobs.
In the earlier trace, ACC was 7 and memory 21 held 5, so executing the ADD instruction produces ACC = 12.
09 | WHY USE ASSEMBLY?
Mnemonics are easier to recognise than long sequences of encoded bits. Assembly also exposes details such as processor operations, registers and addresses, which can be useful for specialised hardware work.
However, it remains harder to write and debug than many high-level programs. A wrong operand can refer to the wrong location even if the instruction is valid.
Assembly is machine dependent: its instructions and translation must match the target architecture. An assembler for a different instruction set cannot simply give the same source an equivalent meaning without appropriate support or changes.
10 | VALID CODE CAN STILL BE WRONG
Suppose the programmer accidentally writes ADD 22 instead of ADD 21. The assembler may accept it because address 22 is valid.
In the initial example, address 22 holds 0, so the program would produce 7 instead of 12. This is a logic error, not necessarily a syntax error.
An assembler can report invalid source forms or unsupported instructions, but it cannot guarantee that a valid program solves the intended problem. The program still needs testing.
11 | COMPARE ASSEMBLY AND MACHINE CODE
| Point | Assembly language | Machine code |
|---|---|---|
| Representation | Mnemonics and operands | Encoded instruction bits |
| Human readability | Easier to interpret than raw encodings | Harder to interpret directly |
| Execution | Needs an assembler to translate source | Directly executable by the matching processor |
| Processor relationship | Tied to the target instruction set | Tied to the target instruction set |
Exam-style explanation: assembly uses mnemonics such as ADD, whereas the processor executes encoded machine instructions. Therefore an assembler is required to translate the source into machine code matching the target instruction set.
12 | COMMON MISTAKES
- Assembly executes directly: its source must be translated by an assembler.
- The mnemonic and operand are the same: the mnemonic names the operation; the operand supplies information it uses.
- An address operand is always a literal value: follow the addressing model given.
- The assembler performs the calculation: translation produces instructions; execution performs their operations.
- A machine-code instruction is just data to add: its fields specify an operation and relevant information.
- Every processor uses these encodings: this lesson’s format is invented.
- Successful assembly guarantees a correct result: logic errors still need testing.