Best way to define and access elements of single dimension Array?

Started by ilian007, November 13, 2009, 08:37:10 PM

Previous topic - Next topic

ilian007

Hello,
If I have to define and access elements of single dimension array (all zeroes in the beining) in 16bit is that the best and easy way:

Array DW DUP 500 (0)

1. If we want index 245 in AX we change "index" with 245 and we have it ?
MOV BX, INDEX                 
MOV AX, ARRAY [BX]     ----> AX has the index number but not its value ?

what if we want get the value on the 245th position in AX  ?

Thank you

dedndave

if the index is 245, that means we want the 245th element
really, the first element is # 0 - so we would want element number 244 to get the 245th element
because each element is two bytes, we have to double it to calculate the address:

        mov     bx,Index
        shl     bx,1
        mov     ax,Array[bx]

i would use names other than "Index" or "Array" - try to make them unique

ilian007


MichaelW

ilian,

There is an error in your array definition. It should be:

Array DW 500  DUP(0)

In high-level languages you access an array element by its index. In assembly language you access an array element by its address. The best way to access an array is to use 32-bit instructions, where more operations can be packed into an indirect memory operand, and where you are not limited to using a base or index register, or one of each. One of the more useful additions for the 32-bit instructions was the ability to include a scale factor in an indirect memory operand. The attachment contains simple demonstrations of array access, one using 16-bit instructions, and one using 32-bit instructions.

eschew obfuscation

ilian007

Thanks Michael :)

I found a website "ther art of assembly" it is very helpfull.

I think I start playing arround with arrays a little bit easier :)

However I have problem with a code I posted a question about. Dont know what is the error everything seems normal :(

thanks