News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

N-1: why does not work?

Started by squall867, June 09, 2012, 02:59:31 AM

Previous topic - Next topic

squall867

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

?

dedndave

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

squall867

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.

dedndave

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

squall867

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

dedndave

yah - that's not a legal addressing mode   :P

clive

mov dword ptr [EBX+4*EDI-4],5
It's a pity the clowns in Washington DC don't understand the Constitution as well as Edward Snowden

MichaelW

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

Well Microsoft, here's another nice mess you've gotten us into.

squall867

Understood, thanks to everybody! :bgrin: