在NASM Assembly中递归添加数组中的所有元素

时间:2021-09-18 15:42:04

Still on my way to learn recursion in NASM Assembly, 32-bit Ubuntu, I am now trying to add all the elements in an array recursively. The array's elements are all 4-bytes each.

我仍然在学习32位Ubuntu的NASM程序集中的递归,我现在试图以递归方式添加数组中的所有元素。数组的元素都是4字节。

I came up with a solution that seems to work.

我想出了一个似乎有用的解决方案。

Basically, to add the elements in an array, I somehow need to count them, right? So I have ESI as my counter. However, this register needs to be set to 0 at the beginning of the function - but I don't think there is any way I can tell whether the current function call is the first one, or the second or third... So to fix this, I have two functions: the initial call, and the recursive call. The first one sets ESI to 0 and then calls the recursive call. The elements are all added to EAX, which is also set to 0 in the initial call..

基本上,要在数组中添加元素,我需要计算它们,对吧?所以我有ESI作为我的柜台。但是,这个寄存器需要在函数开头设置为0 - 但我不认为有任何方法可以判断当前函数调用是第一个,还是第二个或第三个......所以解决这个问题,我有两个函数:初始调用和递归调用。第一个将ESI设置为0,然后调用递归调用。这些元素都添加到EAX中,在初始调用中也设置为0。

But I'm concerned with it because it is somewhat different from two recursive functions I've done before:

但是我很担心它,因为它与我以前做过的两个递归函数有些不同:

Because, first of all, I'm using two functions, one for the start, and another for the actual recursive part. Also, I am using a counter, which feels awfully a lot like an iterative solution.

因为,首先,我使用两个函数,一个用于开始,另一个用于实际的递归部分。此外,我正在使用一个计数器,这非常像迭代解决方案。

So my questions are: is there a solution that resembles better the two recursive functions I posted above? Can my current solution be considered recursive?

所以我的问题是:是否有一个类似于我上面发布的两个递归函数的解决方案?我当前的解决方案可以被认为是递归的吗?

; --------------------------------------------------------------------------
; Recursive function that adds all the elements in an array into EAX.
; The array's elements are 4-bytes each.
; --------------------------------------------------------------------------

SECTION .data
    array:  dd  1,5,3,7,4,8,5,2
    size:   equ $-array

SECTION .text
    global  main
    main:

    ; ------------------------------------------------------------------
    ; * Main
    ; ------------------------------------------------------------------
    call    addVector
    breakpoint:     ; Used for GDB to inspect the result later

    ; ------------------------------------------------------------------
    ; * Exit
    ; ------------------------------------------------------------------
    mov     EAX,0
    int     0x80

    ; ------------------------------------------------------------------
    ; * Initial function call, before doing the recursive calls
    ;   Sets ESI to 0, which will be used to count the array's elements
    ;   Also sets EAX to 0, for storing the result
    ; ------------------------------------------------------------------
    addVector:
    push    ESI
    mov     ESI,0
    mov     EAX,0
    call    recursiveCall
    pop     ESI
    ret

    ; ------------------------------------------------------------------
    ; * Recursive part of the function
    ;   Adds to EAX to current element, and increases the ESI counter by
    ;   4 (because the array's elements are 4-bytes each).
    ;   If the counter happens to be >= the array's size, stop.
    ; ------------------------------------------------------------------
    recursiveCall:
    cmp     ESI,size
    jge     endRecursiveCall
    add     EAX,[array + ESI]
    add     ESI,4
    call    recursiveCall
    endRecursiveCall:
    ret

1 个解决方案

#1


2  

First, your definition of size is wrong, your way will give you the total bytes of the array; this is not what you want. Your array is made of DWORDS, you want to know the total elements, so we divide by 4 (The size of a DWORD):

首先,你的大小定义是错误的,你的方式将给你数组的总字节数;这不是你想要的。你的数组由DWORDS组成,你想知道总元素,所以我们除以4(DWORD的大小):

size:   equ ($-array) / 4

Two ways of doing this, start from the end of the array or the beginning:

两种方法,从数组的末尾或开头开始:

From end:

array:  dd  1,5,3,7,4,8,5,2
size:   equ ($-array) / 4

SECTION .text
global  main
main:

    xor     eax, eax                        ; clear out eax
    mov     esi, size - 1                   ; set our index to array end
    call    recursiveCall

    push    eax
    push    fmtint
    call    printf
    add     esp, 4 * 2

.exit:
    call    exit

recursiveCall:
    add     EAX, dword[array + 4 * ESI]
    dec     ESI
    js      .endRecursiveCall
    call    recursiveCall

.endRecursiveCall:
    ret

From start:

SECTION .text
global  main
main:

    xor     eax, eax                        ; clear out eax
    xor     esi, esi                        ; set out index to start of array
    call    recursiveCall

    push    eax
    push    fmtint
    call    printf
    add     esp, 4 * 2

.exit:
    call    exit

recursiveCall:
    add     EAX, dword[array + 4 * ESI]
    inc     esi
    cmp     esi, size - 1
    jg      .endRecursiveCall
    call    recursiveCall

.endRecursiveCall:
    ret

#1


2  

First, your definition of size is wrong, your way will give you the total bytes of the array; this is not what you want. Your array is made of DWORDS, you want to know the total elements, so we divide by 4 (The size of a DWORD):

首先,你的大小定义是错误的,你的方式将给你数组的总字节数;这不是你想要的。你的数组由DWORDS组成,你想知道总元素,所以我们除以4(DWORD的大小):

size:   equ ($-array) / 4

Two ways of doing this, start from the end of the array or the beginning:

两种方法,从数组的末尾或开头开始:

From end:

array:  dd  1,5,3,7,4,8,5,2
size:   equ ($-array) / 4

SECTION .text
global  main
main:

    xor     eax, eax                        ; clear out eax
    mov     esi, size - 1                   ; set our index to array end
    call    recursiveCall

    push    eax
    push    fmtint
    call    printf
    add     esp, 4 * 2

.exit:
    call    exit

recursiveCall:
    add     EAX, dword[array + 4 * ESI]
    dec     ESI
    js      .endRecursiveCall
    call    recursiveCall

.endRecursiveCall:
    ret

From start:

SECTION .text
global  main
main:

    xor     eax, eax                        ; clear out eax
    xor     esi, esi                        ; set out index to start of array
    call    recursiveCall

    push    eax
    push    fmtint
    call    printf
    add     esp, 4 * 2

.exit:
    call    exit

recursiveCall:
    add     EAX, dword[array + 4 * ESI]
    inc     esi
    cmp     esi, size - 1
    jg      .endRecursiveCall
    call    recursiveCall

.endRecursiveCall:
    ret