News:

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

simple memory question.

Started by gavin, January 14, 2005, 03:32:37 PM

Previous topic - Next topic

gavin

section .data

data_items:                ;These are the data items
  dd 3,67,34,222,45,75,54,34,44,33,22,11,66,0 ;data_items is a label to a location in memory where the items will be stored
  ;dd means reserver a double 32bits


section .text 
 

  global _start           ;must be declared for linker (ld)
  _start:
  mov edi, 0                   ;move 0 into the index register why?
  mov eax,[data_items+edi*4]   ; im confused in this part



I'm using nasm but i don't think this matters muchin my question?

The whole program i copied from a book programming from the ground up works fine.

But  on the last line of code , the instruction means copy whatever is in memory where data_items points to + edi and multiply it by 4.

How does multiplying by 4 get the next number?

Thanks alot.




jimh

You didn't post the entire snippet of code, so I can't be sure what your loop is doing, but when you say:

mov eax, [data_items + edi * 4]

What you are doing is copying into the eax register the value in memory address:  [data_items + edi * 4], so at the start of the loop when edi==0, then that is equivalent to the memory address:  [data_items], (remember that 0 * 4 = 0) but as you increase the edi register by one, then the effective address jumps by four bytes at each iteration of the loop. (1 * 4 = 4, 2 * 4 = 8, etc.)  Notice the size of the data that starts at the label: data_items is a "dd".  This is a four-byte number, so to retrieve the address of each individual number in your "array" you have to more forward four bytes in memory.  This is a very common "base address + offset amount" kind of scheme.

gavin

This is a four-byte number, so to retrieve the address of each individual number in your "array" you have to more forward four bytes in memory.  This is a very common "base address + offset amount" kind of scheme.

Oh i get you now.

so if i used  mov eax, [data_items + edi + 4] would this means the same thing?

And if i declared the string as dd i would do this

mov eax, [data_items + edi * 1] ?

Thanks alot you cleared up so much for me.

I was thinking about it the wrong way.

Thanks again for your quick reply.


Kestrel

at x86 CPU, register index "addressing" unit is "byte" .

db: data byte
dw: data word  ; 1 word = 2 byte
dd: data dword ; 1 dword (dual word) = 2 word = 4 byte

So, when you want addressing to next dword, index need add 4.

in MASM, you can write below, for easier read in your eyes.

    SizeDD = 4  ; <-- define 4 to this symbol

    mov eax, data_items[ edi * SizeDD ]

gavin

I see now exactly what you guys mean.

I'm now happpy i know exactly whats going on in my program.

Thanks so much

I tried masm but moved to linux and nasm seems nice.