Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.

Similar presentations


Presentation on theme: "Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook."— Presentation transcript:

1 Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook

2  A stack is a list of data elements where elements are added/removed at top end only  Also known as pushdown stack or last-in-first-out (LIFO) stack  We push a new element on the stack top or pop the top element from the stack  Programmer can create a stack in the memory  There is often a special processor stack as well 2

3  Processor has stack pointer (SP) register that points to top of the processor stack  Push operation involves two instructions: SubtractSP, SP, #4 StoreRj, (SP)  Pop operation also involves two instructions: LoadRj, (SP) AddSP, SP, #4  Maintenance of stack requires checking when the stack is empty or overflows – can be done by checking the SP against lower and upper bounds.  Among other things, the processor stack is useful in subroutine calls. 3

4 4 Stack pointer (SP) register (should) points to top of the processor stack

5 5 SP = 100 Initial Stack 60 64 68 72 76 80 84 88 92 96 100 [something]

6 6 SP = 92 N #A Add couple input parameters 60 64 68 72 76 80 84 88 92 96 100 [something]

7 7 #A N SP = 72 R1 R2 After register saves R3 R4 R5 60 64 68 72 76 80 84 88 92 96 100 [something]

8 8 #A Max Value SP = 72 R1 R2 Update stack with an output parameter R3 R4 R5 60 64 68 72 76 80 84 88 92 96 100 [something]

9 9 SP=92 After return from a function #A Max Value 60 64 68 72 76 80 84 88 92 96 100 [something]

10 10 SP=100 After stack restore by calling program 60 64 68 72 76 80 84 88 92 96 100 [something]

11  You do not talk about…  Leave the stack as you found it *  * some exceptions apply 11

12  Subroutine calls  Parameter passing 12

13  During execution of Call instruction, PC upated to point to instruction after Call  Save this address for Return instruction to use  Simplest method: place address in link register (return address register)  Call instruction performs two operations: store updated PC contents in link register, then branch to target (subroutine) address  Return just branches to address in link register

14 14

15

16 16 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return

17 17 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 1000 204 PC LINK

18 18 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 1000 204 PC LINK

19 19 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 1000 204 PC LINK

20 20 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 2000 1604 204 PC LINK

21 21 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 2000 1604 PC LINK

22 22 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return #Return+4 1604 PC LINK

23 23 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 1604 PC LINK

24 24 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 1604 PC LINK

25 25 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 1604 PC LINK

26 26 SUB1SUB2SUB3 1000 First Instr.. 1600 Call SUB3 1604 Next Instr.. Return. 200 Call SUB2 204Next Instr.. 2000 First Instr.. Return 1604 PC LINK ERROR!

27  When nested calls are made, the last call made is first one to be returned, i.e. the call- return protocol is Last-In First-Out (LIFO).  The nested calls can be arbitrarily deep, e.g. for recursive routines.  Hence, subroutines use the processor stack to store the Link register values during nested calls. 27

28  Mechanism hidden in HLL but must be explicit in assembly language.  A program may call a subroutine many times with different data to obtain different results  Information exchange to/from a subroutine is called parameter passing  Pass input parameters before the subroutine Call receive output parameters after the call  Parameters may be passed & received in registers  Simple, but limited to available registers  Alternative: use stack for parameter passing, and also for local variables & saving registers 28

