gpt4 book ai didi

python - 你对这些汇编助记符有什么建议吗?

转载 作者:太空狗 更新时间:2023-10-30 00:57:59 24 4
gpt4 key购买 nike

在大学的最后一个学期,我在计算机语言课上的老师教了我们一门名为 Whitespace 的深奥语言。 .为了在非常繁忙的日程(期中考试)中更好地学习语言,我写了一篇 interpreterassemblerPython .一个assembly language旨在方便编写程序,并且 sample program是用给定的程序集编写的 mnemonics .

现在是夏天,一个新项目已经开始,目标是为 Whitespace 0.3 重写解释器和汇编器,随后将进行进一步的开发。由于比以前有更多的时间来设计它,这里为您提供了一个大纲,其中为汇编语言提供了一组修订的助记符。这篇文章被标记为他们讨论的 wiki。

您过去有过使用汇编语言的经验吗?是否有一些您认为应该重命名为其他内容的说明?您是否发现自己跳出框框思考并使用与助记符命名时不同的范例?如果您对其中任何一个问题的回答是肯定的,那么我们非常欢迎您。感谢主观回答!


堆栈操作(IMP:[Space])

堆栈操作是比较常见的操作之一,因此 IMP [Space] 的不足之处。有四个堆栈指令。

hold N       Push the number onto the stack
copy Duplicate the top item on the stack
copy N Copy the nth item on the stack (given by the argument) onto the top of the stack
swap Swap the top two items on the stack
drop Discard the top item on the stack
drop N Slide n items off the stack, keeping the top item

算术(IMP:[Tab][Space])

算术命令对栈顶的两项进行运算,并用运算结果替换它们。推送的第一个项目被认为是运算符(operator)留下的。

add          Addition
sub Subtraction
mul Multiplication
div Integer Division
mod Modulo

堆访问(IMP:[Tab][Tab])

堆访问命令查看堆栈以找到要存储或检索的项目的地址。要存储项目,请按地址,然后按值并运行存储命令。要检索项目,请压入地址并运行检索命令,这会将存储在堆栈顶部位置的值放置。

save         Store
load Retrieve

流量控制(IMP:[LF])

流控操作也很常见。子程序由标签标记,以及有条件和无条件跳转的目标,通过它们可以实现循环。程序必须通过[LF][LF][LF]结束,这样解释器才能干净退出。

L:           Mark a location in the program
call L Call a subroutine
goto L Jump unconditionally to a label
if=0 L Jump to a label if the top of the stack is zero
if<0 L Jump to a label if the top of the stack is negative
return End a subroutine and transfer control back to the caller
halt End the program

I/O(IMP:[Tab][LF])

最后,我们需要能够与用户进行交互。有读写数字和单个字符的IO指令。有了这些,就可以编写字符串操作例程。读取指令从栈顶获取存储结果的堆地址。

print chr    Output the character at the top of the stack
print int Output the number at the top of the stack
input chr Read a character and place it in the location given by the top of the stack
input int Read a number and place it in the location given by the top of the stack

问题您将如何重新设计、重写或重命名以前的助记词,原因是什么?

最佳答案

我认为我建议的第一个改变是将 hold 和 drop 分别改为 push 和 pop。

然后我可能会将 copy 重命名为 dup(我认为这是面向堆栈的语言中此操作最常见的名称)。

我有点不解,为什么您经常使用与助记符不同的简短单词解释。比如助记符是Save,解释是Store。助记符是Load,解释是Retrieve。顺便说一句,这是两个没有向我充分解释的助记符。保存什么在哪里?从哪里加载什么? (编辑问题随后被编辑以使这些含义清楚)

感谢有趣的帖子。

关于python - 你对这些汇编助记符有什么建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2901274/

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