490 likes | 692 Views
Stacks. Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter. Ordering of opcodes and operands. Postfix: operand(s) then opcode 4 5 + Works well with stacks Prefix: opcode then operand(s) + 4 5 Infix: operand opcode operand 4 + 5.
E N D
Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter
Ordering of opcodes and operands • Postfix: operand(s) then opcode • 4 5 + • Works well with stacks • Prefix: opcode then operand(s) • + 4 5 • Infix: operand opcode operand • 4 + 5
Precedence (aka Order of Operations) • Precedence is the order in which operations occur when an expression contains more than one operation. • Operations with higher precedence are performed before operators with lower precedence. • 1 + 2 * 3 - 4 • 1 + 6 - 4 (multiplication has higher precedence) • 7 - 4 (start on the left when operators have the same precedence) • 3
Postfix good for Hardware • Postfix order is better suited for hardware since one must prepare the inputs (a.k.a. the data, a.k.a. the operands) before operating on them to get an output. • Postfix is particularly well suited for architectures that use a stack to perform computations.
The stack • A stack is a data structure (which may be implemented in hardware or in software) that holds a sequence of data but limits the way in which data is accessed. • A stack obeys the Last-In-First-Out (LIFO) protocol, the last item written (pushed) is the first item to be read (popped).
Stack 2 is pushed onto the stack 2 is popped off of the stack 4 2 3 3 1 1 1 1 1
Stack Pointer: don’t move all the data just change the pointer The stack pointer is pointing to the next available location in the stack. When it’s pointing at the 2, the 2 is no longer on the stack.
Evaluating a postfix expression using a stack (1) Enter the postfix expression and click Step
Jump • A processor generally assumes that the next statement to be executed is the next line of the code stored in memory. • Recall part of the fetch-execute cycle is to increment the program counter (PC). • A jump or goto alters this usual plan. A new value is placed into the program counter.
Conditional Jump • A conditional jump will change the PC or leave it unchanged based on the state of one of the Flag registers. • The ALU can perform some comparison and place the result in a flag register, then the flag’s value could eventually be fed to the PC load control. So the PC is loaded (changed) or not based on the flag which came from the comparison. • Conditional jumping gives the programmer ifs and loops.
Calling a subroutine • A subroutine call involves a jump. This “passes the control” to the subroutine. But a subroutine call differs from an ordinary jump in that one wants to return. • One reason for having subroutines is to reduce repeated code. The subroutine may be called from any number of locations, so the return address is not known until the program is running.
Storing the PC • Prior to executing the jump portion of a call, the PC holds the address of the line of code immediately following the call instruction. And this is where one wants to be upon returning from the subroutine. Thus we have to store the value of the program counter (somewhere) and then change the value of the PC (i.e. execute the jump)
A subroutine can call another subroutine that can call another subroutine that …. • There cannot be a simple register for storing the PC (return address) because a subroutine can call another subroutine requiring two stored values. • In certain early architectures there was just a register for this purpose, but that was too limiting.
Calling and Returning • Calling: If Main calls RoutineA, we store the MainReturnAddress. Then if RoutineA calls RoutineB, we store RoutineAReturnAddress. • Returning: RoutineB returns control to RoutineA so we first need the last return address stored (RoutineAReturnAddress). Then RoutineA returns the control to Main and we need MainReturnAddress. • The last return address stored is the first one needed, this is the protocol of a stack. (LIFO)
Call is to Push as Return is to Pop • A subroutine call thus requires pushing the value of the PC onto a stack (and then changing the value of the PC). • A return from a subroutine requires popping the return address from the stack (and then placing that value in the PC).
References • Computer Architecture, Nicholas Carter • Computers Systems: Organization and Architecture, John Carpinelli • http://www.cs.orst.edu/~minoura/cs261/javaProgs/stack/Polish.html