Logout

JSR Extra "JSR Extra 6.1.5 - stacks and the machine instruction cycle, plus postfix


JSR Notes:

Stacks in the Machine Instruction Cycle

You should note at this point how stacks are used within the machine instruction cycle. Or more particularly, the way that stacks are used when methods are called within other methods (or in IB-speak, when subprograms ard called from within a main program). When this happens the address of the next instruction in the main programming segment will be put on a stack, and the first address of the subprogram will go to the program counter (followed by the instruction register). When the subprogram is finished, the top address of the stack is poped and goes into the program counter. Naturally, a method calling a method which calls another method, and so on will result in the stack growing taller, and eventually being popped back down to where it started.

 

Postfix Notation and Stacks

Somewhat related to this is the notion of postfix versus infix for mathematical operations. Back in 5.5.1, included is defining postfix notation and prefix notation. This is not the same thing as, for example, in a loop counting up with k++. It's something different. You can see it, and example questions on pages 255 and 256 of the textbook.

Basically postfix is a variation of the order of two operands and an operator:
2 + 3 (prefix notation) becomes
2 3 +

Compilers convert mathematical expressions from infix to postfix. The reason for this is so that stacks can be used when evaluating expressions. What happens with 2 3 + for example is the following:

2 pushed to the stack
3 pushed to the stack
When the operator is encountered, the expression is evaluated, and
The result is pushed to the stack.