Part 9: Assembly Arrays, String Primitives & Hardware Streaming (REP MOVSB)
Lesson Overview & Memory Streaming
High-performance software frequently processes massive sequential collections of data: image pixel buffers, strings, audio PCM streams, and multidimensional scientific matrices. In x86-64 assembly, contiguous data structures can be processed through indexed SIB loops or specialized CPU block string instructions. In Part 9, we analyze 1D and 2D array stride calculations, master the string primitives (MOVSB, LODSB, STOSB, SCASB, CMPSB), understand the Direction Flag (CLD / STD), and explore modern Fast String hardware acceleration (ERMSB / FSRM).
1. Array Layout & Stride Calculations in Assembly
An array in assembly language is simply a block of contiguous memory bytes. Accessing the $i$-th element of an array located at base address Array follows the formula:
For multidimensional 2D row-major matrices with dimensions $M imes N$ (Rows × Columns), accessing cell $[row, col]$ requires calculating the row stride:
2. Dedicated Hardware String Primitives
x86-64 provides dedicated instructions designed specifically for streaming blocks of memory between registers and RAM:
| Instruction | Data Size | Operation | Registers Used |
|---|---|---|---|
| MOVSB / MOVSW / MOVSD / MOVSQ | 1, 2, 4, 8 bytes | Copies data from memory at [RSI] to memory at [RDI] | RSI → RDI |
| LODSB / LODSD / LODSQ | 1, 4, 8 bytes | Loads data from memory at [RSI] into Accumulator (AL / EAX / RAX) | [RSI] → RAX |
| STOSB / STOSD / STOSQ | 1, 4, 8 bytes | Stores value in AL / EAX / RAX into memory at [RDI] | RAX → [RDI] |
| SCASB / SCASQ | 1, 8 bytes | Compares value in AL / RAX with memory at [RDI] (sets ZF) | AL - [RDI] |
3. The Direction Flag (DF): CLD vs. STD
After each byte or quadword is transferred, the CPU automatically increments or decrements RSI and RDI based on the state of the Direction Flag (DF) in RFLAGS:
CLD(Clear Direction Flag): Sets DF = 0. Registers increment automatically (advancing forward from low memory to high memory). This is mandatory under the System V AMD64 ABI!STD(Set Direction Flag): Sets DF = 1. Registers decrement automatically (stepping backwards).
4. Silicon Acceleration: REP MOVSB (ERMSB / FSRM)
When combined with the REP (Repeat) prefix, the CPU automatically iterates the string instruction until RCX == 0. Here is how standard C memcpy is implemented in hardware assembly:
; High-speed memory copy: memcpy(dest, src, 4096)
cld ; Clear direction flag (forward copy)
lea rdi, [dest_buf] ; Destination pointer in RDI
lea rsi, [src_buf] ; Source pointer in RSI
mov rcx, 4096 ; Byte count in RCX
rep movsb ; Hardware fast copy microcode
Modern Intel and AMD processors feature Enhanced REP MOVSB (ERMSB) and Fast Short REP MOVSB (FSRM) hardware, which internally transforms rep movsb into 256-bit or 512-bit vector bus transfers directly inside the silicon microcode engine!
Summary & Next Step
Stream operations handle data blocks cleanly. In Part 10, we reach one of the most critical topics in systems programming: the Call Stack, subroutine creation, function prologues/epilogues, stack frame pointers (RBP), the 128-byte Red Zone, and the official System V AMD64 ABI calling convention.
Related reading
Part 11: Linux 64-Bit System Calls, NASM Macros & Dynamic Memory Allocation
Learn low-level systems programming: Linux 64-bit SYSCALL table, multi-argument NASM %macro directives, file I/O operations, and sys_mmap memory allocation.
By Md. Harun-Or-Roshid
Part 10: Stack Frames, Procedures & The System V AMD64 ABI in Assembly
Master assembly subroutines: CALL, RET, stack frames, RBP base pointers, 16-byte stack alignment, and the official System V AMD64 ABI calling convention.
By Md. Harun-Or-Roshid
Part 8: Conditional Branching, Jump Instructions & Fast Loops in Assembly
Learn how conditional jumps work in x86-64 assembly using CMP, TEST, JE, JNE, JG, JL, JA, JB, and write high-performance loops with branch prediction.
By Md. Harun-Or-Roshid
Part 7: Bitwise Logic, Masking, and Shift/Rotate Instructions in x86-64
Explore bitwise operations in x86-64 assembly: AND, OR, XOR, NOT, TEST, alongside logical shifts (SHL/SHR), arithmetic shifts (SAR), and rotations.
By Md. Harun-Or-Roshid