在大学的最后一个学期,我在计算机语言课上的老师教我们一门名为Whitespace的深奥语言。为了在非常繁忙的日程(期中考试)中更好地学习这门语言,我用Python编写了一个解释器和汇编器。一种汇编语言旨在方便编写程序,并使用给定的汇编助记符编写了一个示例程序。
现在是夏天,一个新项目已经开始,目标是为 Whitespace 0.3 重写解释器和汇编器,之后会有进一步的发展。由于比以前有更多的时间来设计它,因此这里为您提供了一个大纲,该大纲为汇编语言提供了一组经过修改的助记符。这篇文章被标记为 wiki 供他们讨论。
你过去有过汇编语言的经验吗?是否有一些您认为应该重命名为不同的指令?您是否发现自己跳出框框思考并使用与命名助记符不同的范式?如果您对这些问题中的任何一个问题的回答都是肯定的,那么这里非常欢迎您。感谢主观回答!
堆栈操作(IMP:[空格])
堆栈操作是更常见的操作之一,因此 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])
算术命令对栈顶的两项进行运算,并用运算结果替换它们。推送的第一个项目被认为是操作员的左侧。
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
问题:您将如何重新设计、重写或重命名以前的助记符,原因是什么?