Understanding x86 Assembly Direct and Indirect Addressing and the LOOP Instruction
This article explains x86 assembly direct and indirect memory addressing, the meaning of address length, how to use registers like BX for indexed access, and demonstrates loop‑based multiplication with detailed code examples and execution analysis.
The author updates an ongoing series on assembly language, noting the slow publishing pace due to a busy schedule but promising continued tutorials.
In x86 assembly, the operand mov ax,[0] loads a 2‑byte word from the memory location whose offset is 0 in the DS segment, illustrating direct addressing.
Similarly, mov al,[0] loads a single byte from the same offset, showing that the address and the size of the memory unit (word vs. byte) are both required to describe a complete memory operand.
To read data at a specific segment:offset, one can set DS to the segment base and use an offset of 0, e.g., mov bx,10000H mov ds,bx mov al,[0] , which loads the byte at 10000H into AL.
Indirect (register‑based) addressing uses brackets around a register, such as mov ax,[bx] , which reads a word from the address DS:BX. If DS=1000H and BX=1, the CPU fetches the word at 1000H:0001H.
The article introduces a notation using parentheses to denote the contents of a register or memory location, e.g., (ax), (ds), ((ds)*16+(bx)).
It then details the BX addressing mode, showing how to move data between registers and memory with instructions like mov [bx],ax and a sequence of inc bx and mov [bx],ax operations, accompanied by illustrative memory diagrams.
After mastering BX indirect addressing, the author presents a simple multiplication example: computing 2 × 2 by loading 2 into AX and repeatedly adding AX to itself.
To avoid writing many add ax,ax statements for larger repetitions, the LOOP instruction is introduced. By loading the loop count into CX and placing a label before the repeated code, e.g., mov cx,8 s: add ax,ax loop s , the CPU decrements CX and jumps back to the label until CX reaches zero.
The article explains the three‑step execution of LOOP, the importance of placing the label before the loop body, and provides a full example that multiplies 123 by 456 using CX as the counter.
Throughout, code snippets are presented within ... tags and images illustrating memory states are retained.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.