Question: A simulation is essentially an execution of the program execution description in a manner similar to how a computer runs a program. The simulator should

A simulation is essentially an execution of the program execution description in a manner similar to how a computer runs a program. The simulator should (i) load the primitives into a list; (ii) iterate over the primitives in the list, executing each one, until a HALT primitive is executed; (iii) keep track of the number of clock ticks each primitive consumes; and (iv) output when each DOOP or BLOCK primitive is executed. Just like a CPU uses a program counter to keep track of the current instruction, the simulator should use an index or a pointer to keep track of the current primitive being executed. Only the DOOP and BLOCK primitives consume clock time. The LOOP, END, and HALT primitives perform control flow in the simulation but do not take any time themselves. The simulation always starts with the clock initialized to 0 ticks. When a DOOP or BLOCK is executed, the number of ticks specified by the primitive is added to the clock. For the example in Figure 1, the first DOOP occurs at clock tick 0, the second DOOP occurs at clock tick 1, the following BLOCK occurs at clock tick 5, and the primitives in the loop commence at clock tick 8. The total clock tick count will be less than 100000. A LOOP primitive specifies the number of times the body of the loop should be executed. Each iteration begins at the primitive following the LOOP and ends at the corresponding END primitive. As loops can be nested, a stack should be used to manage the loops. When a LOOP primitive is encountered the simulator should push 1. the index or the pointer to the first primitive in the loop body, and 2. the number of iterations that need to be performed. The simulator then moves to the first primitive in the loop body. When an END primitive is encountered at the end of the loop body, 1. the number of iterations and the pointer to the first primitive should be popped off the stack, and 2. the number of iterations should be decremented. a. If the remaining number of iterations is greater than 0, the pointer to the first primitive in the loop and the remaining number of iterations is pushed back on the stack, and the simulator is moved back to the first primitive in the loop (using the pointer that was pushed). b. Otherwise, if the remaining number of iterations is 0, then the loop is done and the simulation advances to the primitive after the END. When a HALT primitive is encountered the simulation is terminated, regardless of whether there are primitives after the HALT.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!