Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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 the first DOOP occurs at clock tick the second DOOP occurs at clock tick the following BLOCK occurs at clock tick and the primitives in the loop commence at clock tick The total clock tick count will be less than 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 the index or the pointer to the first primitive in the loop body, and 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, the number of iterations and the pointer to the first primitive should be popped off the stack, and the number of iterations should be decremented. a If the remaining number of iterations is greater than 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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started