磁芯大战
汇编程序间的战斗
磁芯大战(core war or core wars)就是汇编程序间的大战,程序在虚拟机中运行,并试图破坏其他程序,生存到最后即为胜者。程序用一种特殊的汇编语言(RedCode)完成,运行于叫做MARS(Memory Array RedCode Simulator)的虚拟机中。
人物介绍
早在1949年,电脑的先驱者约翰·冯·诺依曼在他的论文《自我繁衍的自动机理论》中已把病毒的蓝图勾勒出来,当时,绝大部分的电脑专家还不太相信这种会自我繁殖的程序.然而短短十年之后,磁芯大战在贝尔实验室中诞生,使他的设想成为事实。他是三个年轻人在工余时的产物。他们是麦耀莱、维索斯基以及莫里斯。其中莫里斯就是后来制造了“莫里斯蠕虫”的罗特·莫里斯的父亲。当时三人年纪都只有二十多岁。
用于磁芯大战的游戏有多种,例如有个叫爬行者的程序(Creeper),每一次执行都会自动生成一个副本,很快电脑中原有资料就会被这些爬行者侵蚀掉;“侏儒”(Dwarf)程序在记忆系统中行进,每到第五个“地址”(address)便把那里所储存的东西变为零,这会使原本的程序严重破坏;最奇特的就是一个叫“印普”(Imp)的战争程序了,它只有一行指令,那就是“MOV 0,1”,其中MOV是“Move”的简写,即移动的意思。它把身处的地址中所载的“0”写(移)到下一个地址中,当“印普”展开行动之后,电脑中原有的每一行指令都被改为“MOV 0,1”,换句话说,荧光屏上留下一大堆“MOV 0,1”。在那些日子里,电脑都没有连线,是互相独立的,因此病毒瘟疫很难传播。如果有某部电脑受到“感染”,失去控制,工作人员只需把它关掉便可。但是当电脑连线逐渐成为社会结构的一部分之后,一个或自我复制的病毒程序便很可能带来无穷的祸害了。例如爬行者程序就能够从一台机器“爬”到另一台机器中。因此长久以来,懂得玩“磁芯大战”游戏的电脑工作者都严守一项不成文的规定:不对大众公开这些程序的内容。 1983年,这项规定被打破了。科恩·汤普逊(Ken Thompson)是当年一项杰出电脑奖得奖人。在颁奖典礼上,他作了一个演讲,不但公开证实了电脑病毒的存在,而且还告诉所有听众怎样去写自己的病毒程序。他的同行全都吓坏了,然而这个秘密已经流传出去了。
1984年,情况愈复杂了。这一年,《科学美国人》月刊(Scientific American)的专栏作家杜特尼(A.K.Dewdney)在5月刊号写了第一篇讨论“磁芯大战”的文章,并且只要寄上两块美金,任何读者都可以收到有关写程序的纲领,在自己家的电脑中开辟战场。
在1985年3月份的《科学美国人》里,杜特尼再次讨论“磁芯大战”和病毒。在文章的开头他便说:“当去年5月有关‘磁芯大战’的文章印出来时,我并没有想过我所谈论的是那么严重的题目……”文中第一次提到“病毒”这个名称。他提到说,意大利的电脑程序员利用感染磁碟的方式使其它电脑受到破坏性程序的感染。就这样,潘多拉之盒被打开了,许多程序员都了解了病毒的原理,进而开始尝试编制这种具有隐蔽性、攻击性和传染性的特殊程序。病毒从隐秘走向公开,先是利用磁碟,然后是利用网络,迅速在全世界范围内扩散开来,成为电脑用户的头号敌人。
磁芯大战(Core War)的语言Redcode教程
其他信息
原题
Corewar Tutorial Pt INewsgroups: rec.games.corewar
(Mark A. Durham)
Subject: Intro to Redcode Part I
Organization: Rice University,Houston,TX
Date: Thu,14 Nov 1991 09:41:37 GMT
Redcode介绍
-----------------------
I. 前言 - 读者注意! { 第一部分 }
Ⅱ. 标记 { 第一部分 }
Ⅲ. MARS的特性 { 第一部分 }
Ⅳ. 地址模式 { 第二部分 }
V. 指令集 { 第二部分 }----------------------------------------------------------------------
Introduction to Redcode
-----------------------
I. Preface - Reader Beware! { Part I }
Ⅱ. Notation { Part I }
Ⅲ. MARS Peculiarities { Part I }
Ⅳ. Address Modes { Part Ⅱ }
V. Instruction Set { Part Ⅱ }
----------------
前言
Thus,any program can pass itself off as an implementation of Core
War.(译者:科瑞沃尔) 理论上,大家都希望在一个系统上写Redcode,
并且知道在所有其他系统上它都会以相同的方式运行. 哎呀,这没什么关系呀.
I. Preface - Reader Beware!
Thus,any program can pass itself off as an implementation of Core
War. Ideally,one would like to write a Redcode program on one
system
and know that it will run in exactly the same manner on every other
system. Alas,this is not the case.
Core War 有四种系统:非ICWS系统,ICWS'86,ICWS'88,和扩展系统.
非ICWS系统通常与A. K. Dewdney在《科学的美国》上的一篇文章
一般支持ICWS'86,ICWS'88规则,并有一定的扩展.我将会谈到常用的公共扩展,如果
它们在所有扩展系统上都可用的话(大部分都不是).
Basically,Core War systems fall under one of four catagories:
Non-ICWS,ICWS'86,ICWS'88,or Extended. Non-ICWS systems are
usually
ICWS'88 systems conform to the standards set out by the
International
Core War Society in their standards of 1986 and 1988,respectively.
Extended systems generally support ICWS'86,ICWS'88,and proprietary
extensions to those standards. I will discuss frequently common
extensions as if they were available on all Extended systems (which they most certainly are not).
我将不会在这里讨论非ICWS系统. 如果你能读懂这篇文章,你就可以轻易理解
额外标准问题的困扰,我将尝试指出来.
我会尝试指出其他解释有歧义的地方,并清楚表明我的或其他人的相关解释.
I will not describe Non-ICWS systems in this article. Most Non-
ICWS systems will be easily understood if you understand the systems
ICWS'86 and ICWS'88 (to a lesser extent) both suffer from
ambiguities
and extra-standard issues which I will try to address.
This is where the reader should beware. Because almost any
interpretation of the standard(s) is as valid as any other,I
naturally prefer MY interpretation. I will try to point out other
common interpretations when ambiguities arise though,and I will
clearly indicate what is interpretation (mine or otherwise) as such.
You have been warned!
----------------------------------------------------------------------
标记
冒号后缀的可视为普通.
Ⅱ. Notation
explaining
certain usages. Items without colon-suffixed prefaces can be
considered universal.
Redcode由以下格式的汇编语言指令组成
<;标签> <;操作符> , <;注释>
Redcode consists of assembly language instructions of the form
Recode例程:
; Imp
; by A. K. Dewdney
imp MOV imp,imp+1 ; 程序把自己复制到前面一个指令
END ; 并且移动穿过内存
(译者注:IMP是最短的Core War战士,只有一句)
An example Recode program:
; Imp
; by A. K. Dewdney
imp MOV imp,imp+1 ; This program copies itself ahead one
END ; instruction and moves through memory.
<;标签>;是可选的.
86: <;标签> 从第一列开始,1到8个字符长,必须以字母开头且由合法字符组成.
88: <;标签> 同上,但长度不限和区分大小写. 只有前8个字符有意义
X: <;标签>;前面可以有任意多的空白(空格,TAB键,和新行),长度任意,必须以字母
注释: 我全用小写字母写 标签,这样来和操作符操作数区分
The
86:
long,beginning with an alphabetic character and consisting
88:
addressed. Only the first eight characters are considered
significant.
X:
alphanumerals
but must start with an alphabetic,and case is significant
Commentary: I will always use lowercase letters for labels to
distinguish labels from opcodes and family operands.
操作符 和<;标签>;之间用空白隔开. 操作符不区分大小写. DAT,MOV,
ADD,SUB,JMP,JMZ,JMN,DJN,CMP,SPL,和 END 都是可用的操作符
86: SPACE 也作为一个操作符.
88: SLT 和 EQU 是操作符. SPACE 不是.
X: 以上的和XCH,PCT都是操作符,还有无数的扩展.
注释: END,SPACE,和 EQU是伪指令,因为它们向汇编程序指明了操作,但不
产生可执行的代码. 我用大写的操作符来和<;标签>;,文本区分开.
The is separated from the
whitespace. Opcodes may be entered in either uppercase or
lowercase. The case does not alter the instruction. DAT,MOV,
ADD,SUB,JMP,JMZ,JMN,DJN,CMP,SPL,and END are acceptable
opcodes.
86: SPACE is also recognized as an opcode.
88: SLT and EQU are recognized as opcodes. SPACE is not.
X: All of the above are recognized as opcodes as well as XCH and
PCT,
plus countless other extensions.
Commentary: END,SPACE,and EQU are known as pseudo-ops because they
really indicate instructions to the assembler and do not produce
executable code. I will always capitalize opcodes and
pseudo-ops
to distinguish them from labels and text.
The and 合起来表示A-操作数. 类似地,the
合起来表示B-操作数. A-操作数对某些操作符来说是可选的.B-操作数对某些操
作符来说也是可选的. 只有END可以不带操作数
86: 操作数用逗号隔开
88: 操作数用空白隔开
X: 操作数可用空白或逗号隔开. 少了一个逗号可能由于歧义而导致无法预料的后果.
注释: '88标准强迫你在写一个操作数时不能用空格,接收空格来分开两个操作数. 我
喜欢在表达式里用空格,因此我用逗号来分开操作数,我在这里会清楚地这样做.
The and taken together are referred to as the
A-operand. Similarly,the combination is
known
as the B-operand. The A-operand is optional for some opcodes.
The B-operand is optional for some opcodes. Only END can go
without at least one operand.
86: Operands are separated by a comma.
88: Operands are separated by whitespace.
X: Operands are separated by whitespace and/or a comma. Lack of a
comma can lead to unexpected behaviour for ambiguous constructs.
Commentary: The '88 standard forces you to write an operand without
whitespace,reserving whitespace to separate the operands. I
like
whitespace in my expressions,therefore I prefer to separate my
operands with a comma and will do so here for clarity.
<;模式> 有 # (即时寻址),@ (间接寻址),或< (86: Auto-Decrement 间接寻址88: Pre-Decrement 间接寻址). 缺省为直接寻址.
86: $ 表示直接寻址
88: $ 不可用
X: $ 和86一样
注释: Auto-Decrement 间接寻址和Pre-Decrement 间接寻址的区别是语义的,而非依据造句法的
(译者:我不太明白~)
is # (Immediate Addressing),@ (Indirect Addressing),or <
(86: Auto-Decrement Indirect,88: Pre-Decrement Indirect). A
missing mode indicates Direct Addressing.
86: $ is an acceptable mode,also indicating Direct Addressing.
88: $ is not an acceptable mode.
X: $ is an acceptable mode as in 86:.
Commentary: The distinction between Auto-Decrement Indirect
Addressing
and Pre-Decrement Indirect Addressing is semantic,not
syntactic.
<;字段> 是标签和整数的任意组合,由 + (加号)和 - (减号)隔开.
88: 添加了 * (乘号
X: 表达式中允许出现括号和空格.
编译程序来说可能很有用,但对Redcode编译程
序就完全是多余的. 在Redcode中当前语句总是能用0来表示.
is any combination of labels and integers separated by the
arithmetic operators + (addition) and - (subtraction).
special
label symbol meaning the current statement.
88: Arithmetic operators * (multiplication) and / (integer division)
X: Parentheses and whitespace are permitted in expressions.
useful in some real assemblers,but is completely superfluous in
a
Redcode assembler. The current statement can always be referred
to as 0 in Redcode.
<;注释> 由 ; (分号)开头,在新行结束(译者:象C++的//),可以是任意数量
的字符. 注释可以自己占一行,前面不需要有指令
88: 明确允许空行
begins with a ; (semicolon),ends with a newline,and can
have any number of intervening characters. A comment may appear
on a line by itself with no instruction preceding it.
88: Blank lines are explicitly allowed.
我用花括号来包括一类操作数
Redcode语句就写成这样
x?? A,B ; 这描述了所有可能的Redcode语句.
I enclose sets of operands or instructions in curly braces. Thus
family of acceptable Redcode statements can be represented as
x?? A,B ; This represents all possible Redcode statements.
的整数.
of
with the same value as the MARS memory size.
----------------------------------------------------------------------
MARS特性
MARS有两个特点,使Redcode和其他汇编语言不同. 首先,MARS没有绝对地址.
第二,内存是循环的.
由于没有绝对地址,所以Redcode都是用相对寻址写的. 在相对寻址里,所以地址
都解释为当前执行指令的位移. 地址 0 就是当前执行指令. 地址 -1 是前一个当
前执行指令(假设没有跳转或分支).地址 +1 是下一个执行指令 (假设没有跳转或分支).
因为内存是循环的,每条指令都有无穷多个地址. 假设内存大小为M,当前指令的地址
有 { ...,-2M,-M,0,M,2M,... }. 前一个指令是{ ...,-1-2M,-1-M,-1,M-1,2M-1,... }. 下一个指令是{ ...,1-2M,1-M,1,M+1,2M+1,... }.
Ⅲ. MARS Peculiarities
There are two things about MARS which make Redcode different from
any other assembly language. The first of these is that there are
no
absolute addresses in MARS. The second is that memory is circular.
Because there are no absolute addresses,all Redcode is written
using relative addressing. In relative addressing,all addresses
are
interpreted as offsets from the currently executing instruction.
Address 0 is the currently executing instruction. Address -1 was
the
previously executed instruction (assuming no jumps or branches).
Address +1 is the next instruction to execute (again assuming no jumpsor branches).
Because memory is circular,each instruction has an infinite
number
of addresses. Assuming a memory size of M,the current instruction
has the addresses { ...,-2M,-M,0,M,2M,... }. The previous
instruction is { ...,-1-2M,-1-M,-1,M-1,2M-1,... }. The next
instruction is { ...,1-2M,1-M,1,M+1,2M+1,... }.
注释: MARS因历史原因通过标准化字段为0 到 M-1 的正整数来运行这些
利用了环状特性的目标代码. 由于内存大小在编程阶段常常是未知的,
MARS有一个引导程序在处理代码放置和初始化任务指针之余,使字段标准化.
注释:Redcode程序常想提早知道MARS的内存大小. 有时这是不可能的.
因为标准化字段只能用0 到 M-1 的整数来表示,我们不能用M来表示.较好
的东西是? M-1. 但我们怎样在不知道内存大小的时候写M-1? 回忆上面讲的,
-1 就等于 M-1. 最后要注意的: -1/2 是等于 0 的(而不是 M/2[译者注:是(m-1)/2吧?]) 汇编程序表达式求值得 -0.5,然后舍去了.
Commentary: MARS systems have historically been made to operate on
object code which takes advantage of this circularity by
insisting
that fields be normalized to positive integers between 0 and M-1,
inclusive. Since memory size is often not known at the time of
assembly,a loader in the MARS system (which does know the memory size) takes care of field normalization in addition to its normal
operations of code placement and task pointer initialization.
Commentary: Redcode programmers often want to know what the memory
size of the MARS is ahead of time. This is not always possible.
Since normalized fields can only represent integers between 0
and
M-1 inclusive,we can not represent M in a normalized field.
The
next best thing? M-1. But how can we write M-1 when we do not
know the memory size? Recall from above that -1 is equivalent
to
M-1. Final word of caution: -1/2 is assembled as 0 (not as M/2)
since the expression is evaluated within the assembler as -0.5
and
then truncated.
86: 只能载入两个编译了的Redcode程序(战士)到MARS内存(磁芯).
88: 磁芯在没有载入战士时被初始化为(充满)DAT 0,0.
可以载入任意数量的战士.
注释: 比赛时几乎都是一对一,只载入对战的两个战士.
86: Only two assembled-Redcode programs (warriors) are loaded into
MARS memory (core).
88: Core is initialized to (filled with) DAT 0,0 before loading any
warriors. Any number of warriors may be loaded into core.
Commentary: Tournaments almost always pit warrior versus warrior
with
only two warriors in core.
的任务. 当一个战士的所有进程被杀死,它就输了. 当只剩下一个战士可
以继续运行,那它就是胜利者.
86: 每个战士最多只能有 64 个进程.
88: 不限制进程数量
----------------------------------------------------------------------
MARS is a multi-tasking system. Warriors start as just one task,
have been killed,the warrior is declared dead. When there is a
sole
warrior still executing in core,that warrior is declared the
winner.
86: Tasks are limited to a maximum of 64 for each warrior.
88: The task limit is not set by the standard.
----------------------------------------------------------------------
第二部分:
Corewar Tutorial Pt ⅡNewsgroups: rec.games.corewar
(Mark A. Durham)
Subject: Intro to Redcode Part Ⅱ
Organization: Rice University,Houston,TX
Date: Thu,14 Nov 1991 09:45:13 GMT
地址模式
寻址模式巧妙地(有时不太巧妙地) 改变指令的作用.这里简单说一下.
细节留待介绍指令集时再讲.
井号(#) 表示操作数为即时寻址模式.即时寻址模式数据包含在当前指令的
字段里. 如果 A-模式是即时的话,数据就在A-字段. 如果 B-模式是即时的
话,数据就在B-字段.
如果没有指明模式 (86: 或使用 '$'),缺省为直接寻址模式. 直接寻址模式
指向相对于当前指令的一条指令. 地址 0 指向当前指令. 直接地址-1指向(物理上)
前一条指令. 直接地址 +1指向(物理上)下一条指令.
Ⅳ. Address Modes
Addressing modes subtly (sometimes not-so-subtly) alter the
behaviour of instructions. A somewhat brief description of their
general properties is given here. Specifics will be left to the
instruction set section.
An octothorpe (#) is used to indicate an operand with an
Immediate
Address Mode. Immediate mode data is contained in the current
instruction's field. If the A-mode is immediate,the data is in the
A-field. If the B-mode is immediate,the data is in the B-field.
If no mode indicator is present (86: or the US dollar sign '$' is present),Direct Address Mode is used. Direct addresses refer to
instructions relative to the current instruction. Address 0 refers
to
the current instruction. Direct address -1 refers to the
(physically)
previous instruction. Direct address +1 refers to the (physically)
next instruction.
@ 表示间接寻址模式. 在间接寻址模式中,间接地址和直接寻址一样也指向一条
指令,但目标不是间接地址指向的指令,而是间接地址指向的指令的B-字段表示
的地址.
例如:
x-2 DAT #0,#0 ; 目标指令
x-1 DAT #0,#-1 ; 指针指令
x MOV 0,@-1 ; 复制自己到 x-2.
The commercial-at (@) is used to indicate Indirect Address Mode.
In indirect addressing,the indirect address points to an
instruction
as in direct addressing,except the target is not the instruction to
which the indirect address points but rather the instruction pointed
to by the B-field of the instruct pointed to by the indirect
address.
Example:
x-2 DAT #0,#0 ; Target instruction.
x-1 DAT #0,#-1 ; Pointer instruction.
x MOV 0,@-1 ; Copies this instruction to location x-2.
小于号 (<) 表示 (86: 自动-,88: 预-)消耗间接寻址模式. 它的作用和
间接寻址模式一样,但指针在使用前会被消耗.
(译者注:好像就是先减 1 再取值)
例如:
x-2 DAT #0,#0 ; 目标指令
x-1 DAT #0,#0 ; 指针指令. 和@的例子比较一下.
x MOV 0,<-1 ; 复制自己到 x-2..
The less-than (<) is used to indicate (86: Auto-,88: Pre-)
Decrement Indirect Address Mode. Its behaviour is just like that of
Indirect Address Mode,except the pointer is decremented before use.
Example:
x-2 DAT #0,#0 ; Target instruction
x-1 DAT #0,#0 ; Pointer instruction. Compare to @ example.
x MOV 0,<-1 ; Copies this instruction to location x-2.
注释:虽然消耗间接寻址模式看起来象间接寻址模式的简单扩展,有时候它真的
很巧妙 - 特别是与 DJN 一起使用. 有一点要注意,它的名字从 '86标准的
自动消耗间接寻址模式 该为 '88 标准的预消耗间接寻址模式.它们的差异在
下面提到,这对一般的Redcode程序员来说是不太重要的. 我建议没兴趣的人
跳过下一段.
Commentary: Although Decrement Indirect addressing appears to be a
simple extension of Indirect addressing,it is really very
tricky
at times - especially when combined with DJN. There are sematic
differences between the '86 and '88 standards,thus the change
in
name from Auto-Decrement to Pre-Decrement. These differences
are
discussed below. This discussion is non-essential for the
average
Redcode programmer. I suggesting skipping to the next section
for
the weak-stomached.
(译者:我也不想翻译呀~但还是拿出专业精神~)
86: Durham: 指令从内存取出到一个指令寄存器. 再对每个操作数求值,储存
位置(到地址寄存器)和指令(到数值寄存器).求值之后指令被执行.
操作数求值: 如果是即时模式,地址寄存器置为0 (当前指令地址) 数值
寄存器置为当前指令. 如果是直接模式,地址寄存器置为字段的值,数值
寄存器置为地址寄存器指向的指令. 如果是间接模式,地址寄存器置为字段
与字段指向的指令的B-字段的和,数值寄存器置为地址寄存器指向的指令.
如果是自动消耗模式,地址寄存器置为一个比字段与字段指向的指令的B-字段
的和少 1 的值,数值寄存器置为地址寄存器指向的指令.
操作数被求值后(但在指令执行前),如果任一模式是自动消耗的话,相应的
内存地址会消耗(译者:就是减1).如果两个模式都是自动消耗且两个字段
都指向同一指针,内存地址会消耗两次. 注意:现在指令指向与任一操作
数以及它在寄存器中的任何一个拷贝都不同的指令.
86: 其他: 和上述相同,除了没有寄存器.所有过程都在内存中进行.
86: Durham: Instructions are fetched from memory into an instruction
register. Each operand is evaluated,storing a location (into an address register) and an instruction (into a value register) for
each operand. After the operands have been evaluated,the
instruction is executed.
Operand Evaluation: If the mode is immediate,the address
register
is loaded with 0 (the current instruction's address) and the
value
register is loaded with the current instruction. If the mode is
direct,the address register is loaded with the field value and
the value register is loaded with the instruction pointed to by
the address register. If the mode is indirect,the address
register is loaded with the sum of the field value and the
B-field
value of the instruction pointed to by the field value and the
value register is loaded with the instruction pointed to by the
address register. If the mode is auto-decrement,the address
register is loaded with a value one less than the sum of the
field
value and the B-field value of the instruction pointed to by the
field value and the value register is loaded with the
instruction
pointed to by the address register. AFTER the operands have
been
evaluated (but before instruction execution),if either mode was
auto-decrement,the appropriate memory location is decremented.
If both modes were auto-decrement and both fields pointed to the
same pointer,that memory location is decremented twice. Note
that this instruction in memory then points to a different
instruction than either operand and also differs from any copies
of it in registers.
86: Other: As above,except there are no registers. Everything is
done in memory.
注释: ICWS'86清楚说明指令寄存器的使用,但其他操作数地址和数值寄存器
都只是隐含的. 歧义性和缺乏对内存和寄存器工作原理的清晰描述让
ICWS'86 备受责难,因而促使 ICWS'88诞生.
88: 同上,除了所有工作都在内存中完成,以及预消耗间接寻址取代了自动消
耗间接寻址. 预消耗间接寻址在操作数求值时消耗内存(译者:注意消耗内存的意思是令该地址的值减1) 而不是求值之后. 它先对A操作数求值
再对B操作数求值.
Commentary: ICWS'86 clearly states the use of an instruction
register,
but the other operand address and value registers are only
implied. Ambiguities and lack of strong statements delineating
what takes place in memory and what takes place in registers
condemned ICWS'86 to eternal confusion and gave birth to
ICWS'88.
88: As above except everything is done in memory and Pre-Decrement
Indirect replaces Auto-Decrement Indirect. Pre-Decrement
Indirect
decrements memory as it is evaluating the operands rather than
after. It evaluates operand A before evaluating operand B.
----------------------------------------------------------------------
指令集
DAT A,B
DAT (数据data) 指令有两个作用. 一,它允许你储存数据作指针,位移等之用
二,执行DAT指令的任何进程都会从进程队列移走. 当战士所有的进程都从进程队
列移走,那它就输了.
86: DAT 只能有一个操作数-- B-操作数. A-字段没有定义(例子中是 #0),
但B-操作数相同的 DAT 指令必须相同.
88: DAT 允许有两个操作数,但只有两种模式 - 即时 和 预消耗.
X: DAT 可带一个或两个任意模式的操作数. 如果只有一个操作数,将被认为是B-操
作数,A-操作数则默认为 #0
注释: 值得注意的是消耗将在进程移出队列之前发生,因为指令在操作数求值后执行
V. Instruction Set
DAT A,B
The DAT (data) instruction serves two purposes. First,it allows you to store data for use as pointers,offsets,etc. Second,any task which executes a DAT instruction is removed from the task queue.
When all of warrior's tasks have been removed from the queue,that warrior has lost.
86: DAT allows only one operand - the B-operand. The A-field is left undefined (the example shows #0),but comparisons of DAT instructions with identical B-operands must yield equality.
88: DAT allows two operands but only two modes - immediate and pre-decrement.
X: DAT takes one or two operands and accepts all modes. If only one operand is present,that operand is considered to be the B-operand and the A-operand defaults to #0.
Commentary: It is important to note that any decrement(s) WILL occur before the task is removed from the queue since the instruction executes only after the operand evaluation.
MOV A,B
MOV (移动move) 指令复制一个字段值(如果均为即时模式)或整个指令 (如果均不是即时模式) 到磁芯的另一个位置(从 A 到 B).
86: Durham: MOV #a,#b 把自己改写为 MOV #a,#a.
注释: 在 ICWS'86 中有一个印刷上的错误,把 MOV #a,B 解释错了.
88: 不允许使用即时的B-模式.(译者:可能译得不对)
X: 允许使用即时的B-模式,和B-操作数为0的效果相同. (参阅 86: Durham: 上面).
MOV A,B
The MOV (move) instruction either copies a field value (if either mode is immediate) or an entire instruction (if neither mode is immediate) to another location in core (from A to B).
86: Durham: MOV #a,#b changes itself to MOV #a,#a.
Commentary: There is a clear typographical error in ICWS'86 which
changes the interpretation of MOV #a,B to something
non-sensical.
the next-to-last line of the second column on page 4.
88: No immediate B-modes are allowed.
X: Immediate B-modes are allowed and have the same effect as a
B-operand of 0. (See 86: Durham: above).
ADD A,B
86: ADD 指令把 A-位置的值和B-位置的值相加,取代B-位置的旧内容
88: 如果 A-模式 是即时模式,ADD的作用同上.如果A-模式不是即时模式,
则A-操作数指向的指令的A-字段和B-字段分别于B-操作数指向的指
令的A-字段和B-字段相加. B-模式不能为即时模式.
X: B-模式可为即时模式,作用与86:相同.
例如: 当执行一次之后 ADD #2,#3 变成 ADD #2,#5 .
ADD A,B
86: The ADD instruction adds the value at the A-location to the
value
at the B-location,replacing the B-location's old contents.
88: If the A-mode is immediate,ADD is interpreted as above. If the
A-mode is not immediate,both the A-field and the B-field of the
instruction pointed to by the A-operand are added to the A-field
and B-field of the instruction pointed to by the B-operand,
respectively. The B-mode can not be immediate.
X: Immediate B-modes are allowed and have the same effect as in 86:.
Example: ADD #2,#3 becomes ADD #2,#5 when executed once.
SUB A,B
SUB (减subtract) 指令和上述的情况相同,不同的是 A 减去 B.
SUB A,B
The SUB (subtract) instruction is interpreted as above for all
three cases,except A is subtracted from B.
JMP A,B
JMP (跳转jump) 指令改变指令指针为 A-操作数.
86: JMP 只能有一个操作数 - A-操作数. B-操作数看作 #0.
88: JMP 允许两个操作数,但A-模式不能为即时模式
X: JMP 允许两个操作数,A-模式可以是即时模式.即时的A-模式操作数在运行是
看成是 JMP 0,B .
JMP A,B
The JMP (jump) instruction changes the instruction pointer to
point
to the instruction pointed to by the A-operand.
86: JMP allows only one operand - the A-operand. The B-operand is
shown as #0.
88: JMP allows both operands,but the A-mode can not be immediate.
X: JMP allows both operands and the A-mode can be immediate. An
immediate A-mode operand is treated just like JMP 0,B when
executed.
JMZ A,B
JMZ (为零跳转jump if zero) 指令当B-操作数指向的指令的B-字段为零时,
跳转到A-操作数指向的指令.
88: 不允许即时的 A-模式.
JMZ A,B
The JMZ (jump if zero) instruction jumps to the instruction
pointed
to by the A-operand only if the B-field of the instruction pointed
to
by the B-operand is zero.
88: Immediate A-modes are not allowed.
JMN A,B
JMN (非零跳转jump if non-zero) 指令当B-操作数指向的指令的B-字段不为零
时,跳转到A-操作数指向的指令.
88: 不允许即时的 A-模式.
JMN A,B
The JMN (jump if non-zero) instruction jumps to the instruction
pointed to by the A-operand only if the B-field of the instruction
pointed to by the B-operand is non-zero.
88: Immediate A-modes are not allowed.
DJN A,B
DJN (消耗后非零跳转decrement and jump if non-zero) 指令消耗B-操作数指向
的指令的B-字段,消耗后不为零就跳转到A-操作数指向的指令.
88: 不允许即时的 A-模式.
DJN A,B
The DJN (decrement and jump if non-zero) instruction causes the
B-field of the instruction pointed to by the B-operand to be
decremented. If the decremented values is non-zero,a jump to the
instruction pointed to by the A-operand is taken.
88: Immediate A-modes are not allowed.
CMP A,B
CMP (比较,相等时跳过compare,skip if equal)指令比较两个字段(如果都是即时模式)或两条完整的指令(如果均不是即时模式),当两者相等时跳过下一条指令.
注释: 在 ICWS'86 中有一个印刷上的错误,把 CMP #a,B 解释错了.
是写对了.
88: 不允许即时的 B-模式.
CMP A,B
The CMP (compare,skip if equal) instruction compares two fields
(if either mode is immediate) or two entire instructions (if neither mode is immediate) and skips the next instruction if the two are
equivalent.
Commentary: There is a clear typographical error in ICWS'86 which
changes the interpretation of CMP #a,B to something
non-sensical.
the fifth line from the bottom of the second column on page 5.
Also,the comments to the example on page 6 have been switched
(equal is not equal and vice versa). The labels are correct
though.
88: Immediate B-modes are not allowed.
SPL A,B
SPL (分裂split)指令 分裂成战士当前进程和一个新进程. 例如: 两个
战士的战斗,1 和 2,战士 1 有两个进程 (1 和 1') 战士 2 只有一个进程,
看起来就想这样: 1,2,1',2,1,2,1',2,等等.
86: SPL 只允许有一个操作数 - B-操作数. A-操作数看成 #0. 执行SPL后,
下一条执行的指令是新增的进程(新进程放在进程队列的前面). 每个战士最
多可以有64个进程.
88: SPL分裂 A-操作数,而不是B-操作数.执行SPL后,下一条执行的指令
是原来没有新增进程时会执行到的那个进程(新进程放在进程队列的后面)
没有明确限制进程的数量. 不允许即时模式的 A-操作数.
X: 允许即时模式的 A-操作数,和 SPL 0,B 的效果一样.
SPL A,B
The SPL (split) instruction splits the execution between this warrior's currently running tasks and a new task. Example: A battle between two warriors,1 and 2,where warrior 1 has two tasks (1 and 1') and warrior 2 has only one task would look like this: 1,2,1',2,1,2,1',2,etc.
86: SPL allows only one operand - the B-operand. The A-operand is shown as #0. After executing the SPL,the next instruction to execute for this warrior is that of the newly added task (the new task is placed at the front of the task queue). A maximum of 64 tasks is allowed for each warrior.
88: SPL splits the A-operand,not the B-operand. After executing the SPL,the next instruction to execute for this warrior is the same instruction which would have executed had another task not been added (the new task is placed at the back of the task queue).
There is no explicit task limit on warriors. Immediate A-operands are not allowed.
X: Immediate A-operands are allowed and behave as SPL 0,B when executed.
88: SLT A,B: SLT (如果小于就跳过skip if less than) 指令 如果A小于B就跳过下一条指令. 不允许即时的 B-模式.
X: 允许即时的 B-模式.
X: XCH A,B: XCH (交换exchange)指令 交换A-操作数指向的指令的A-字段和B-字段
X: PCT A,B: PCT (保护protect) 指令 保护A-操作数指向的指令直到当有指令尝试去覆盖被保护的指令时去除保护.
88: SLT A,B: The SLT (skip if less than) instruction skips the next instruction if A is less than B. No Immediate B-modes are allowed.
X: Immediate B-modes are allowed.
X: XCH A,B: The XCH (exchange) instructions exchanges the A-field and the B-field of the instruction pointed to by the A-operand.
X: PCT A,B: The PCT (protect) instruction protects the instruction pointed to by the A-operand until the protection is removed by an instruction attempting to copy over the protected instruction.
-----------------------------------------
Pseudo-Ops: Instructions to the Assembler
-----------------------------------------
END
END 伪指令表示Redcode源程序结束
86: END 不带操作数
88: 如果END后带有标签,则首先执行END带的标签处的指令.(译者:即程序的入口)
X: ORG A (起点origin) 取代了END初始指令的功能.
END
The END pseudo-op indicates the end of the Redcode source
program.
86: END takes no operands.
88: If END is followed by a label,the first instruction to be executed is that with the label following END.
X: ORG A (origin) takes over this initial instruction function from END.
Commentary: If no initial instruction is identified,the first instruction of your program will be the initial instruction.
86: SPACE A,B: SPACE 伪指令使Redcode源程序清单变得美观. SPACE A,B 的意思是跳过 A 行,保持B行在下一页. 某些汇编程序不支持SPACE,但将它看作注释.
注释: 正常的标签是一个参照物. 例如:
x DAT #0,#x ; 这里x使用在B-字段中
x+1 DAT #0,#x ; 每条指令的B-字段给出
x+2 DAT #0,#x ; 相对x的位移.
等同于
x DAT #0,#0 ; 位移为0
x+1 DAT #0,#-1 ; 1
x+2 DAT #0,#-2 ; 2
但是
x! EQU 0 ; 作用就象 #define x! 0
DAT #0,#x! ; 在扩展系统中,标签里
DAT #0,#x! ; 可以使用惊叹号.
DAT #0,#x! ; 我专用它们
; 来表示即时等同标签.
等同于
DAT #0,#0 ; 直接写文本
DAT #0,#0 ; 在每一行看来
DAT #0,#0 ; 都是一样的
----------------------------------------------------------------------
全文完
86: SPACE A,B: The SPACE pseudo-op helps pretty-up Redcode source listings. SPACE A,B means to skip A lines,then keep B lines on the next page. Some assemblers do not support SPACE,but will treat it as a comment.
Commentary: A normal label is a relative thing. Example:
x DAT #0,#x ; Here x is used in the B-field
x+1 DAT #0,#x ; Each instruction's B-field gives
x+2 DAT #0,#x ; the offset to x.
is the same as
x DAT #0,#0 ; Offset of zero
x+1 DAT #0,#-1 ; one
x+2 DAT #0,#-2 ; two
but
x! EQU 0 ; Equate label like #define x! 0
DAT #0,#x! ; Exclamation points can be used
DAT #0,#x! ; in labels (in Extended systems)
DAT #0,#x! ; I use them exclusively to indicate
; immediate equate labels.
is the same as
DAT #0,#0 ; A direct text replacement
DAT #0,#0 ; appears the same on every
DAT #0,#0 ; line it is used.
----------------------------------------------------------------------
参考资料
最新修订时间:2024-01-07 22:04
目录
概述
人物介绍
参考资料