Assembly: Arrays

Posted April 17, 2006 by William_Wilson in Computer programming

Last Updated on

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.

Creates an array and traverses it, printing the values.

array DD 20 DUP (?)
;80 bytes total or 20 4 byte Long Integers

INCLUDE io.mac
main PROC

mov BX,OFFSET array ;get start of array (array[0])
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)
cmp AX,20
jle fill ;if less than 20 elements entered, add another

putLint [BX-4]
sub BX,4 ;update BX - array pointer
sub AX,1 ;decrement count
cmp AX,1
jg next ;if not finished do another pass

main ENDP
END main

Questions/Comments: [email protected]
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

Leave a Reply