News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

arrays

Started by ninjarider, August 11, 2005, 01:48:08 PM

Previous topic - Next topic

ninjarider

in the data section how would i define an array called KD with 255 elements all which are to be a byte

czDrillard

KD db 255 dup(0)


best regards,

czDrillard

ninjarider

if i want to move a value into element 65 it would look like this right

mov kd[65], 1

Mark Jones

"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
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

ThoughtCriminal

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

raymond

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
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

JHER VON ARBANEL

     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