The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: n00b! on September 02, 2008, 06:08:34 PM

Title: Memory Operand as Index
Post by: n00b! on September 02, 2008, 06:08:34 PM
.
Title: Re: Memory Operand as Index
Post by: DoomyD on September 02, 2008, 07:39:35 PM
The code you suggested(using registers) should work, although personally, I would skip ecx.
mov eax, [var1]
mov eax, [eax+ebx+5]
Title: Re: Memory Operand as Index
Post by: BogdanOntanu on September 02, 2008, 07:57:36 PM
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.
Title: Re: Memory Operand as Index
Post by: raymond on September 03, 2008, 03:11:22 AM
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.
Title: Re: Memory Operand as Index
Post by: zooba on September 03, 2008, 05:12:34 AM
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
Title: Re: Memory Operand as Index
Post by: hutch-- on September 03, 2008, 05:24:08 AM
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.