- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我应该编写递归程序来计算给定范围内可被 3 整除的数字之和。首先,从键盘读取两位数 N,这给了我们 1 到 N 的范围。 N 号通过堆栈传递给过程。此外,范围内数字的总和应通过堆栈返回。问题是,我无法设法通过堆栈返回总和。所以,一点点帮助不会有坏处。这是代码:
data segment
n dw ?
sum dw ?
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
mov ax,@data
mov ds,ax
mov es,ax
mov cx,3
mov ah,1
int 21h
sub al,30h
mov ah,0
mov bx,10d
mul bx
mov n,ax
mov ah,1
int 21h
sub al,30h
mov ah,0
add n,ax
mov ax,n
push ax
call suma
mov ax, 4c00h
int 21h
suma proc
push bp
mov bp,sp
mov ax,[bp+4]
cmp ax,1
je l1
dec ax
push ax
call suma
mov ax,[bp+4]
push ax
div cl
cmp ah,0
pop ax
JNE l1
add sum,ax
L1:
pop bp
ret 2
suma endp
ends
end start
最佳答案
正如 Jester 所指出的,这是一项愚蠢的任务,但如果您很好地理解堆栈,您应该能够以任何方式解决它。
您当前的代码:
; For example let's say sp = 54 (address 54 in memory)
push ax ; store argument n, sp = 52
call suma ; do the call
; inside suma under the argument there are pushed also
; return address by CALL, sp =50
; and old bp by PUSH bp, sp = 48
; then [sp+4] points to argument n (48+4 = 52)
; stack is restored by the function ("ret 2"), sp = 54
现在如果你想把结果放在栈上,你必须从调用者的角度保留栈空间,因为在suma
之后。返回,sp
在我的示例中恢复为“54”,以及从 suma
写入堆栈的所有内容拜访下级sp
地址可能已经被覆盖(例如,如果中断发生在 ret 2
和您的下一条指令之间,那么中断确实使用了地址 54 以下的堆栈内存,并覆盖了它)。在地址 54+ 处写入内存的任何内容都会对调用者有效,但会破坏调用者存储在那里的值。
顺便说一句,您当前的相关代码不会在 ax
中返回结果,因为它在以错误的方式进行递归时会自行覆盖它,并在地址 sum
的内存中累积结果, 未由 suma
初始化, 所以它只会工作一次。可以这样写,即使多次调用,递归也会返回正确的总和(首先将总和归零,然后调用 F(n-1) 并根据需要在 F(n- 1)).
因此,要么决定返回该值代替参数 n,那么您必须:
suma
将结果存储到 [ebp+4]
内存ret
(不从栈中释放参数)call suma
之后无处不在pop ax
从堆栈中选择结果或者在参数之上保留栈空间,比如:
sub sp,2 ; make space for result in stack
; you can also use another bogus "push ax", not caring about value, just adjusting sp
push ax ; push argument n
call suma
pop ax ; read result + restore stack
然后里面suma
你必须将结果存储到 [bp+6]
, ret 2
保持原样,只释放参数。
无论哪种方式,调用者都必须保留堆栈空间,因为任何未保留的堆栈空间(低于 sp
的当前值)都可能随时被中断处理程序覆盖(在 x86 16b 实模式中)。
关于assembly - 递归过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47249697/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!