A
A
Alertoso2021-04-11 12:37:31
assembler
Alertoso, 2021-04-11 12:37:31

Why subtract 4 and multiply by 4?

I don’t really understand why 4 is subtracted and multiplied by 4, why is this a number, about pushad, do I understand correctly that it puts RON values ​​on the stack, that is, the address of the array in esi, and in ecx the size of the array?

Procedure Bubble(var A: array of Integer; M: Integer);
Asm
    pushad

    dec   ecx
    mov   esi, 1

@1:
    mov   edi, ecx
    mov   edx, [eax+edi*4]

@2:
    mov   ebx, [eax+edi*4-4]
    cmp   ebx, edx
    ja    @3
    mov   [eax+edi*4-4], edx
    mov   [eax+edi*4], ebx
    jmp   @4

@3:
    mov   edx, ebx

@4:
    dec   edi
    cmp   edi, esi
    jnl   @2

    inc   esi
    cmp   esi, ecx
    jle   @1

    popad
End;

And how can you redo it without a procedure, but just to use it in the main program

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2021-04-11
@Alertoso

Procedure Bubble(var A: array of Integer; M: Integer);
# var A: array of Integer - ссылка на A, передаётся в eax
# M: Integer - размер массива, передаётся в ecx
Asm
    pushad
    dec   ecx
    mov   esi, 1
@1:
    mov   edi,  ecx
    mov   edx, [eax+edi*4] #  mov   edx, A[edi], 4-байтные int
@2:
    mov   ebx, [eax+edi*4-4] # ebx, A[edi - 1]
    cmp   ebx, edx
    ja    @3
    mov   [eax+edi*4-4], edx #mov   A[edi - 1], edx
    mov   [eax+edi*4], ebx  # A[edi], ebx  # вот и поменялись местами
..............................

Everything seems to be clear.
You can redo it, but why - there is a quicksort, use it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question