The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ninjarider on August 11, 2005, 01:48:08 PM

Title: arrays
Post by: ninjarider on August 11, 2005, 01:48:08 PM
in the data section how would i define an array called KD with 255 elements all which are to be a byte
Title: Re: arrays
Post by: czDrillard on August 11, 2005, 02:05:25 PM
KD db 255 dup(0)


best regards,

czDrillard
Title: Re: arrays
Post by: ninjarider on August 11, 2005, 02:09:17 PM
if i want to move a value into element 65 it would look like this right

mov kd[65], 1
Title: Re: arrays
Post by: Mark Jones on August 11, 2005, 02:19:20 PM
"KD" compiles to a MEMORY offset at runtime.

KD+0 = byte zero
KD+1 = byte one
KD+2 = byte two

Then consider this:

MOV ECX,offset KD
MOV byte ptr [ECX+0], myVal0
MOV byte ptr [ECX+1], myVal1
MOV byte ptr [ECX+2], myVal2

or

MOV ECX,offset KD
MOV byte ptr [ECX], myVal0
INC ECX
MOV byte ptr [ECX], myVal1
ADD ECX,1
MOV byte ptr [ECX], myVal2

:bg
Title: Re: arrays
Post by: ThoughtCriminal on August 11, 2005, 02:44:04 PM
Since it is created in the data section it will be created at assemble time, or link time depending on how you look at it.

KD db 255 dup(0)

mov kd[65], 1

Works just fine. 
Something I learned reading this board is that the assembler does not create variables, it just creates lables.  So any where you can us a lable you should also be able to add [ ] to access(define?) an offset from that label.

mov kd[65], 1    assembles to:

00401014 C6 05 69 30 40 00 01 mov         byte ptr (403069h)],1

opcode C6 mov r/m8,imm8
Title: Re: arrays
Post by: raymond on August 11, 2005, 03:00:45 PM
Just remember that offsets associated with labels are 0-based.

For example, KD[4] would thus be the 5th element of the KD array.

Raymond
Title: Re: arrays
Post by: hutch-- on August 13, 2005, 04:08:39 AM
ninjarider,

This is one place where learning your adressing modes for memory with the instructions makes your life a lot easier and you can routinely change the size of the elements once you get the swing of it. An array is normally a sequence of bytes in memory, even if the data size is bigger than a byte and it can be either DATA section memory or allocated memory. Except for byte data, you have to control the step from one address to the next which can be 2, 4 or 8 bytes. Now if this sounds complicated, its not in practice once you get the swing of it.

You have 4 possibles with complex addressing.

1. Base address
2. Index
3. Scale
4. Displacement

Using MASM notation in square brackets, it works like tis,


[base+index*scale+displacement]


Base and Index are 32 bit registers. Scale is either 2, 4 or 8 and displacement is an additional offset from the memory location.

This is pseudo code so that you get the basic idea.


.data?
  mybytes db 256 dup (?)          ; allocate 256 bytes in the uninitialised data secion

.code
  ......
    mov ebx, OFFSET mybytes       ; load the base address of the memory
    mov ecx, 31
    mov dl, BYTE PTR [ebx+ecx]    ; base address + 31 bytes
    mov dh, BYTE PTR [ebx+ecx+1]  ; base address +31 bytes + 1 byte displacement


Note that in the pseudo code there is no scale as it is addressing BYTE data. If it was a DWORD array, you use the scale value like this.


mov edx, DWORD PTR [ebx+ecx*4]


I have used the fully specified data size so it makes sense but note that you can use the shorter form if the assembler can work out the data size from the target register.
Title: Re: arrays
Post by: JHER VON ARBANEL on August 13, 2005, 11:26:06 PM
     Well the easiest way to define it is
        kd db 255 dup(?)
    u are defining ur variable kd with a length of 255 positions and this is consider like an array the size of the positions are "byte" or 8 bits  and in all the  bytes the caracter that will be repeated is the one that appears in ( ) when u put ? the compiler will initialize with 0 in that position of memory, in some books they say that when u put ? the data in the bytes are undefined but if u see them in memory they are zeros....
     when u refer to kd for example
mov byte ptr kd,23 ;the first position of your array is filled with 23
in others words, if u want to refer to the last position of your array u must add an offset off 254
mov byte ptr kd[254],23
or
Quotemov edi,offset kd; first you point your array
mov byte ptr [edi+254],23 ; then you add the offset to your poniter and now u can fiill it