[BX] Register and Loop Instruction
[BX] and Memory Cell Description
[0] represents a memory cell with a byte length of one, where 0 indicates an offset address of 0, and the segment address is stored in ds.
From this, we can see that describing a memory cell requires two pieces of information: the address of the memory cell and the length of the memory cell (i.e., the data type).
Similarly, [BX] stores the offset address in the bx register (isn’t this similar to pointers in C language?).
Loop
This word means ‘loop’.
【A descriptive symbol is defined in the book, such as representing the content in ax, which is equivalent to providing a pointer expression】<span>()</span>
<span>(ax)</span>
Defining idata Constants
[BX]
EA: effective address, which refers to the offset address in our assembly language.
SA: segment address.
Therefore, the address represented by [BX] is<span>(ds)*16+(bx)</span>
Incrementing bx means adding 1 to the content of bx, which is equivalent to the increment operation in C language (i++).
Let’s analyze a piece of code.
mov ax,2000H
mov ds,ax // Assign value to the segment register
mov bx,1000H
mov ax,[bx] // Load the content of 21000H into ax
inc bx
inc bx
mov [bx],ax // Transfer the content of ax (or the content of 21000H) to 21002H
inc bx
inc bx
mov [bx],ax // Similar operations follow
inc bx
mov [bx],ax
inc bx
mov [bx],ax
Loop Instruction
assume cs:code // Call the cs register
code segment // Define the code segment
mov ax,2
add ax,ax
mov ax,4c00H // This is the return code for the program
int 21h
code ends
end
This program actually calculates 2+2 or 2*2.
But what about 15*15?
Here we can introduce the loop instruction.
Multiplication can be defined and calculated using addition.
assume cs:code // Call the cs register
code segment // Define the code segment
mov ax,15
mov cx,14
s:add ax,ax // Execute loop
loop s
mov ax,4c00H // This is the return code for the program
int 21h
code ends
end
The above only involves a bit of new knowledge, namely how to use loops in assembly.
Let’s supplement some knowledge.
-
Labels: In assembly, a label is an address, such as the code in the previous code segment and the label s in the above code. Here, the label s marks the address of the instruction add ax, ax.
-
When executing the instruction loop s, the CPU performs two operations:
(cx)=(cx)-1
If (cx) != 0, then jump to the address pointed to by s to execute the instruction.
【To add, I find these English abbreviations quite confusing while reading; it makes me wonder why CX is used here instead of BX, but there is indeed a reason behind it.
The AX, BX, CX, and DX registers in the 8086 CPU are 16-bit general-purpose registers, and their names and functions are as follows:
-
AX (Accumulator Register)
- Mainly used for arithmetic operations (such as addition and multiplication), with results typically stored in AX.
- In I/O operations, AX is used for data transfer.
- Can be split into high 8 bits AH (Accumulator High) and low 8 bits AL (Accumulator Low).
- Full English name: Accumulator
- Function:
BX (Base Register)
- Commonly used for memory addressing, serving as a base pointer (e.g.,
<span>MOV BX, AX</span>
). // This aligns with the title of our chapter. - Can also be split into BH (Base High) and BL (Base Low).
- In real mode, it forms a physical address in conjunction with segment registers (like DS).// This also echoes the previous content.
- Full English name: Base
- Function:
CX (Counter Register)
- Used as a counter for loops or repeated operations (e.g.,
<span>LOOP</span>
instruction). - In string operations, CX specifies the number of operations (e.g.,
<span>REP MOVSB</span>
). - Can be split into CH (Counter High) and CL (Counter Low).
- Full English name: Counter
- Function:
DX (Data Register)
- In I/O operations, stores port addresses (e.g.,
<span>OUT DX, AX</span>
). - In 32-bit multiplication and division, combines with AX to store results (e.g., DX:AX stores 32-bit products).
- Can be split into DH (Data High) and DL (Data Low).
- Full English name: Data
- Function:
Supplementary Notes:
-
Naming Logic:
- The first letter (A/B/C/D) represents the main purpose of the register (accumulation, base, counting, data).
- X indicates “extended” (eXtended), as the 8086 extended 8-bit registers to 16-bit (e.g., AX consists of AH and AL).
-
Practical Usage Flexibility: Although the names suggest specific uses, these registers can be flexibly used in programming (e.g., BX can also be used for general data storage), but following conventions can improve code readability.
These registers are core components of the 8086 architecture, and understanding their design logic helps in mastering assembly language programming and low-level hardware interaction.
】
Using the Loop Instruction
assume cs:code // Call the cs register
code segment
mov ax,0ffffh // Initialize, load address into the segment register
mov dx,ax
mov bx,6
mov al,[bx] //(ax)=0000 0006h
mov ah,0
mov dx,0 // Initialize dx for storing accumulated results
mov cx,3
s:add dx,ax
loop s
mov ax,4c00h
int 21h
code ends
end
Segment Prefixes
When accessing memory cells, the segment prefixes “ds:, cs:, ss:, es:” are used in assembly to explicitly indicate the segment address of the memory cell.The benefit of this is to accurately locate the memory cell we want to access.
A Safe Space
In 8086 mode, writing content to memory arbitrarily is dangerous; you do not know whether important data or instructions are written in that memory space.
This issue seems tricky, as it is difficult to remember where in memory something was written. However, the operating system can solve this problem for us.
If we want to write to memory space, we can use the memory space allocated by the operating system (the book points out that our current computer runs on an operating system in CPU protected mode, and we cannot operate on the real hardware).
However, DOS real mode can.
Generally, in DOS mode, the memory cells from 0:200 to 0:2ff are not used, so we can use them.
Some Conclusions
This article mainly discusses the use of the BX register and expands our addressing methods, introducing the loop instruction to enhance our ability to solve problems using assembly language.