This series will explain the book “Assembly Language”. This section covers Chapter 5 – **[BX] Register and Loop Instructions**.
In this section, we introduce another way to describe memory, the [BX] register, and the commonly used loop instruction: loop, discussing its applications, significance, and related content.
The entire text uses (ax) to represent the contents of the ax register, and (address) to represent the contents at address address.
| 1. Meaning of [BX] |
| 2. Essence of the loop instruction |
| 3. Execution steps of the loop instruction |
| 4. Commands to skip loops: p, g |
1. [BX]
1. Explanation of [BX]
In Chapter 3 of “Assembly Language” – Interaction between Registers and Memory, we learned that [address] represents the offset address (i.e., effective address EA), and the segment address is by default in the ds register. In that chapter, we directly provided specific constants, for example, [1000], which indicates an offset address of 1000. A natural extension of this idea is to store this constant in a register: [AX], and the [BX] introduced here is precisely that!
[BX] and [address] also represent a memory unit, with its offset address stored in the bx register.
Clearly, BX is just an example; it can be replaced with AX, BX, CX, or DX! The essence is simply to store the offset address of data in memory using a register!
2. A Small Test
Let’s take a look at the functionality of the following instructions.
Source Code:
mov ax,[bx]
Function:
The data stored in bx serves as an offset address EA, with the segment address SA by default in ds. The data at SA:EA is then loaded into ax. That is: (ax)=((ds)*16+(bx)).
Source Code:
mov [bx],ax
Function:
The data stored in bx serves as an offset address EA, with the segment address SA by default in ds, and the data in ax is stored at memory location SA:EA. That is: ((ds)*16+(bx))=(ax).
2. Loop Instruction
1. Introduction to the loop instruction
The format of the loop instruction is:
loop label
When the CPU executes the loop instruction, it performs two operations:
| Operations of the loop instruction |
|---|
| ① (cx) = (cx) – 1; |
| ② Check the value in cx; if it is not zero, jump to the label to execute the program, if it is zero, continue executing downwards. |
From the above description, we can see that the value in <span>cx</span> affects the execution result of the loop instruction. Typically (note that we say typically), we use the loop instruction to implement looping functionality, with <span>cx</span> storing the number of iterations.
2. Essence of loop s
Jump to the address indicated by label s. For example, if the address of label s is ffff:0006, it is equivalent to <span>loop ffff:0006</span>.
3. A Small Test
1. Problem
Task: Program to calculate 2^6, with the result stored in ax.
Analysis: Let (ax) = 2, we can calculate (ax) = (ax) * 2, so the final value in (ax) will be 2^2. Therefore, each addition doubles the original value, and we can use a loop to achieve the calculation of 2^6.
2. Program to calculate 2^2 is as follows
assume cs:code
code segment
mov ax, 2
add ax, ax
mov ax, 4c00h
int 21h
code ends
end
3. Program to calculate 2^6 is as follows
assume cs:code
code segment
mov ax, 2
mov cx, 6-1
; Note that we loop 6 times, we only need to write 6-1 because the operation is executed, and the loop instruction is executed afterwards!
L: add ax, ax
loop L
mov ax, 4c00h
int 21h
code ends
end
4. Step-by-step Debugging Analysis
The final result of 2^6 = 64, and with each loop, the value in ax is doubled:
Compilation and Linking Process:
For the specific compilation and linking process to create an executable file, please refer to this article:😉【DOSBox】3- Complete Development Process
Debugging the Executable File Process:
For the specific debugging of the executable file, please refer to this article:【DOSBox】2- Debugging Executable Files
Step-by-step Debugging Process:
Clearly, after 6 loops, the result is 64!
Note that the essence of s is the address of the label, and <span>loop s</span> essentially jumps to the address indicated by label s.
3. Comprehensive Practice
1. Problem
Consider the problem of multiplying the number in <span>ffff:0006</span> by 3, storing the result in <span>dx</span>.
2. Analysis
Since the data range of one unit is <span>0~2^8-1 (255)</span>, <span>255*3 < 2^16 = 65535</span>, so it can be completely stored in <span>dx</span>.
3. Source Code
assume cs:code
code segment
mov ax, 0ffffh
mov ds, ax
xor ax, ax
mov bx, 0006h
mov ax, ds:[bx]
mov cx, 3
s: add ax, ax
loop s
mov ax, 4c00h
int 21h
code ends
end
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/c93411ec-bc44-4c73-8ced-f822c81729a9.png)
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/ecac441b-4aa4-472b-8dd5-b84230747587.png)
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/d4e3132c-ec33-48c5-bce9-25dbf810a696.png)
4. Direct Execution Until Skipping Loop Commands: g
1. -p, -g Commands
In the execution process above, we completed it step by step using the -t command. If there are 100 loops, it would require pressing the keyboard many times. Is there a way to skip directly? Of course, and there are many commands!
| Commands to Skip Loops Directly(Multi-step Commands) | Explanation |
|---|---|
| -p | Applicable for those who wish to quickly skip loops, interrupt handling, or subroutine calls, to quickly check the status of subsequent programs. |
| -g | No parameters: DEBUG will start executing the program from the current instruction pointer (IP) position until it normally ends, encounters an interrupt instruction (such as int instruction), or encounters an error in the program (such as illegal instruction, memory access error, etc.) to stop.With parameters: The command format is –g start address breakpoint address1 breakpoint address2 …. The start address specifies where the program starts executing (can be omitted, in which case it starts from <span>the current IP position</span>); the breakpoint address is the position in the program execution where it needs to pause. |
2. Demonstration
-p Command
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/4469aa17-abd9-48f7-b161-4c3e1366036f.png)
-g Command
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/ba9246cc-03fa-4eb6-b268-5429372fcd3d.png)
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/c0439954-be25-46dc-8111-29f4e5c22a80.png)
5. Note
Note that in assembly source programs, data cannot start with a letter, so
<span>ffffh</span>must be written as<span>0ffffh</span>
Conclusion
Alright, in this section, we discussed the basic knowledge of <span>Chapter 5 of "Assembly Language": [BX] Register and Loop Instructions</span>. Let’s review:
| 1. What is the meaning of [BX]? |
| 2. What is the essence of the loop instruction? |
| 3. What are the execution steps of the loop instruction? |
| 4. What are the functions of the commands p and g to skip loops? What are their respective roles? |
| 5. Can assembly data start with a letter? Why? |
| 6. What is the relationship between loop instruction and cx register? |
If you cannot explain each item in this table, then please review the above content carefully!
Answers can be found in the comments section, and everyone is welcome to discuss~
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/17dcc1cb-edda-48e7-babf-7d8540840a9d.gif)
😉【Assembly Language】1 – Basic Hardware Knowledge
😉【Assembly Language】2 – Basic Knowledge of Registers
😉【Assembly Language】3 – Interaction between Registers and Memory
😉【Assembly Language】4 – The First Complete Assembly Program
😉【DOSBox】1 – Debug
😉【DOSBox】2 – Debugging Executable Files
😉【DOSBox】3 – Complete Development Process
😉【C Language】C Token (C89 C99 C11)
😉【C Language】Basic Knowledge of Pointers
😉【C Language】Basic Knowledge of Arrays
😉【C Language】Structure Alignment
😉【C Language】Huawei C Language Advanced Test
😉【C Language】Summary of Methods to Trigger Disk Refresh
😉【C Language】Detailed Explanation of File Operation Modes
😉【C Language】Comprehensive Explanation of C Language File Knowledge
😉【C Language】Optimal Practices from extern to Header File Inclusion
😉【C Language】C Language Keywords and Overloading Mechanisms
😉【C Language】Two Methods for Handling Long Strings
😉【C Traps and Pitfalls】 -1- Lexical Traps
😉【C Traps and Pitfalls】 -2- Syntax Traps
😉【C Traps and Pitfalls】 -3- Semantic Traps
😉【Linux】Essential Knowledge Before Learning Linux
😉【Linux】Memory Abstraction of Processes in the Linux Kernel
😉【Linux】Overview of Linux 1 – Usage of Physical Memory
😉【Linux】The Entire Process from Writing Software to Running It
😉【Linux】Three Addressing Modes of CPU: Real Mode, Protected Mode, Long Mode
😉【Linux】Addressing in Real Mode and Protected Mode, Explained Clearly This Time!
😉【Linux】Source Code Directory Structure of Linux 0.11
😉【Linux】Makefile Mechanism and Basic Explanation
😉【Linux】Compiling and Running Linux 0.11
😉【Linux】”Inside the Intranet Document” – Overview of Linux Kernel Structure
😉【Linux】Linux Interrupt Mechanism
😉【Linux】Description of Linux Processes
😉【Linux102】1 – Makefile
😉【Linux102】2 – Makefile.header
😉【Linux102】3 – system.map
😉【Linux102】4 – bootsect.s
😉【Linux102】5 – setup.s
😉【Linux102】6 – head.s
😉【Linux102-D】/boot
😉【Linux102】7 – main.c
😉【docker】1 – Basic Knowledge
😉【Cloud Computing】0 – The Past and Present of Cloud Computing
😉【Cloud Computing】1 – Basic Knowledge
😉【Cloud Computing】2 – Basic Technologies (Computing, Communication, Storage)
😉【Cloud Computing】3 – Comprehensive Introduction to Huawei Cloud
😉【Cloud Computing】4 – Usage of Huawei Cloud
😉【Huawei Cloud】 -1 Elastic Cloud Server – ECS
😉【Huawei Cloud】 Bare Metal Server (BMS)
😉【Huawei Cloud】 Image Service (IMS)
😉【Huawei Cloud】 Elastic Scaling Service (AS)
😉【Huawei Cloud】 Cloud Container Engine (CCE)
😉【Huawei Cloud】 Virtual Private Cloud (VPC)
😉【Huawei Cloud】 Elastic Load Balancing (ELB)
😉【Huawei Cloud】 Virtual Private Network (VPN)
😉【Huawei Cloud】 NAT Gateway
😉【Huawei Cloud】 Cloud Disk (EVS)
About Xiao Xi
😉 Hehe, I am Xiao Xi, focusing on <span>C Language</span><span>, </span><code><span>Linux Kernel</span><span>, and </span><code><span>Cloud Computing</span><span> fields.</span>
Here is my WeChat, looking forward to learning and communicating with you!
![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/d743f2b5-1e33-431c-8878-a4c294663b99.jpeg)
C_Linux_Cloud
Please note when adding WeChat
Xiao Xi’s motto:
<span>Don't underestimate simplicity; simplicity can be difficult. Don't underestimate difficulty; difficulty can be simple.</span>My articles are all about explaining simple knowledge, if you like this style:
What do you want to see in the next issue? Leave a message in the comments! See you next time!
For the specific compilation and linking process to create an executable file, please refer to this article:
For the specific debugging of the executable file, please refer to this article:![Assembly Language: Understanding the [BX] Register and Loop Instructions](https://boardor.com/wp-content/uploads/2025/10/0105c3a5-5e2e-4283-81cc-d4124348ffe2.png)