This tech-recipe explains how to create and traverse an array in assembly language.
Assembly does not care what your variable is. It only cares how many bytes it needs. Creating and traversing an array is thus a little confusing, but it is very simple as long as each element is the same size.
This example is well commented and creates and then displays a 20-element array of integers.
*NOTE that the example loads the array, then displays it backwards. If you wish to display it forward, load the offset of the array into BX again, and add four instead of subtracting it.
TITLE ARRAYS ARRAYS.ASM
Creates an array and traverses it, printing the values.
array DD 20 DUP (?)
;80 bytes total or 20 4 byte Long Integers
mov BX,OFFSET array ;get start of array (array)
mov AX,1 ;set AX to 1
mov [BX],AX ;[BX] - what BX points to
add AX,1 ;increment AX
add BX,4 ;move BX to next element
;(array[i], and ++i)
jle fill ;if less than 20 elements entered, add another
sub BX,4 ;update BX - array pointer
sub AX,1 ;decrement count
jg next ;if not finished do another pass
Questions/Comments: [email protected]
-William. § (marvin_gohan)
Follow the reactions below and share your own thoughts.