MAC-1程序集递归 [英] MAC-1 assembly recursion

查看:67
本文介绍了MAC-1程序集递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何在MAC-1汇编程序中调用递归函数?

How would you call a recursive function in MAC-1 assembler?

在c ++中,您将执行以下操作:

In c++ you would do something like:

int func(int num)
{
    if(num == 0) return 1;
    return num * func(num-1);
}

我知道如何使用

CALL

以及如何在堆栈上加载参数以及如何使用它,我也有自己编写的乘法指令,所以

and how to load parameters on stack and take it of, I also have multiplication instruction made by myself, so

MUL 6

会做:

ac = ac * 6

但是我不知道如何调用递归.

but I cant figure out how to call recursive.

推荐答案

我不知道如何调用递归

I cant figure out how to call recursive

没有太多.您只需对所使用的相同函数进行调用,并将当前调用中的所有内容保存在堆栈中.

There's not much to it. You just do a call to the same function that you're in, and save everything from the current call on the stack.

例如(伪代码):

n=3, CALL func
  (n==3):  PUSH n, n--, CALL func
    (n==2):  PUSH n, n--, CALL func
      (n==1):  PUSH n, n--, CALL func
        (n==0):  ac=1, RET
      POP n, ac *= n, RET 
    POP n, ac *= n, RET  
  POP n, ac *= n, RET    

这篇关于MAC-1程序集递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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