Author Topic: N-1: why does not work?  (Read 5870 times)

squall867

  • Guest
N-1: why does not work?
« 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

?

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: N-1: why does not work?
« Reply #1 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...
Code: [Select]
        mov     ecx,N
        dec     ecx
DEC ECX is a single-byte instruction that doesn't take many clock cycles   :P

squall867

  • Guest
Re: N-1: why does not work?
« Reply #2 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.

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: N-1: why does not work?
« Reply #3 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
Code: [Select]
        mov     ecx,N
        dec     ecx

if you are doing this in a loop - it may be best to simply push and pop the count
Code: [Select]
        mov     ecx,N

loop00: push    ecx

;do stuff

        pop     ecx
        dec     ecx
        jnz     loop00

squall867

  • Guest
Re: N-1: why does not work?
« Reply #4 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

dedndave

  • Member
  • *****
  • Posts: 8827
  • Still using Abacus 2.0
    • DednDave
Re: N-1: why does not work?
« Reply #5 on: June 09, 2012, 03:38:04 AM »
yah - that's not a legal addressing mode   :P

clive

  • Regular Member
  • *
  • Posts: 27
  • Toolsmith and Assembler coder (x86,ARM,MIPS,68K)
Re: N-1: why does not work?
« Reply #6 on: June 09, 2012, 06:14:38 AM »
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

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: N-1: why does not work?
« Reply #7 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.
Code: [Select]
;==============================================================================
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

  • Guest
Re: N-1: why does not work?
« Reply #8 on: June 09, 2012, 08:11:20 AM »
Understood, thanks to everybody! :bgrin: