- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我仍在学习 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/
我被告知“汇编”是您在文件中编写的内容,让您的“汇编程序”将其转换为二进制代码。 但我看到这两个术语在各种作品中混合搭配。我什至听说你编写了“汇编器”,然后“汇编器”使其可执行。 正确的用词是什么?
我在正确终止用 Assembly 编写的 16 位 DOS 程序时遇到问题。这是部分代码: .386P .model flat stack_s segment stack 'stack'
我需要多少档才能正确执行以下指令。我对我所做的事情有些困惑,所以我在这里看到专家的答案。 lw $1,0($2); beq $1,$2,Label; 请注意,检查是否会发生分支将在解码阶段完成。但是在
我正在尝试在汇编中进行简单的乘法运算,但是由于某些原因,当标记了MUL函数时,我看不到寄存器会发生变化。 mov bx, 5 mov cx, 10 mul cx 最佳答案 这些称为指令,它们指定
我正在尝试在 Assembly 中实现递归斐波那契程序。但是,我的程序崩溃了,出现了未处理的异常,我似乎无法找出问题所在。我不怀疑这涉及我对堆栈的不当使用,但我似乎无法指出哪里...... .386
我编写了以下代码: .386 .model small .stack 100h .data text db "Paper",0 .code start : lea dx ,
我有一个用汇编语言编写的裸机 ARM 的启动代码,我正在尝试了解它是如何工作的。该二进制文件被写入一些外部闪存中,并在启动时将其自身的一部分复制到 RAM 中。尽管我读过这篇文章wikipedia e
我在数据部分定义了一个二维数组和两个一维数组(一个用于列总和,一个用于行总和),并且我编写了一个函数,将二维数组求和到一维数组中。我使用 eax 和 ebx 作为二维数组的索引,但是当 eax 或 e
我正在开始组装,我正在使用 nasm 来组装代码,我正在尝试处理驻留在内存中的字符串并更改它,我想检查一个字节是否在某个范围内(ascii),这样我就可以决定如何处理它,我似乎不知道如何检查一个值是否
虽然您通常不希望将一个整体程序集用于小型项目以外的任何事情,但可能会将事物分离得太多。 组装分离过多的迹象/气味是什么? 最佳答案 第一个(明显的)是:在一个有很多项目的解决方案中,其中只有少数(比如
我正在尝试编写斐波那契的汇编代码版本,它给出第 n 个斐波那契数并返回它。 出于某种原因,它在存储斐波那契数的返回值和添加它们时遇到问题。 我希望它打印第 n 个斐波那契数。 我对我的代码做了一些修改
我有一个最小的、可重现的示例有两个问题,该示例具有三个针对 .NET Core 3.1 的项目。但我也想以 .NET Standard 2.0 为目标。 该示例适用于需要在运行时加载程序集并使用提供的
: 运算符在汇编中做什么?代码如下:DS:DX我还没有找到该运算符(operator)的任何文档。(我正在使用 NASM) 最佳答案 那实际上只是一个寄存器分隔符,而不是运算符。这意味着使用 DX 寄
我在哪里可以找到为 gmp-5.0.0 编写的程序的汇编代码我正在使用 UBUNTU 和 G++ 编译器..编译代码的命令是“g++ test.cc -o outp -lgmp” 实际上我想知道在 1
我是组装新手,我有一个关于如何表示负数的问题 我有三个 DWORDS 变量,比如说: result DWORD 0 i DWORD 3 j DWORD 5 我想计算这个公式:result = i -
我想编写我的第一个汇编程序。我在论文上做了一些程序,但这是我第一次使用编译器。我正在使用 ideone .我的程序很简单, 翻译 A = 5 - A到 assembly NEG A ADD A, 5
程序集,masm 嘿,我写了宏来打印存储在 dane1 段中的 1 字节值。 我将值除以 16,然后将提醒推送到堆栈,直到值==0。然后我弹出提醒将它们转换为 ASCII 码,并打印它们。 有人可以看
我正在研究 nasm 的一个大学项目。唯一的问题是我无法生成 162 和 278 之间的偶数随机数。我尝试了很多算法,但似乎无法限制范围内的数字。 是否有一个小技巧或调整来获得所需的范围内的数字?目的
终于在无数次错误的漫长 session 之后,希望这是最后一个。 没有编译或运行时错误,只是一个逻辑错误。 编辑:(固定伪代码) 我的伪代码: first = 1; second = 1; thir
我知道在程序集r0中调用函数时,包含第一个参数,直到r3是第四个。我知道,当它超过四个时,将使用堆栈指针,但是我不太确定具体细节。 r0-r3仍然保持前四个,其余的进入堆栈吗?我正在看下面的程序集,试
我是一名优秀的程序员,十分优秀!