反转装配中的阵列 [英] Reversing an array in assembly

查看:103
本文介绍了反转装配中的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何以一种尽可能灵活的方式来反转装配中的数组.到目前为止,我的代码是:

I'm trying to figure out how to reverse an array in assembly in a way that makes it as flexible as possible. The code I have so far is this:

; 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

这有效,但仅当数组具有偶数个元素时才有效.我该怎么做才能使其也适用于奇数?

This works, but only if the array has an even number of elements. What should I do to make it work for an odd number as well?

推荐答案

通常,最好将其想象为2个指针.第一个指针从数组的开始处开始并向结尾移动,第二个指针从数组的末处开始并向开始移动.

In general it's best to imagine it as 2 pointers. The first pointer begins at the start of the array and moves towards the end, and the second pointer begins at the end of the array and moves towards the start.

如果数组中的项目数为偶数,则最终start将高于end.如果数组中的项目数为奇数,则最终start将等于end.基本上,在C语言中,您会寻找类似while(start < end) { ... }的东西.

If there are an even number of items in the array, eventually start will be higher than end. If there are an odd number of items in the array, eventually start will be equal to end. Basically, in C you'd be looking for something like while(start < end) { ... }.

对于汇编(NASM语法),它可能看起来像这样:

For assembly (NASM syntax), this might look a bit like this:

;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

这篇关于反转装配中的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