29  Convert the example program of finding the max of N numbers to a subroutine:  Calling program provides N (number of elements) and the address of the first number (i.e., A[0])  Subroutine MAX(N, #A) returns the max value of N numbers stored starting at location #A.  Hence, there are two input parameters and one output parameter.  First, consider passing& receiving parameters in registers (we use the register assignments in the code we developed earlier to keep it simple)the code we developed earlier ▪ N in reg. R3 ▪ Address #A of A[0] in reg. R5 ▪ Max value returned in R1  Assume the values of N and A[0] are at location N and A. 29

30  Calling Program  Load parameters N and A in the designated registers (R3 & R5)  Call MAX  Return value should be available in the designated register (R1)  Subroutine (MAX)  Push registers (other than parameter registers) used by the subroutine on the stack (i.e. push R2 and R4)  Find the value of the max element and place it in R1 (code identical to what we already saw except for the last two lines, since the value is to be returned in register R1)already saw  Pop registers from the stack (& update stack)  Return  Note: Since MAX does not call another subroutine (it is a leaf subroutine), it does not need to save the Link register on the stack. 30

31  Calling Program  Load parameters in designated registers MoveR3, #N # Location of N LoadR3, (R3)# Value of N Load R5, #A # Address of first element  Call MAX Call MAX  Max value will be available in R1 after return 31

32  Subroutine (MAX)  Push registers R2 and R4 used by the subroutine on the stack Subtract SP, SP, #8# Create room for two items Store R2, 4(SP)# Push R2 Store R4, (SP)# Push R4  Find the value of the max element before  Pop registers from the stack (& update stack) LoadR2, 4(SP)# Restore R2 LoadR4, (SP)# Restore R4 AddSP, SP, #8 # Update stack pointer  Return Return 32

33 33 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Assembly Code

34  Break the Code into three pieces: 1. Becomes part of calling program for passing parameters 2. Becomes part of the subroutine; precede it with register saves; follow it with register restores and return 3. Becomes part of calling program to save returned value 34 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Assembly Code 1 2 3

35 35 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A MoveR2, #Max StoreR1, (R2) Calling Program Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop 1 2 3 MAX Subroutine

36 36 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A MoveR2, #Max StoreR1, (R2) Calling Program Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop 1 2 3 MAX Subroutine CallMAX MAX: Return

37 37 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 MoveR3, #N LoadR3, (R3) MoveR5, #A MoveR2, #Max StoreR1, (R2) Calling Program Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop 1 2 3 MAX Subroutine MAX:Subtract SP, SP, #8 Store R2, 4(SP) Store R4, (SP) LoadR2, 4(SP) LoadR4, (SP) AddSP, SP, #8 Return CallMAX

38  Convert the code for the calling program and MAX to Nios II and verify it works as expected. 38

39  Assume A calls B with input parameters i 1, …,i n and B returns resulting values o 1, …, o m. Assume m <= n (common case).  General Scheme:  A pushes i 1, …,i n on the stack and calls B.  B pushes any (saving) registers it would need for its computation on the stack; also the return-address register if it is not a leaf procedure.  B performs its computation and writes the resulting values o 1, …, o m on the stack (reusing the space used by input parameters).  B pops the saving registers from the stack and returns. 39

40  Calling Program  Push input parameters onto stack (grows high to low), SubtractSP, SP, #8# Create room for 2 items LoadR2, #N# Address of N in R2 LoadR2, (R2)# Value of N in R2 StoreR2, 4(SP)# Push it on the stack LoadR2, #A# Get the second parameter in R2 StoreR2, (SP)# push it on the stack  Call subroutine CallMAX  Get max value from the stack and restores stack (pop input parameters) LoadR1,4(SP) # Result pushed on stack by MAX # Result overwrites value of N on stack AddSP, SP, #8 #Effectively pops stack 40

41  Subroutine MAX  Save all regs. it will use (R1 – R5) on stack Subtract SP, SP, #20Create room for five items Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2 … Store R5, (SP)Push R5  Load input parameters from stack and compute sum LoadR3, 24(SP)Get N into R3 LoadR5, 20(SP)Get #A into R5  Body of the Subroutine:  Restore saved registers and pop stack, return LoadR1, 16(SP)Restore R1 LoadR2, 12(SP)R2… LoadR5, (SP)R5 AddSP, SP, #20Pop stack ReturnReturn to calling program 41

42  Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lines must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 4. Restore R1-R5 and pop stack 5. Return 42 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Earlier Assembly Code

43 43 SP = 100 Initial Stack 60 64 68 72 76 80 84 88 92 96 100 [something]

44 44 SP = 92 N #A Add couple parameters 60 64 68 72 76 80 84 88 92 96 100 [something]

45 45 #A N SP = 72 R1 R2 After register saves R3 R4 R5 60 64 68 72 76 80 84 88 92 96 100 [something]

46 46 #A Max Value SP = 72 R1 R2 Update stack with output parameter R3 R4 R5 60 64 68 72 76 80 84 88 92 96 100 [something]

47 47 SP=92 After return from a function #A Max Value 60 64 68 72 76 80 84 88 92 96 100 [something]

48 48 SP=100 After stack restore by calling program 60 64 68 72 76 80 84 88 92 96 100 [something]

49  Required Changes 1. Push R1–R5 on stack 49 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… Store R5, (SP)Push R5 MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2)

50  Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 50 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… Store R5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2)

51  Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lines must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 51 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… StoreR5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop StoreR1, 24(SP)

52  Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lined must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 4. Restore R1-R5 and pop stack 52 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… StoreR5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop StoreR1, 24(SP) Load R1, 16(SP)Restore R1 Load R2, 12(SP)Restore R2… LoadR5, (SP)Restore R5 AddSP, SP, #20

53  Required Changes 1. Push R1–R5 on stack 2. R3 and R5 must be initialized from the stack 3. Last two lined must be replaced with pushing the result in R1 onto stack where input parameter N was passed. 4. Restore R1-R5 and pop stack 5. Return 53 MAX:Subtract SP, SP, #20 Store R1, 16(SP)Push R1 Store R2, 12(SP)Push R2… StoreR5, (SP)Push R5 LoadR3, 24(SP) LoadR5, 20(SP) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop StoreR1, 24(SP) Load R1, 16(SP)Restore R1 Load R2, 12(SP)Restore R2… LoadR5, (SP)Restore R5 Return

54  Convert the code for the calling program and Max to Nios II and verify it works as expected. 54

