YouTip LogoYouTip

Assembly Array

## Assembly Language - Arrays An array is a collection of the same type of data stored contiguously. In assembly, an array is a continuous block of memory, and each element is accessed using base address plus offset. * * * ## One-Dimensional Array Definition ## Example ; Array definition section.data ; Method 1: List elements one by one arr1 dd 10,20,30,40,50; Array of 5 double-word elements ; Method 2: Initialize using dup arr2 dd 10 dup(0); 10 double-words, all 0 ; Method 3: Byte array bytes db 1,2,3,4,5,6; 6 bytes ; Method 4: Character array (string) chars db'tutorial',0; 7 bytes ; Array length calculation (at compile time) arr1_len equ($- arr1)/4; Number of elements in double-word array arr2_len equ($- arr2)/4 bytes_len equ($- bytes); Byte array, no division needed section.bss ; Uninitialized array buffer resd 100; Reserve space for 100 double-words * * * ## Array Element Access Access array elements using the addressing mode: base + index Γ— element size: ## Example ; File path: array_access.asm ; Reading and writing array elements section.data nums dd 100,200,300,400,500; 5 elements nums_len equ($- nums)/4 section.text global _start _start: ; Access element 0 (index 0) mov eax,; eax = 100 ; Access element 2 (index 2) mov eax,[nums +2*4]; eax = nums = 300 ; 2 * 4 = 8, offset 8 bytes from nums ; Use register as index mov esi,3; index = 3 mov eax,[nums +esi*4]; eax = nums = 400 ; Modify array element mov dword[nums +4],250; nums = 250 ; Array now: 100, 250, 300, 400, 500 ; Use EBX as base register mov ebx, nums ; ebx = array base address mov eax,[ebx+4*4]; eax = nums = 500 mov eax,1 mov ebx,0 int 0x80 > Note: The multiplication by 4 in `[nums + esi * 4]` is because each element is 4 bytes (double-word). For byte arrays (db), write directly as `[arr + esi]`; for word arrays (dw), write as `[arr + esi * 2]`. * * * ## Array Traversal ## Example ; File path: array_traverse.asm ; Traverse array and calculate sum section.data numbers dd 5,10,15,20,25,30,35,40,45,50 count equ($- numbers)/4; Number of elements section.text global _start _start: mov ecx, count ; Number of iterations mov esi,0; index (starting from 0) mov eax,0; accumulator sum_loop: add eax,[numbers +esi*4]; Add array element inc esi; index +1 loop sum_loop ; eax = 5+10+15+...+50 = 275 ; Another traversal method: using pointer mov ecx, count mov ebx, numbers ; ebx points to array start mov eax,0; accumulator sum_loop2: add eax,; Add current element add ebx,4; move pointer to next element loop sum_loop2 ; eax = 275 mov ebx,eax; exit code = sum mov eax,1 int 0x80 * * * ## Array Search ## Example ; File path: array_search.asm ; Search for a specified value in array section.data data dd 12,45,67,23,89,34,56,78,90,11 data_len equ($-data)/4 target dd 23; value to search found_msg db'Found at index: ',0 found_len equ$- found_msg notfound_msg db'Not found (tutorial)',0xA notfound_len equ$- notfound_msg newline db 0xA section.text global _start _start: mov ecx, data_len ; number of iterations mov esi,0; current index search_loop: mov eax,[data+esi*4]; load current element cmp eax,; equals target value? je found ; found inc esi; index +1 loop search_loop ; not found mov eax,4 mov ebx,1 mov ecx, notfound_msg mov edx, notfound_len int 0x80 jmp exit found: ; found (esi is the index) mov eax,4 mov ebx,1 mov ecx, found_msg mov edx, found_len int 0x80 ; convert index to ASCII character for output add esi,'0'; single digit index to character push esi; push onto stack for temporary storage mov eax,4 mov ebx,1 mov ecx,esp; stack top address mov edx,1 int 0x80 pop esi ; output newline mov eax,4 mov ebx,1 mov ecx, newline mov edx,1 int 0x80 exit: mov eax,1 mov ebx,0 int 0x80 * * * ## Two-Dimensional Arrays Two-dimensional arrays are stored in memory as a one-dimensional array by row-major order. The address formula to access `arr` is: Address = base + (i * columns + j) * element size ## Example ; File path: 2d_array.asm ; Two-dimensional array definition and access section.data ; 3 rows Γ— 4 columns 2D array matrix dd 1,2,3,4 dd 5,6,7,8 dd 9,10,11,12 rows equ 3 cols equ 4 elem_size equ 4; double-word = 4 bytes section.text global _start _start: ; Access matrix = 7 (row 2, column 3) ; Address = matrix + (1*4 + 2) * 4 = matrix + 24 mov eax,[matrix +(1* cols +2)* elem_size] ; eax = 7 ; Use registers for dynamic calculation (assume i=2, j=1) ; matrix = 10 (row 3, column 2) mov esi,2; row i = 2 mov edi,1; column j = 1 mov eax, cols ; number of columns mul esi; eax = i * cols = 2*4 = 8 add eax,edi; eax = i*cols + j = 8+1 = 9 ; eax = eax * elem_size ; Note: MUL result is in EAX, use it directly mov eax,[matrix +eax* elem_size] ; eax = 10 ; Traverse all elements of 2D array mov ecx, rows * cols ; total elements = 12 mov esi,0; index mov ebx,0; accumulator traverse: add ebx,[matrix +esi* elem_size] inc esi loop traverse ; ebx = 1+2+3+...+12 = 78 mov eax,1 int 0x80 * * * ## Complete Bubble Sort Example ## Example ; File path: bubble_sort.asm ; Bubble sort algorithm section.data array dd 64,34,25,12,22,11,90,78 array_len equ($- array)/4 section.text global _start _start: mov ecx, array_len ; outer loop: n times dec ecx; outer loop only needs n-1 times outer_loop: push ecx; save outer counter mov esi,0; inner index starts from 0 mov ecx, array_len -1; inner loop count inner_loop: mov eax,[array +esi*4]; a mov ebx,[array +esi*4+4]; a[j+1] cmp eax,ebx; a > a[j+1] ? jle no_swap ; no, don't swap ; swap a and a[j+
← Assembly RecursionAssembly Numbers β†’