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: [email protected]
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

  • http://tinrit.com/ menan

    thanks bro

  • Anonymous

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

  • Anonymous

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

  • Anonymous

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

  • http://www.facebook.com/people/Sagar-Ragas/100000293917512 Sagar Ragas

    thank you

  • Mitsos

    nice & short

  • Shai

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

  • Mary C. Brown

    Thanks! Helped alot!