Die Umkehrung ein array in der Montage

Ich versuche herauszufinden, wie du ein array in einer Baugruppe in einer Weise, die macht Sie so flexibel wie möglich. Der code, den ich habe, so weit ist diese:

; This program takes an integer array and reverses it's elements, using a loop, the SIZE, TYPE and LENGTHOF
; operators.

TITLE lab4              (lab4.asm)

INCLUDE Irvine32.inc

.data

    arr DWORD 1h, 2h, 3h, 4h, 5h, 6h    ; Array of integers with 6 elements.
    len DWORD LENGTHOF arr /2          ; The length of the array divided by 2.
    ;rng    DWORD LENGTHOF arr          ; The complete length of the array.

.code
main PROC

    mov eax, len    ; Moves the length (divided by 2) into the eax register.
    mov ebx, len    ; Sets the ebx register to 0 to serve as the counter.
    mov ecx, len    ; Loads the length of the array into the ecx register.
    mov edx, len    ; Sets a counter that starts at the end of the array.

    dec edx

    ; Start of the loop
    L1: 

    mov eax, arr[esi + (TYPE arr * ebx)]    ; Assigns to eax the value in the current beginning counter.

    xchg eax, arr[esi + (TYPE arr * edx) ]  ; Swaps the value in eax with the value at the end counter.

    mov arr[esi + (TYPE arr * ebx)], eax    ; Assigns the current beginning counter the value in eax.

    dec edx                 ; Decrements the end counter by 1.
    inc ebx                 ; Increments the beginning counter by 1.
    loop    L1              
    ; end of the loop

    mov ecx, LENGTHOF arr
    mov ebx, 0

    ; Start of loop
    L2:                                     ; Loop that runs through the array to check and make sure
    mov eax, arr[esi + (TYPE arr * ebx)]    ; the elements are reversed.
    inc     ebx
    call    DumpRegs
    loop    L2          
    ; End of loop

    exit
main ENDP

END main

Dies funktioniert jedoch nur, wenn das array eine gerade Anzahl von Elementen. Was sollte ich tun, um es Arbeit, die für eine ungerade Zahl ist, wie gut?

  • Sie können den Stapel, um es rückgängig zu machen: drücken Sie alle, dann klappt es, es wird in umgekehrter Reihenfolge 😉
  • Ich bin mir nicht sicher, ob die Zuordnung zulassen würde, dass. Ich habe zum verwenden von Funktionen wie SIZEOF, LENGTHOF, TYP und Art, dies zu tun. Ich habe im Grunde tauschen Sie alle von hand.
  • mov ebx,len nicht mit der Kommentar-was bedeutet, Sie haben eine mov ebx,0. Es scheint, wie die aktuelle code würde nur in umgekehrter array zweimal, endet wieder dort, wo Sie begonnen. Um dies zu vermeiden nach mov ecx,len , es sollte ein shr ecx,1. Für ungerade Länge, der mittlere Wert wird nicht getauscht.
InformationsquelleAutor user1675108 | 2014-03-01
Schreibe einen Kommentar