Principles of computer execution of assembly code
Assembly Language is a low-level programming language that is closely related to Machine Language. Assembly language consists of human-readable instructions that are translated into machine-understandable binary code, known as machine code. This article describes the basics of how a computer executes assembly code, with illustrations to help understand the process.
I. What is assembly language?
Assembly language uses helpers (mnemonics) to represent instructions that a computer can understand. It serves as a bridge between humans and hardware and is easier to understand than machine code, but still needs to be tightly integrated with the hardware architecture of the computer.
Example:
MOV AX, 5 ; Stores the value 5 in the AX register. ADD AX, 3 ; Add the value 3 to the value of the AX register
In this example, theMOV
cap (a poem)ADD
are assembly language instructions.AX
is a processor register.
II. How does a computer execute assembly code?
Assembly code cannot be directly executed by a computer. It must go through several steps of conversion and eventually become the machine language of the computer. This process includes the following stages:
1. Preparation of assembly code
Programmers write assembly code, which is in the form of a mnemonic indicating the specific operation that the computer should perform.
2. Assembler conversion
The assembler converts assembly code into machine code. Each assembly instruction corresponds to one or more machine instructions, and the assembler translates these helpers into binary instructions that the CPU can execute.
3. Machine code execution
The machine code that has been converted by the assembler is loaded into the computer's memory and the CPU executes each machine instruction through instruction cycles.
Instruction Cycle is the process by which the CPU executes each instruction, which is usually divided into the following stages:
- Fetch: The CPU takes the next instruction to be executed from memory and stores it in the Instruction Register (IR).
- Decode: The CPU interprets and recognizes the operation code (Opcode) of an instruction and determines the operation that the instruction requires to be performed.
- Execute: The CPU performs operations in instructions, such as performing arithmetic operations or memory reads.
- Write Back: Writes the result of the calculation back to a register or memory.
The following figure shows the basic flow of the CPU executing an instruction:
III. Example of assembly code execution
Let's take a simple assembly code as an example to show the process from writing to execution.
Code:
MOV AX, 5 ; Stores the value 5 in the AX register. ADD AX, 3 ; Add the value 3 to the value of the AX register
1. Preparation phase:
The programmer writes the above assembly code with instructions to store the value 5 into register AX and then add 3 to the value of AX.
2. Assembler Conversion:
The assembler converts these instructions into the corresponding machine code. Each assembly instruction corresponds to a machine code:
-
MOV AX, 5
→B8 05 00
(B8 is the MOV instruction, 05 00 is the binary representation of 5) -
ADD AX, 3
→05 03 00
(05 is the ADD instruction and 03 00 is the binary representation of 3)
This machine code is loaded into the computer's memory and waits for the CPU to execute it.
3. CPU execution:
The CPU starts executing machine code:
-
indexing stage: Read the first instruction from memory
B8 05 00
。 -
decoding stage: The CPU will
B8
identify asMOV
instruction and loads 5 into register AX. -
Implementation phase: Completion
MOV AX, 5
operation that writes the value 5 to the AX register. -
Finger picking, decoding, execution: CPU reads and executes
ADD AX, 3
instruction that ends up storing 8 (the result of 5+3) in the AX register.
IV. Summary
Assembly language, while easier to understand than machine language, is still closely dependent on the hardware architecture. The computer translates assembly code into machine code through an assembler and executes each instruction through the process of instruction cycles. Understanding this process is important for understanding the underlying principles of computer operation.
With this flow, we can see the whole process from assembly language to machine code to actual execution.