55  Locations at the top of the processor stack are used as a private work space by subroutines  A stack frame is allocated on subroutine entry and deallocated on subroutine exit  A frame pointer (FP) register enables access to private work space for current subroutine  With subroutine nesting, the stack frame also saves return address and FP of each caller

56 1. Return Address Register needs to be saved only if the procedure corresponding to this stack frame is non-leaf. 2. A local variable must be saved for a non-leaf procedure only if its value needs to be preserved across a call to another procedure. 56 I/O Parameters Saved FP Local Variables 2 Saving Registers Return Address Register 1 FP

57  As we saw earlier, the stack frame for the call to MAX(A,N) is shown on the right.  This one is without the frame pointer  Note that the space occupied by the input parameters is part of the stack frame of the calling program 57 #A N R1 R2 R3 R4 R5 Stack Frame for call to MAX(A,N)

58 58 #A N R1 R2 R3 R4 R5 Saved [FP] FP

59  Specific to each procedure call  Builds up on the stack for each nested call and builds down for each return  If during a procedure call, stack is used for computation, the stack-frame size may change dynamically  The order of items on a stack frame is by convention only and can change for a different convention. 59

60 The next three slides show step-by-step development of assembly code for another example of a non-recursive procedure. You should study this carefully and then do the following exercise to convert the code to a subroutine, to understand better the process we used to convert the max-finding program

61 itoah(n, S) /* Convert positive integer n to hexadecimal string S in ASCII - non recursive */ int n; char S[]; { int i, j i = 0; do { j = n % 16; /* modulo-16 operator */ if(j<10) S[i++] = j + '0'; else S[i++] = j-10+'A’ /* i++ is C syntax */ } while ((n = n/16) > 0); /* while condition checked at the end of the loop */ } 1.Hand-simulate the program for the call itoah(95, S). 2.Assume, input parameters n & S, and the local variables i & j are mapped to registers R2, R3, R4, and R5 respectively. Further, assume that the program requires no other registers to compute the body of the loop. Show the stack state: (1) before the call to itoah (2) after the call just before the first instruction in the body is executed, and (3) just before the procedure is ready to return. 61

62 62 { int i, j i = 0; do { j = n % 16; if(j<10) S[i++] = j + '0'; else S[i++] = j-10+'A’ } while ((n = n/16) > 0) } { int i, j i = 0; do { j = n % 16; if(j<10) S[i++] = j + '0'; else S[i++] = j-10+'A’ } while ((n = n/16) > 0) }

63 63 nS[i]ij R2R3R4R5 Register Assignments: i = 0 LOOP: j = n and 0xF if(j<10) toto L1 S[i] = j – 10 + ‘A’ goto L2 L1:S[i] = j + ‘0’ L2:i = i+1 n = n/16 if(n>0) goto LOOP MoveR4, R0 LOOP: And R5, R2, #0xF Branch_if_(R5<#10) L1 SubtractR3, R5, #10 AddR3, R3, #41 Branch L2 L1:AddR3, R5, #30 L2:AddR4, R4, #1 Right_ShiftR2, R2, 4 Branch_if_(R2>0) LOOP Intermediate Code Assembly Code

64  Convert the itoah program to two programs: main that calls the subroutine itoah. Assume parameters n and #S are passed in registers.  Note that string S is both an input and the output parameter. 64

65 itoah(n, S, i) /* Convert positive integer n to hexadecimal string S in ASCII, i is index - recursive program*/ int n, i; char S[]; { int j; if ((j = n/16) != 0)/* integer division */ itoah(j, S, i+1); j = n % 16; if(j<10) S[i] = j + '0'; else S[i] = j-10+'A' } 1.Hand-simulate the program for the call itoah(95, S, 0). 2.Assume, input parameters n, S, and i and the local variable j are mapped to registers R2, R3, R4, and R5 respectively. Further, assume that the program requires no other registers to compute the body of the loop. Show the stack state: (1) before each call (2) after each call just before the first instruction in the body is executed, and (3) just before the program is ready to return. 65

66 66

67 67 Variable:Maxi#N = (Addr. N) A[i]#A= Addr. A[0] RegisterR1R2R3R4R5 Intermediate Code Max = A[0] i = 1 Loop: if(!(A[i])>Max)) goto Skip Max = A[i] Skip:i = i+1 if(N>i) goto Loop MoveR3, #N LoadR3, (R3) MoveR5, #A Load R1, (R5) MoveR2, #1 Loop:AddR5, R5, #4 LoadR4, (R5) Branch_if_(R1>=R4) Skip MoveR1, R4 Skip:AddR2, R2, #1 Branch_if_(R3>R2) Loop MoveR2, #Max StoreR1, (R2) Assembly Code Max = A[0] for i=1, i<N { if(A[i]>Max) Max = A[i] } Note: R2 is reused after the for loop to store the address #Max


Download ppt "Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook."

Similar presentations


Ads by Google