The MASM Forum

General => The Campus => Topic started by: squall867 on June 09, 2012, 02:59:31 AM

Title: N-1: why does not work?
Post by: squall867 on June 09, 2012, 02:59:31 AM
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

?
Title: Re: N-1: why does not work?
Post by: dedndave on June 09, 2012, 03:09:44 AM
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
Title: Re: N-1: why does not work?
Post by: squall867 on June 09, 2012, 03:16:22 AM
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.
Title: Re: N-1: why does not work?
Post by: dedndave on June 09, 2012, 03:27:45 AM
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
Title: Re: N-1: why does not work?
Post by: squall867 on June 09, 2012, 03:30:43 AM
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
Title: Re: N-1: why does not work?
Post by: dedndave on June 09, 2012, 03:38:04 AM
yah - that's not a legal addressing mode   :P
Title: Re: N-1: why does not work?
Post by: clive on June 09, 2012, 06:14:38 AM
mov dword ptr [EBX+4*EDI-4],5
Title: Re: N-1: why does not work?
Post by: MichaelW on June 09, 2012, 06:59:36 AM
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

Title: Re: N-1: why does not work?
Post by: squall867 on June 09, 2012, 08:11:20 AM
Understood, thanks to everybody! :bgrin: