Assembly: Arrays

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
COMMENT |
Creates an array and traverses it, printing the values.
|
.MODEL SMALL
.STACK 100H
.DATA

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

.CODE
.486
INCLUDE io.mac
main PROC
.STARTUP

mov BX,OFFSET array ;get start of array (array[0])
mov AX,1 ;set AX to 1
fill:
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

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

.EXIT
main ENDP
END main

Questions/Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

8 Responses to “Assembly: Arrays”

  1. October 18, 2009 at 10:07 pm, menan said:

    thanks bro

    Reply

  2. April 08, 2010 at 7:43 pm, Anonymous said:

    Find the arithmetic mean of the squares of nonzero elements of the sequence. An array of words.

    Reply

  3. April 08, 2010 at 8:00 pm, Anonymous said:

    Find the arithmetic mean of the squares of nonzero elements of the sequence. An array of words.

    Reply

  4. April 08, 2010 at 8:05 pm, Anonymous said:

    Find the arithmetic mean of the squares of nonzero elements of the sequence. An array of words. in asm!!!

    Reply

  5. December 05, 2010 at 1:29 pm, Sagar Ragas said:

    thank you

    Reply

  6. March 18, 2011 at 11:14 pm, Mitsos said:

    nice & short

    Reply

  7. December 25, 2011 at 8:33 am, Shai said:

    Thank you for this toturial, it was very helpful! :)

    Reply

  8. November 16, 2012 at 6:00 pm, Mary C. Brown said:

    Thanks! Helped alot!

    Reply

Leave a Reply