Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring Array?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi, im new to at MASM, and would like to know to declare an array.Can you show some examples?
 
Here's an uninitialized array:

array1 db 10 dup (?)

Let's analyze this one by one:

array1 - the label for this array. This is the name you use to refer to this array
db - means "define byte." This means that the array element's size is a byte. 'dw' would mean that the array elements are words (two bytes), 'dd' double words (four bytes).
10 dup ( ) - means that we are repeating ('dup'licating) the value in parenthesis ten times. So we have ten '?' values in our array.
? - means that we don't care what values the assembler puts in there, because the array is unitialized anyway.

Now here are some problems for you:
what does this mean?
array2 dw 20 dup (100)

How large is this array, in bytes?
array3 dw 10 dup (100,120)

Just post your answer, or if you don't know just ask me.... "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
hi,
thanks for your help , let me see.
array3 dw 10 dup (100,120)

the array name is "array3". dw is datatype. An array of 10 element is defined and 100, 200 is the values being initialized. Am I correct? Also how do I access and asign a
value tot it?

Another question, what is the difference between these?
what are they use for and how do I use them?
EAX
EBX
ECX
EDX
and
AX
BX
CX
DX ?
Thankyou so far for the help, I really appreciate it.
My programming background is in c++ and really neew to MASM.

 
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."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top