Assembly: arrays

Contributor Icon Contributed by William_Wilson  
Tag Icon Tagged: Computer programming  

How to create and traverse an array in assembly language


Assembly doesn’t care what your variable is, it only cares how many bytes it needs. creating and traversing an array is thus a little confusing, but very simple as long as each element is the same size.

This example is well commented and creates and the 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 array into BX again, and add 4 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)

 

7 Comments -


  1. menan said on October 18, 2009

    thanks bro

  2. Anonymous said on April 8, 2010

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

  3. Anonymous said on April 8, 2010

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

  4. Anonymous said on April 8, 2010

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

  5. Sagar Ragas said on December 5, 2010

    thank you

  6. Mitsos said on March 18, 2011

    nice & short

  7. Shai said on December 25, 2011

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

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -