Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在汇编中反转数组_Arrays_Assembly_Reverse_Masm_Irvine32 - Fatal编程技术网

Arrays 在汇编中反转数组

Arrays 在汇编中反转数组,arrays,assembly,reverse,masm,irvine32,Arrays,Assembly,Reverse,Masm,Irvine32,我正试图找出如何以一种尽可能灵活的方式反转汇编中的数组。到目前为止,我掌握的代码如下: ; 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, 2

我正试图找出如何以一种尽可能灵活的方式反转汇编中的数组。到目前为止,我掌握的代码如下:

; 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

这是可行的,但仅当数组的元素数为偶数时才可行。我该怎么做才能使它也适用于奇数?

一般来说,最好将它想象为两个指针。第一个指针从数组的开头开始并向末尾移动,第二个指针从数组的末尾开始并向开头移动

如果数组中的项目数为偶数,则
start
最终将高于
end
。如果数组中的项目数为奇数,则最终
start
将等于
end
。基本上,在C语言中,您需要查找类似于
的内容,而(start

对于assembly(NASM语法),这可能看起来有点像:

;Reverse array of 32-bit "things" (integers, pointers, whatever)
;
;Input
; esi = address of array
; ecx = number of entries in array

reverseArrayOf32bit:
    lea edi,[esi+ecx*4-4]      ;edi = address of last entry
    cmp esi,edi                ;Is it a tiny array (zero or 1 entry)?
    jnb .done                  ; yes, it's done aleady

.next:
    mov eax,[esi]              ;eax = value at start
    mov ebx,[edi]              ;ebx = value at end
    mov [edi],eax              ;Store value from start at end
    mov [esi],ebx              ;Store value from end at start

    add esi,4                  ;esi = address of next item at start
    sub edi,4                  ;edi = address of next item at end
    cmp esi,edi                ;Have we reached the middle?
    jb .next                   ; no, keep going

.done:
    ret

您可以使用堆栈将其反转:将其全部按下,然后将其弹出,其顺序将相反;)我不确定任务是否允许这样做。我必须使用SIZEOF、LENGTHOF、TYPE等函数来完成这项工作。基本上我必须手工交换它们。mov ebx,len与注释不匹配,这意味着你有一个mov ebx,0。看起来当前代码只会将数组反转两次,最终返回到开始的位置。为了避免在mov ecx,len之后出现这种情况,应该有一个shr ecx,1。对于奇数长度,中间值不交换。