UMBC | CMSC 313 -- Arrays | Previous | Next |
  | ||||||
The starting point is given a label as the name of the array. Like in C, indexing into the array is 0-origin indexing, which means the first element is the 0th element. (Actually, C does it the way assembly language does it, because because so much of C is directly related to the assembly language implementation of C's features.)
To locate a specific element in the array, we take the size of the element and multiple it by its index number. The second element (with the subscript of 1) is 1 times the sizeOf( element ) plus the starting address or:
N EQU 50 ; array holds fifty items S EQU 4 ; each item is four bytes long Ary resd N * S ; in C, this is int Ary[50]; ; This is 800 bytes long.
Higher-dimensional arrays can be considered to be arrays of arrays.
MOV [counts + 5], 27 ; Remember it is zero-based!
MOV [bigcounts + 2 * 5], 27 ; Remember it is zero-based, ; and we must get to the correct byte.
EBX and EBP are the base registers. EBP is called the base pointer and is used only in stack operations. Use BP/EBP in the stack only, NEVER for general-purpose indexing!
EDI is used to hold the destination index, ESI holds the source index and EBX is used as a general purpose index register. ESI and EDI are used in string instructions as source and destination, but other instructions can use them as general indexing registers. Additionally, the can be used as general registers, but only as 32-bit registers!
MOV EBX, 5 ; the index MOV [counts + EBX], 27 ; This will allow us to use loops ; and simply update the contents of ; EBX each time
MOV EBX, 0 MOV ECX, 10 more: MOV [ counts + EBX ], 0 INC EBX LOOP more
section .data wArray DW 1234h, 2345h, 3456h, 4567h, 5678h, 6789h, 789Ah, 89ABh, 9ABCh, 0ABCDh section .bss section .text global main ;must be declared for linker (ld) main: ;tell linker entry point MOV EDI, 0 MOV ECX, 10 more: MOV word [ wArray + EDI ], 0 ADD EDI, 2 ; Remember to byte adjust! LOOP more mov ebx,0 ;successful termination of program mov eax,1 ;system call number (sys_exit) int 0x80 ;call kernel
MOV EDI, counts MOV ECX, 10 more: MOV [ EDI ], 0 ADD EDI, 2 ; Remember to byte adjust! LOOP more
Where:
W[ N + 3 ] = 27;
mov edx, [ N ] mov [ W + 6 + 2 * edx ], 27That was confusing! The W is the address of the array, 6 is 3 * 2 (two bytes per word), and then the number of word in N has to be adjusted to be the number of bytes in N words.
mov eax, msg mov eax, [eax + 4 ] ; add 4 to eax(!) mov ebx, [eax + 2 * eax ] ; ebx = eax * 3 mov ebx, [ 4 * eax ] ; ebx = eax * 4 mov ebx, [eax + 4 * eax ] ; ebx = eax * 5