子程序&转到设计

发布于 2024-08-27 15:58:46 字数 1096 浏览 4 评论 0原文

我有一个关于子例程的奇怪问题:因为我正在创建一种最小的语言,并且我不想添加像 whilefor 这样的高级循环,所以我计划只是添加 goto 来保持图灵完备。

现在我想,eww - gotos - 如果我不得不经常使用 gotos,我就不想用那种语言编程了。所以我考虑添加子程序。

我看到的区别如下:

  • gotos

    • 转到(明显的队长)先前定义的点并从那里继续执行程序。导致难以理解且有错误的代码,我认为这是事实。
  • 子程序

    • 类似:您在某处定义它们的起点,当您调用它们时,程序会跳转到那里 - 但是子例程可以通过return返回到调用它的点.

好的。为什么我不添加更多类似功能、更漂亮的子例程呢?因为:

如果我从其他子例程中调用子例程,为了使返回工作,我必须使用一个包含当前运行的子例程来自顶部的点的堆栈

这意味着,如果我使用子例程创建循环,最终会得到一个极其消耗内存、带有返回位置的溢出堆栈。不好。

不要将我的子例程视为函数。它们只是返回到调用它们的位置的 goto,它们实际上并不像当今几乎所有语言中的 return x; 语句那样返回值。

现在我的实际问题是:

  • 如何解决上述子例程循环中的堆栈溢出问题?我是否必须添加一个不带 return 选项的单独的 goto 语言构造?

  • 汇编器没有循环,但正如我所见,myJumpPoint:, jnz, jz, retn。这对我来说意味着还必须有一个包含所有返回位置的堆栈。

    • 我的说法正确吗?
    • 那么长时间运行的循环又如何呢?那么它们不会溢出堆栈/吃掉内存吗?
    • 我在汇编程序中得到的 retn 符号是否完全错误?如果是,请向我解释一下。

I have a strange question concerning subroutines: As I'm creating a minimal language and I don't want to add high-level loops like while or for I was planning on just adding gotos to keep it Turing-Complete.

Now I thought, eww - gotos - I wouldn't want to program in that language if I had to use gotos so often. So I thought about adding subroutines instead.

I see the difference as the following:

  • gotos

    • Go to (captain obvious) a previously defined point and continue executing the program from there. Leads to hardly understandable and buggy code, I think that's a fact.
  • subroutines

    • Similiar: You define their starting point somewhere, as you call them the program jumps there - but the subroutine can go back to the point it was called from with return.

Okay. Why didn't I just add the more function-like, nice looking subroutines? Because:

In order to make return work if I call subroutines from within subroutines from within other subroutines, I'd have to use a stack containing the point where the currently running subroutine came from at top.

That would then mean that I would, if I create loops using the subroutines, end up with an extremely memory-eating, overflowing stack with return locations. Not good.

Don't think of my subroutines as functions. They are just gotos that return to the point they were called from, they don't actually give back values like the return x; statement in nearly all today's languages.

Now to my actual questions:

  • How can I solve the above problem with the stack overflow on loops with subroutines? Do I have to add a separate goto language construct without the return option?

  • Assembler doesn't have loops but as I have seen myJumpPoint:, jnz, jz, retn. That means to me that there must also be a stack containing all the return locations.

    • Am I right with that?
    • What about long running loops then? Don't they overflow the stack/eat memory then?
    • Am I getting the retn symbol in assembler totally wrong? If yes, please explain it to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻之岛 2024-09-03 15:58:46

jnzjz 只是“goto”。

另外,我认为将所有循环实现为子例程的解决方案不如仅添加 goto 优雅。你似乎对 goto 有偏见。这只是一个词。它不会伤害你。

jnz, jz are just "goto".

Also, I think implementing all loops as subroutines is less elegant a solution than just adding goto. You seem to be biased against goto. It's just a word. It won't harm you.

方圜几里 2024-09-03 15:58:46

好吧,你不能用条件跳转来解决这个问题吗?循环检查某些条件以查看是否必须再次运行。使用您的子例程,但添加一个条件返回,它要么返回到调用点,要么返回到例程的开头,具体取决于条件的评估。

Well, couldn't you solve that problem with a conditional jump? A loop checks some condition to see if it has to run again. Go with your subroutines, but add a conditional return, that either returns to the point of call, or returns to the beginning of the routine depending on the evaluation of the condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文