News:

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

Memory Operand as Index

Started by n00b!, September 02, 2008, 06:08:34 PM

Previous topic - Next topic

n00b!

.

DoomyD

The code you suggested(using registers) should work, although personally, I would skip ecx.
mov eax, [var1]
mov eax, [eax+ebx+5]

BogdanOntanu

Quote
Can I somehow take var1 as intermediate value like

eax = 4 Bytes from (4 Bytes from (offset var1) + ebx + 5)


If you want to have a double indirection then NO you can not do this.
The CPU can not perform a double indirection.

This is a confusion partly generated in newbies minds because MASM does accept this syntax:

mov eax,var1


And in fact this means:

mov eax,[var1]


But please do remember that MASM does this in order to help you from typing [] around variables all the time (almost).
For the same reason you have to type "offset" when you want to obtain the address of a variable.

Quote
Or do I have to use registers like

mov ecx, [var1]
mov eax, [ebx + ecx + 5]


Yes, in this case you must do that.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

raymond

As far as I know, MASM would also allow

mov eax,var1[ebx+5]

It would load EAX with the dword residing at offset ebx+5 from the start of var1 if the latter is a global variable where the data you want to retrieve is residing. It may not be acceptable if var1 is a LOCAL variable.

In MASM, mov ecx,[var1] is the same as mov ecx,var1 and would load ECX with the data residing in that variable, and not its address. This would be correct only if var1 contains the address of a memory location from where you want to retrieve data.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

zooba

Local variables and parameters are basically text-equates for DWORD PTR [ebp+x] (assuming the type is DWORD), so when you write:

mov eax, aVariable[4]
You're getting:
mov eax, DWORD PTR [ebp+8][4]

MASM treats consecutive sets of square brackets as addition, so it re-interprets the second snippet above as:
mov eax, DWORD PTR [ebp+8+4]

Nested square brackets are ignored, so if you put multiple brackets around a variable the following happens:
mov eax, DWORD PTR [aVariable]
; becomes
mov eax, DWORD PTR [DWORD PTR [ebp+8]]
; becomes
mov eax, DWORD PTR [ebp+8]


Personally I prefer the interpretation where the variable name is the offset and to get the value you have to explicitly dereference it (I believe FASM works like this?). All it takes though is knowing how it works.

Cheers,

Zooba :U

hutch--

It actually helps to look at the available opcodes. There is an opcode for having a non register as the base address and this is useful for things like the address of a table as it saves you a register.

something like [table+index*multiplier] where index is a register and multiplier is and one of 1 2 4 8. In other opcode combinations the address is treated like an displacement.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php