Thursday, October 24, 2024
HomeComputer programmingAssembly: Arrays

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)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!