Home Download Help Forum Resources Extensions FAQ NetLogo Publications Contact Us Donate Models: Library Community Modeling Commons Beginners Interactive NetLogo Dictionary (BIND) NetLogo Dictionary User Manuals: Web Printable Chinese Czech Farsi / Persian Japanese Spanish
|
NetLogo User Community Models(back to the NetLogo User Community Models)
WHAT IS IT?
This is a model of a generic zero-address computer, also known as a stack machine. This model does not show the numeric output of a program, but rather the standard infix expression for that output. This allows students to test their stack machine programs to see if the programs correctly implement the assigned expression.
HOW IT WORKS
A generic stack machine assembly language has only six operations: Push, Pop, Add, Sub, Mul, and Div. Push adds an item to the top of the stack, then increments the stack pointer. The items on the stack do not operate by themselves in this model; everything happens because of button clicks. This is inherent in any stack model, because only the top of the stack is allowed to be processed.
HOW TO USE IT
First press the Setup button, which builds the base of the stack. Now push at least two items onto the stack, and then try pressing one of the operation buttons. If you have written a Stack Machine program that you wish to test, then for each line of the program simply press the appropriate button. To see a demonstration of a random program, repeatedly press the "Random command" button.
Setup - Initializes variables and builds the base of the stack.
Push - Adds an item to the top of the stack. This requires the user to input the item to be added.
Pop - Removes the top item from the stack, storing it somewhere. In this simulation, it is simply displayed in the console.
Add - Removes the top two items from the stack, adds them, and then pushes their sum onto the stack.
Sub - Removes the top two items from the stack, subtracts them, and then pushes their difference onto the stack.
Mul - Removes the top two items from the stack, multiplies them, and then pushes their product onto the stack.
Div - Removes the top two items from the stack, divides them, and then pushes their quotient onto the stack.
Random command - Generates a single random line of a stack machine program, and then executes it. This is handy for demonstrating how the stack machine works when you do not have a program written yet.
Reset - Clears the command center window and also the graphics window. You must press Setup to build the stack again.
THINGS TO NOTICE
Every time you Push an item, the stack gets one more item on top. For every time you Pop, Add, Sub, Mul, or Div, the stack loses one item.
What happens when you try to Pop and the stack is empty? What happens when you have a single item on the stack, and you try to Add? When you press Sub, how does the stack determine which item is subtracted?
THINGS TO TRY
Convert an arithmetic expression to Reverse Polish Notation (also known as postfix). Enter this RPN expression as a series of stack commands. The final result popped will be the normal infix equivalent of the expression. For example,
C 4 + 3 A - *
would be entered as Push C, Push 4, Add, Push 3, Push A, Sub, Mul, Pop. What is popped should be (C + 4) * (3 – A).
EXTENDING THE MODEL
Add another operator to the stack, such as exponentiation, or sin.
Add a memory bank, where variables may be pushed from, or popped to.
Change the stack machine so that instead of simply changing the label of an item, items always have numeric values and the expressions are evaluated.
Modify push to limit the number of items that can be pushed on the stack (to prevent wrapping).
NETLOGO FEATURES
Several interesting NetLogo features were used in this model. The oper-random procedure uses random-one-of to select the name of a procedure to run, and then uses the run command to call the procedure. This is much cleaner than a series of nested ifelse statements to call the procedure chosen.
Each procedure that removes an item from the stack uses "ifelse any? node-at x y" to check that there is a node (the name of the breed used for items) at position x y.
CREDITS AND REFERENCES
This model was written by Teresa W. Carrigan, April 2004.
Permission to use, modify or redistribute this model is hereby granted,
To refer to this model in academic publications, please use:
In other publications, please use:
For more information on Stack Machines, see one of these textbooks: |
(back to the NetLogo User Community Models)