Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter.

Similar presentations


Presentation on theme: "PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter."— Presentation transcript:

1 PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter

2 PHY 201 (Blum)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 PHY 201 (Blum)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 CSIT 301 (Blum)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 CSIT 301 (Blum)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 CSIT 301 (Blum)6 Stack 11 2 1 3 1 3 4 1 2 is pushed onto the stack 2 is popped off of the stack

7 CSIT 301 (Blum)7 Stack Pointer: don’t move all the data just change the pointer X X X X X 1 X X X X 2 1 X X X X 2 1 X X X X 3 1 X X X 4 3 1 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 PHY 201 (Blum)8 Evaluating a postfix expression using a stack (1) Enter the postfix expression and click Step

9 PHY 201 (Blum)9 Evaluating a postfix expression using a stack (2)

10 PHY 201 (Blum)10 Evaluating a postfix expression using a stack (3)

11 PHY 201 (Blum)11 Evaluating a postfix expression using a stack (4)

12 PHY 201 (Blum)12 Evaluating a postfix expression using a stack (5)

13 PHY 201 (Blum)13 Evaluating a postfix expression using a stack (6)

14 PHY 201 (Blum)14 Evaluating a postfix expression using a stack (7)

15 PHY 201 (Blum)15 Evaluating a postfix expression using a stack (8)

16 PHY 201 (Blum)16 Evaluating a postfix expression using a stack (9)

17 PHY 201 (Blum)17 Evaluating a postfix expression using a stack (10)

18 PHY 201 (Blum)18 Evaluating a postfix expression using a stack (11)

19 PHY 201 (Blum)19 Evaluating a postfix expression using a stack (12)

20 PHY 201 (Blum)20 Evaluating a postfix expression using a stack (13)

21 PHY 201 (Blum)21 Evaluating a postfix expression using a stack (14)

22 PHY 201 (Blum)22 Evaluating a postfix expression using a stack (15)

23 PHY 201 (Blum)23 Evaluating a postfix expression using a stack (16)

24 PHY 201 (Blum)24 Evaluating a postfix expression using a stack (17)

25 PHY 201 (Blum)25 Evaluating a postfix expression using a stack (18)

26 PHY 201 (Blum)26 Evaluating a postfix expression using a stack (19)

27 PHY 201 (Blum)27 Evaluating a postfix expression using a stack (20)

28 PHY 201 (Blum)28 Evaluating a postfix expression using a stack (21)

29 PHY 201 (Blum)29 Evaluating a postfix expression using a stack (22)

30 PHY 201 (Blum)30 Evaluating a postfix expression using a stack (23)

31 PHY 201 (Blum)31 Evaluating a postfix expression using a stack (24)

32 PHY 201 (Blum)32 Evaluating a postfix expression using a stack (25)

33 PHY 201 (Blum)33 Evaluating a postfix expression using a stack (26)

34 PHY 201 (Blum)34 Evaluating a postfix expression using a stack (27)

35 PHY 201 (Blum)35 Evaluating a postfix expression using a stack (28)

36 PHY 201 (Blum)36 Evaluating a postfix expression using a stack (29)

37 PHY 201 (Blum)37 Evaluating a postfix expression using a stack (30)

38 PHY 201 (Blum)38 Evaluating a postfix expression using a stack (31)

39 PHY 201 (Blum)39 Evaluating a postfix expression using a stack (32)

40 PHY 201 (Blum)40 Evaluating a postfix expression using a stack (33)

41 PHY 201 (Blum)41 Evaluating a postfix expression using a stack (34)

42 CSIT 301 (Blum)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 CSIT 301 (Blum)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 CSIT 301 (Blum)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 CSIT 301 (Blum)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 CSIT 301 (Blum)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 CSIT 301 (Blum)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 CSIT 301 (Blum)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 PHY 201 (Blum)49 References Computer Architecture, Nicholas Carter Computers Systems: Organization and Architecture, John Carpinelli http://www.cs.orst.edu/~minoura/cs261/java Progs/stack/Polish.html


Download ppt "PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter."

Similar presentations


Ads by Google