1 / 49

Stacks

Learn about the ordering of opcodes and operands in computer architecture, including postfix, prefix, and infix notations. Explore the concept of precedence and how it affects the evaluation of expressions. Understand the use of stacks in hardware and evaluating postfix expressions using a stack. Discover the concepts of jumps, conditional jumps, and calling subroutines in computer programming.

hallieb
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter

  2. 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

  3. 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

  4. 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.

  5. 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).

  6. Stack 2 is pushed onto the stack 2 is popped off of the stack 4 2 3 3 1 1 1 1 1

  7. 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.

  8. Evaluating a postfix expression using a stack (1) Enter the postfix expression and click Step

  9. Evaluating a postfix expression using a stack (2)

  10. Evaluating a postfix expression using a stack (3)

  11. Evaluating a postfix expression using a stack (4)

  12. Evaluating a postfix expression using a stack (5)

  13. Evaluating a postfix expression using a stack (6)

  14. Evaluating a postfix expression using a stack (7)

  15. Evaluating a postfix expression using a stack (8)

  16. Evaluating a postfix expression using a stack (9)

  17. Evaluating a postfix expression using a stack (10)

  18. Evaluating a postfix expression using a stack (11)

  19. Evaluating a postfix expression using a stack (12)

  20. Evaluating a postfix expression using a stack (13)

  21. Evaluating a postfix expression using a stack (14)

  22. Evaluating a postfix expression using a stack (15)

  23. Evaluating a postfix expression using a stack (16)

  24. Evaluating a postfix expression using a stack (17)

  25. Evaluating a postfix expression using a stack (18)

  26. Evaluating a postfix expression using a stack (19)

  27. Evaluating a postfix expression using a stack (20)

  28. Evaluating a postfix expression using a stack (21)

  29. Evaluating a postfix expression using a stack (22)

  30. Evaluating a postfix expression using a stack (23)

  31. Evaluating a postfix expression using a stack (24)

  32. Evaluating a postfix expression using a stack (25)

  33. Evaluating a postfix expression using a stack (26)

  34. Evaluating a postfix expression using a stack (27)

  35. Evaluating a postfix expression using a stack (28)

  36. Evaluating a postfix expression using a stack (29)

  37. Evaluating a postfix expression using a stack (30)

  38. Evaluating a postfix expression using a stack (31)

  39. Evaluating a postfix expression using a stack (32)

  40. Evaluating a postfix expression using a stack (33)

  41. Evaluating a postfix expression using a stack (34)

  42. 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.

  43. 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.

  44. 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.

  45. 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)

  46. 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.

  47. 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)

  48. 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).

  49. References • Computer Architecture, Nicholas Carter • Computers Systems: Organization and Architecture, John Carpinelli • http://www.cs.orst.edu/~minoura/cs261/javaProgs/stack/Polish.html

More Related