gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:30 25 4
gpt4 key购买 nike

我仍在学习 NASM Assembly、32 位 Ubuntu 中的递归,我现在正在尝试递归地添加数组中的所有元素。该数组的元素均为 4 字节。

我想出了一个似乎可行的解决方案。

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

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

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

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

; --------------------------------------------------------------------------
; 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

最佳答案

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

size:   equ ($-array) / 4

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

自结尾:

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

从头开始:

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

关于arrays - 在 NASM Assembly 中递归添加数组中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19416441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com