⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code_instruction.html

📁 有趣的模拟进化的程序 由国外一生物学家开发 十分有趣
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  TRACE</pre><p>Where you have to replace <tt>organism.inst_test</tt> with the name of theorganism you want to trace.  The new file will appear in the <tt>genebank/</tt>directory, with the same name as the one you loaded in, but a "<tt>.trace</tt>"appended to the end.<h3>CPU Components</h3><p>The various CPU components are often manipulated by instructions, and weneed a standard way of doing this.  We have settled on each component beingassociated with a method to access it, to provide a single location that cancontrol that access.  This has already been useful -- in a multi-threaded CPU (i.e., a CPU that has multiple positions in its genome being executed atthe same time) each thread has its own registers and heads, so we need toalways be sure we are manipulating the component of the active thread.  Ifyou simply use the following methods, they will always find the correctcomponent for you.<table cellpadding=5><tr><td><tt>void <font color="#008800">StackPush</font>(<font color="#880000">int</font> <font color="#000088">value</font>);<br><font color="#880000">int</font> <font color="#008800">StackPop</font>();<br>void <font color="#008800">SwitchStack</font>();</tt><br>There are two stacks in a normal CPU, and more in a multi-threaded version(one global stack, and one local to each thread).  The first stack method willplace a new value on the top of the currently active stack, the second willremove the top value, and the last will toggle the currently active stack.<tr><td><font color="#880000">cCPUHead</font> & <font color="#008800">GetHead</font>(<font color="#880000">int</font> <font color="#000088">head_id</font>);<br>        <font color="#880000">cCPUHead</font> & <font color="#008800">IP</font>();<br>Each thread in a CPU has four heads associated with it, designated by theconstants <tt>HEAD_IP</tt>, <tt>HEAD_READ</tt>, <tt>HEAD_WRITE</tt>, and <tt>HEAD_FLOW</tt>.  These heads each point to a position in memory, and allhave their own purpose.  I will discuss exactly how to use heads at a latterpoint, but for the moment just know that a head can be accessed by passingthe appropriate constant into the GetHead() method.  The extra method IP()was added to more easily obtain just the instruction pointer.  The IP is avery special head since it designates what instruction is going to beexecuted next, and often it will make code clearer if you obtain it by callingIP().  (It will show that you need to make sure of the special qualities ofthe instruction pointer.)<tr><td><font color="#880000">int</font> & <font color="#008800">Register</font>(<font color="#880000">int</font> <font color="#000088">reg_id</font>);<br>There are three registers available, associated with the constants<tt>REG_AX</tt>, <tt>REG_BX</tt>, and <tt>REG_CX</tt>.  If the Register()method is called, an integer reference will be returned associated with thatregister.  Any change to this integer will make a corresponding change tothe register in question.<tr><td><font color="#880000">cCPUMemory</font> & <font color="#008800">Memory</font>();<br>This method allows the programmer to access the full memory of the CPU.  Asyou know, the class cCPUMemory is built on top of cGenome, so you canmanipulate it in all of the same ways.</table>These are only a sampling of the available methods of interacting with thecomponents of the CPU, but they give you a good cross-section withoutoverwhelming you with all of the possibilities.  You should look through thesource files if you want to see the other options that are available to you.<h3>Helper Methods</h3><p>There are several very common tasks that are performed during the executionof many of the instructions.  For each of these tasks we have created ahelper function to ease the creation of new instructions.<table cellpadding=5><tr><td>void <font color="#008800">ReadLabel</font>();<br>        <font color="#880000">cCodeLabel</font> & <font color="#008800">GetLabel</font>();<br>        <font color="#880000">cCPUHead</font> <font color="#008800">FindLabel</font>(<font color="#880000">int</font> <font color="#000088">direction</font>);<br>ReadLabel() will read in the label (series of nop instructions) thatimmediately follows the instruction being executed.  The label that was readcan then be accessed (and even modified)  using GetLabel().  Finally, theFindlabel() method takes single int argument that determines the direction(from the instruction pointer) in which the label should be search for.  Ifthis argument is a 1 it will search forward, and if its a -1, it will searchbackwards.  A zero indicates that the search should start from the beginningof the genome, and proceed to the end.  Once it finds the matching label, itwill return a head at the position in which the label was found.  Thesehelper methods are particularly useful in any instruction that has to affectother portions of the source code.  See the method Inst_HeadSearch for anexample of how these are used.<tr><td><font color="#880000">int</font> <font color="#008800">FindModifiedRegister</font>(<font color="#880000">int</font> <font color="#000088">default_register</font>);<br>        <font color="#880000">int</font> <font color="#008800">FindModifiedHead</font>(<font color="#880000">int</font> <font color="#000088">default_head</font>);<br>These two methods will look ahead and determine if a nop instruction isimmediately following the instruction being executed.  If so, it will returnthe register or head ID associated with that nop (for use in the rest of themethod), and if no nop is found, it will automatically return thedefault value passed in.  We used FindModifiedRegister in the example newinstruction above.<tr><td><font color="#880000">int</font> <font color="#008800">FindComplementRegister</font>(<font color="#880000">int</font> <font color="#000088">base_reg</font>);<br>Several instructions are defined as affecting a certain, modifiable registerand its complement.  In order to have a standard way of determining thecomplement of a register (which, by default, cycle in the same order ascomplement labels), we use this function whenever we need to determine one.See, for example, see the definition of Inst_IfNEqu() in the handout for thefile <tt>hardware_cpu.cc</tt>.<tr><td>void <font color="#008800">Fault</font>(<font color="#880000">int</font> <font color="#000088">fault_loc</font>, <font color="#880000">int</font> <font color="#000088">fault_type</font>, <font color="#880000">cString</font> <font color="#000088">fault_desc</font>="");<br>This helper function should be called whenever an instruction cannot performits operation successfully.  Ideally, most instructions should do somethingmeaningful in any situation so that faults are unnecessary, but sometimesthis doesn't work out well.  The input argument "<tt>fault_loc</tt>" shouldtell where the fault is stemming from to help the user trace it back.  Therange of possible fault locations can be found in the file<tt>cpu_defs.hh</tt>, but you can use <tt>FAULT_LOC_DEFAULT</tt> in mostcases.  The next input in the fault type, which is either FAULT_TYPE_ERRORin the case that the creature actively made a mistake (such as trying todivide before copying an offspring, or trying to take the square root of anegative number), or else FAULT_TYPE_WARNING if its not directly thecreature's fault, such as if it is trying to interact with another creaturethat has just died (it couldn't help the fact that the other creature died).Finally, a string should be given that describes the reason for the fault(this will be readable by humans that are stepping through the organism.)The main reason for faults at the moment is to help us debug an organism.Faults can also be made lethal to the organisms if we want them to developmore "correct" code.</table><h3>Homework</h3><p>For your homework, you will be writing two new instructions in avida.  Thefirst one is the mathematical instruction "<b><tt>cube</tt></b>" which willtake the ?BX? register, and put its value to the third power.  If you lookin the actual source files, you will see that there is already a"<tt>square</tt>" instruction that you can model this on.<p>Next, you will implement the instruction "<b><tt>if-twice</tt></b>" whichwill execute the next instruction if-and-only-if the value in the ?BX?register is twice that of the value in its complement.  In other words bydefault, if would test of BX was twice CX, but if it is followed by a<tt>nop-C</tt> it will test if CX is twice AX.<p>For both of these instruction make sure to craft an organism to test thatthey are working properly!  Turn in this organism (or organisms) to me alongwith the source code when you finish the assignment.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -