大学上学期,我的计算机语言课老师教给我们一种深奥的语言,名为 Whitespace 。为了在非常繁忙的日程(期中考试)中更好地学习语言,我写了一个 解释器和汇编器 ://www.python.org/" rel="nofollow noreferrer">Python。 汇编语言旨在帮助轻松编写程序,示例程序是使用给定的程序集编写的助记符。
现在已经是夏天了,一个新项目已经开始,目标是重写 Whitespace 0.3 的解释器和汇编器,随后会有进一步的开发。由于比以前有更多的额外时间来进行设计,因此这里向您提供一个大纲,其中提供了一组修订后的汇编语言助记符。这篇文章被标记为供他们讨论的 wiki。
您过去有过使用汇编语言的经验吗?是否有一些您认为应该重命名为其他内容的指令?您是否发现自己跳出了框框,采用了与助记符命名时不同的范式?如果您对其中任何一个问题的回答是肯定的,我们非常欢迎您来到这里。主观答案值得赞赏!
堆栈操作(IMP:[空格])
堆栈操作是最常见的操作之一,因此 IMP [空格] 很短。有四个堆栈指令。
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
问题:您将如何重新设计、重写或重命名以前的助记词?出于什么原因?
Last semester in college, my teacher in the Computer Languages class taught us the esoteric language named Whitespace. In the interest of learning the language better with a very busy schedule (midterms), I wrote an interpreter and assembler in Python. An assembly language was designed to facilitate writing programs easily, and a sample program was written with the given assembly mnemonics.
Now that it is summer, a new project has begun with the objective being to rewrite the interpreter and assembler for Whitespace 0.3, with further developments coming afterwards. Since there is so much extra time than before to work on its design, you are presented here with an outline that provides a revised set of mnemonics for the assembly language. This post is marked as a wiki for their discussion.
Have you ever had any experience with assembly languages in the past? Were there some instructions that you thought should have been renamed to something different? Did you find yourself thinking outside the box and with a different paradigm than in which the mnemonics were named? If you can answer yes to any of those questions, you are most welcome here. Subjective answers are appreciated!
Stack Manipulation (IMP: [Space])
Stack manipulation is one of the more common operations, hence the shortness of the IMP [Space]. There are four stack instructions.
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
Arithmetic (IMP: [Tab][Space])
Arithmetic commands operate on the top two items on the stack, and replace them with the result of the operation. The first item pushed is considered to be left of the operator.
add Addition
sub Subtraction
mul Multiplication
div Integer Division
mod Modulo
Heap Access (IMP: [Tab][Tab])
Heap access commands look at the stack to find the address of items to be stored or retrieved. To store an item, push the address then the value and run the store command. To retrieve an item, push the address and run the retrieve command, which will place the value stored in the location at the top of the stack.
save Store
load Retrieve
Flow Control (IMP: [LF])
Flow control operations are also common. Subroutines are marked by labels, as well as the targets of conditional and unconditional jumps, by which loops can be implemented. Programs must be ended by means of [LF][LF][LF] so that the interpreter can exit cleanly.
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])
Finally, we need to be able to interact with the user. There are IO instructions for reading and writing numbers and individual characters. With these, string manipulation routines can be written. The read instructions take the heap address in which to store the result from the top of the stack.
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
Question: How would you redesign, rewrite, or rename the previous mnemonics and for what reasons?
发布评论
评论(3)
我认为我建议的第一个更改是将保持和放下分别更改为推和弹出。
然后也许我会将 copy 重命名为 dup (我认为这是面向堆栈的语言中此操作最常见的名称)。
我有点困惑为什么你经常有与助记符不同的简短单词解释。例如助记符是Save,解释是Store。助记符是Load,解释是Retrieve。顺便说一下,这两个助记符对我来说还没有得到充分的解释。保存什么在哪里?从哪里加载什么? (编辑该问题随后已被编辑,以使这些含义变得清晰)
感谢您的有趣帖子。
I think the first change I'd propose is changing hold and drop to push and pop respectively.
Then maybe I'd rename copy to dup (I think that's the most common name for this operation in stack oriented languages).
I'm a little puzzled why often you have short one word explanations that are different to the mnemonic. For example mnemonic is Save, explanation is Store. Mnemonic is Load, explanation is Retrieve. Incidentally those are the two mnemonics that aren't sufficiently explained to me. Save what where? Load what from where? (Edit the question has subsequently been edited to make these meanings clear)
Thanks for the interesting post.
编辑:
如果您不介意混淆操作码和寻址模式,请使用 CISCy 语法,
另一方面,基于堆栈的代码通常将入栈和出栈视为隐式的。在这种情况下,“imm n”(立即)而不是“push”。那么所有的栈操作都是纯粹的栈操作,这样就很好而且一致了。
我不确定如何写“drop N”——这个描述听起来像是“drop 1”并不等于“drop”,这看起来很奇怪。
EDIT:
If you don't mind conflating opcodes and addressing modes, using CISCy syntax,
On the other hand, stack-based code usually treats push and pop as implicit. In that case, "imm n" (immediate) instead of "push". Then all stack operations are purely stack operations, which is nice and consistent.
I'm not sure how I'd write "drop N" — the description makes it sound like "drop 1" isn't equivalent to "drop" which seems odd.
我不确定我完全理解你的问题,所以如果我有问题,请原谅我。
除了堆栈之外,我可能还会添加一个“状态寄存器”,其中包含由算术运算符设置的各种不同标志(如进位、溢出和零)。
然后我会添加“if”形式来测试这些标志。
我将添加位移和旋转(左和右)指令,以及对位进行操作的 AND/OR/XOR/NOT 操作。
您很可能希望进行某种内存访问,除非您希望 I/O 指令将内存视为值流,以获得良好的老式图灵机感觉。
I'm not sure I completely understand your question, so if I'm off base, forgive me.
In addition to your stack, I would probably add a "status register" that contains a variety of different flags (like Carry, Overflow, and Zero) that are set by the arithmatic operators.
I would then add "if" forms that test those flags.
I would add bit shift and rotate (both left and right) instructions, as well as AND/OR/XOR/NOT operations that operate on bits.
You will most likely want to have some sort of memory access, unless you intend the I/O instructions to treat memory as a stream of values for that good ol' fashioned Turing Machine feel.