You are here: Articles > Programming > Programming

 See more articles about "Programming "

Assembly: arrays

 

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)

 

Also see ...

Flash / Actionscript: Create an Array of Objects from a Unique Class
H3When starting in AS2, I found it difficult to make an array of objects after creating my own class. Hopefully, this little tutorial will make it easier for others./H3PI spent 8 hours trying to figure this out initially. To me actionscript handles this a little differently than other progra

Flash/Actionscript Basics: If - Then - Else Statement Syntax
H3Creating a basic if then statement in flash or actionscript is easy. Here is the correct syntax. /H3PThe basic syntax is the following: br / br /span style="font weight: bold"if (condition) br / { br / statement(s); br / } br //span br / br /Here i

Assembly: division
H3How to divide large numbers with assembly language, and where the results end up./H3Pnumbers are most commonly divided by smaller numbers (eg a 16 bit number divided by an 8 bit number) br / br /The numerator must be placed in specific registers and the denomenator is supplied as a singl

Assembly: Bit Shifting
H3Shift bits left or right by a desired numebr of places/H3P br / br / br /Bit shifting is an easy task. br /Shift left (in this case by 8): br /div class="code" br /shl EAX,8 br /P br /or to Shift right (in this case by 8): br /div class="code" br /

Assembly: Bit Rotating
H3Shift bits left or right by a desired numebr of places, with a wrap around to the other side/H3PThis method saves all bits. br /Say you shifted AX by 8, in either direction the new AX value would hold AH as the old AL and AL would become the old AH. This is done with a temporary copy of t