Hi, I got an external variable, say int N=4.
Now, in my embedded asm snippet I do something like
mov ECX, N-1
how come in ECX I do not find 3 but 1228?
Is there a way to obtain this effect instead of
dec N
mov ECX, N
?
if it is a variable, as in a memory operand, there is no instruction to load it decremented
however...
mov ecx,N
dec ecx
DEC ECX is a single-byte instruction that doesn't take many clock cycles :P
Omg you're right! So it would be better to load N in a register so I can do that operation, right?
I need to do it many times in my code.
that will depend on whether or not you want the value in memory to be decremented
this code does not alter the stored value
mov ecx,N
dec ecx
if you are doing this in a loop - it may be best to simply push and pop the count
mov ecx,N
loop00: push ecx
;do stuff
pop ecx
dec ecx
jnz loop00
Mmm...this doesn't work either:
mov EDI,N //loading N value in EDI
mov [EBX+4*(EDI-1)],5
basically I want to store 5 in memory, according to that expressione...but it does not work.
If it is
mov [EBX+4*EDI],5
everything goes well instead..T_T
yah - that's not a legal addressing mode :P
mov dword ptr [EBX+4*EDI-4],5
To answer the first question, it does not work because assembly is not a high-level language.
An indirect memory operand with 32-bit registers can specify one base and one index register, with a optional scaling factor of 1, 2, 4, or 8 on the index register, and any number of displacements. The LEA instruction can be used to calculate the address of an indirect memory operand, or it can be used to harness the processor's addressing mechanism to do one shift, two additions, and one move in a single instruction. I originally had a third example in this code, but it made the solution too easy.
;==============================================================================
include \masm32\include\masm32rt.inc
;==============================================================================
.data
array dd 0,1,2,3,4,5,6,7
.code
;==============================================================================
start:
;==============================================================================
mov esi, 4
mov edi, 2
mov eax, array[esi+edi*4-4]
printf("%d\n", eax)
lea eax, [esi+edi*4-4]
printf("%d\n\n", eax)
inkey
exit
;==============================================================================
end start
Understood, thanks to everybody! :bgrin: