Assembly Language: Understanding the [BX] Register and Loop Instructions

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.

Overview of This Section
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:Assembly Language: Understanding the [BX] Register and Loop InstructionsFor 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:Assembly Language: Understanding the [BX] Register and Loop InstructionsFor the specific debugging of the executable file, please refer to this article:【DOSBox】2- Debugging Executable Files

Step-by-step Debugging Process:Assembly Language: Understanding the [BX] Register and Loop InstructionsAssembly Language: Understanding the [BX] Register and Loop InstructionsClearly, 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
Source Code in Image Format
Assembly Language: Understanding the [BX] Register and Loop Instructions
The value at address ffff:0006 before debugging is 31h, and after multiplying by 3, it should be 93h
Assembly Language: Understanding the [BX] Register and Loop Instructions
The process of debugging execution ends, and indeed the value in dx is 93h

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
-p command, directly solves loops, interrupt calls, subroutines, etc. From here, we can see that after execution, the correct result 93h is obtained

-g Command

Assembly Language: Understanding the [BX] Register and Loop Instructions
-g command, without parameters, directly executes to the end
Assembly Language: Understanding the [BX] Register and Loop Instructions
-g command, with parameters, acts as a breakpoint, executing to the specified address! From here, we can see that after execution, the correct result 93h is obtained

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:

Review of Knowledge on [BX] and Loop Instructions
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

Assembly Language

😉【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 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

😉【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

Cloud

😉【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

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:

Feel free to follow, comment, and share so more friends can see it~~~🙈

What do you want to see in the next issue? Leave a message in the comments! See you next time!

Leave a Comment