Well array3 has TWENTY (20) elements...
That's because what's inside the dup () are TWO elements which are duplicated TEN (10) times, so that there are in fact TWENTY elements. This means that in fact we have an array that's like this:
array3 dw 100,200,100,200,100,200,100,200,100,200,100,200,100,200,100,200,100,200,100,200
equivalent to a C defined array:
int array3[]={100,200,100,200,100,200,100,200,100,200,100,200,100,200,100,200,100,200,100,200};
Similarly:
array4 dd 5 dup (100,200,300,400)
is also twenty elements. Each element is a double word or four bytes so that the entire array is 80 bytes.
So: how do you use an array? Quite similar to a C array but with some limitations....
For example, to get the first element in a C array:
x=array3[0];
Asm:
mov ax,array3[0]
mov x,ax
this simply puts the first element into the variable x.
BUT:
x=array3[1];
y=array3[4];
Asm:
mov ax,array3[2]
mov x,ax
mov ax,array3[8]
mov y,ax
That's because we defined array3 in words, which are two bytes long. The value in assembly's brackets are always assumed to be byte lengths. So we need to multiply by two because the array elements are twice the size the assembler expects.
AND:
x=array3[y];
Asm:
mov bx,y
shl bx,1
mov ax,array3[bx]
mov x,ax
This assembly section first loads the value of y into a base register, bx. That's because you can only put the base registers in the brackets of ASM arrays. You can use the index registers si or di also if you're keeping a value in bx. Also, take note that we 'shl' or shift left the value in bx. That's because array3 is in WORDS which are two bytes long. So by shifting left by 1, we essentially multiply this value by two, the size of each element. Finally we get the array entry into ax which we then copy to variable x.
Take especial note that bp is rarely used as a base register in assembly programming because bp refers to the STACK segment (SS) and not the DATA segment (DS). In general assembly separates SS from DS although HLL compilers make them the same because they are addicted to bp. A bit of ancient history here, because HLL compiler writers in the old days were assembly programmers, they recognized that bp was often left unused by assembly programs, so they used it in their HLL compilers so as to reduce register usage conflicts.
When a register has an 'e' before it (e.g. eax for ax, ebx for bx, etc.) it means that we are using a 32-bit (four bytes, two words, a C long) register, as opposed to a 16-bit register (two bytes, one word, a C int). When we use ax, we are simply accessing the lower word of eax. In general, if you are a beginner you will want to concentrate on using the 16-bit registers properly first. Of course there are some limitations on 16-bit registers that are lifted; for instance, when using 32-bit registers (the 'e' versions) you can put any register in assembly brackets, not just the base and index registers.
"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."