Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2-4 Subroutines and Stacks Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture   Chapter 3: ARM Instruction Set Architecture.

Similar presentations


Presentation on theme: "Chapter 2-4 Subroutines and Stacks Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture   Chapter 3: ARM Instruction Set Architecture."— Presentation transcript:

1 Chapter 2-4 Subroutines and Stacks Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture   Chapter 3: ARM Instruction Set Architecture

2 2 Subroutines It is often necessary to perform a particular task many times on different data values When a program branches to a subroutine, we say that it is calling the subroutine The instruction that performs the branch is called a call_subroutine instruction After a subroutine has been executed, the calling program must resume execution: return Execution continues at the instruction immediately after the instruction that called the subroutine The location where the calling program resumes execution is the location pointed to by the PC while the call_subroutine is being executed

3 3 The contents of the PC must be saved by the call_subroutine instruction to enable correct return to the calling program Call_subroutine is a special branch instruction that performs the following operations  store the contents of the PC in the link register (LR)  branch to the target address specified by the instruction The return from a subroutine branches to the address contained in the link register Subroutines (cont.)

4 4 MemoryCalling program Memory Subroutine locationlocation SUB …. 200Call SUB 1000 first instruction 204next instruction ….. …. ….. Return 204 1000 PC Link Register ReturnCall Subroutine Linkage Q? Performed by the processor automatically

5 5 When one subroutine calls another The return address of the second call is also stored in the link register It is essential to save the content of the link register in another location before issuing a new call Subroutine nesting can be carried out to any depth A stack data structure can be used to store the return addresses associated with subroutine calls Call-subroutine pushes the content of the PC onto the stack and loads the subroutine address into the PC The return instruction pops the return address from the stack into the PC Subroutine Nesting

6 6 Two methods  i) Using the link register (ex. BL Sub)  ii) Using the stack (ex. CALL Sub) Subroutine Nesting

7 7 What do you know about parameter passing in C ? Parameter Passing int CallingFunction() { int test1; int test2; int *ptr1; … test1 = Procedure(test2, ptr); if (test1 > 0) test2 = 0; else test2 = Subroutine(); return test2; } int Procedure(int subtest2, int *subptr1) { int test3; if (subtest2 == 0) test3 = … ; else test3 = … ; return test3; }

8 8 When calling a subroutine, a program must provide the subroutine with parameters to be used in the computation Later the subroutine may return other parameters resulting from the computation The parameters may be placed in registers or in the stack (local variables) or in fixed memory locations (global variables) Parameter Passing

9 9 Calling program Move N, R1R1 serves as a counter Move #NUM1, R2R2 points to the list CallLISTADDCall subroutine MoveR0, SUMSave result … Subroutine LISTADDClear R0Initialize sum to 0 LOOPAdd(R2)+, R0Add entry from list Decrement R1 Branch > 0LOOP ReturnReturn to calling program Address location is passed in R2; Counter in R1, Sum is returning in R0 Q? Parameter Passing Using Registers Passing parameters through the registers is straightforward, efficient and fast   however there are a limited number of registers   the calling program may need to retain information in some registers for use after returning from the subroutine

10 10 Block Diagram of a Processor Where is the stack located? Where are registers?

11 11 Registers vs. Stack What are registers?  A small amount of very fast computer memory used to speed the execution of computer programs by providing quick access to commonly used values—typically, the values being calculated at a given point in time.  Most, but not all, modern computer architectures operate on the principle of moving data from main memory into registers, operating on them, then moving the result back into main memory—a so-called load-store architecture.  The top of the memory hierarchy, and provide the fastest way for the system to access data. The term is often used to refer only to the group of registers that can be directly indexed for input or output of an instruction.

12 12 Registers vs. Stack What is the stack?  A memory block used to temporarily save values, beyond the amount of data that registers can hold  Push adds a given node to the top of the stack leaving previous nodes below.  Pop removes and returns the current top node of the stack.  Typically grows towards descending addresses

13 13 Alternatively, parameters can be placed on the stack  Using a stack is flexible  No need to match which registers are using  A stack can handle a large number of parameters Example Top of stack is at level 1 when execution starts Main program Move #NUM1,-(SP) push parameters onto stack Move N,-(SP) Call LISTADDcall subroutine (top of stack at level 2) Move4(SP),SUMsave result Add#8,SPrestore top stack (top of stack at level 1) Parameter Passing Using the Stack

14 14 LISTADD MoveMultiple R0-R2,-(SP) save registers to be used (top of the stack at level 3) Subroutine Parameter Passing Using the Stack Subroutine SP

15 15 LISTADD MoveMultiple R0-R2,-(SP) save registers to be used (top of the stack at level 3) Move 16(SP), R1 initialize counter to n Move 20(SP), R2 initialize pointer to the list Clear R0 initialize sum to 0 LOOP Add (R2)+, R0 add entry from list Decrement R1 Branch>0 LOOP Move R0, 20(SP) put result on the stack MoveMultiple (SP)+,R0-R2 restore registers Return return to calling program Subroutine Parameter Passing Using the Stack Subroutine SP SP+4 SP+8 SP+16 SP+20

16 16 The subroutine uses three registers The contents of these registers are saved by pushing them on the stack using the move_multiple instruction Before return, they are restored Parameter Passing Using the Stack

17 17 Passing by reference  Instead of passing the actual list entries, the calling program passes a pointer, which is the address of the list in the memory Passing by value  The actual number of entries n is passed to the subroutine Parameter Passing

18 18 Stack management  During the execution of the subroutine, 6 locations at the top of stack contain entries that are needed by the subroutine  These locations are in a private work space for the subroutine created at the time the subroutine is entered and freed up when the subroutine returns  I.e., Local variables in high- level languages are created in the stack; thus they are temporary  This work space is called a stack frame. Layout of a Typical Stack Frame saved [R1] saved [R0] localvar2 localvar3 localvar1 Return address saved [FP] param1 param2 param3 param4 SP FP (frame pointer) Old TOS stack frame for called subroutine

19 19 Assuming the SP point to the old TOS Before the subroutine is called, the calling program pushes the 4 parameters onto the stack The call instruction is executed and the return address is pushed onto the stack The first two instructions executed by the subroutine push the content of FP (for main) onto the stack MoveFP, -(SP) MoveSP, FP Now, both FP and SP point to the location of the saved FP Space for the three local variable is allocated Subtract #12, SP The contents of R0 and R1 are saved The subroutine executes its task The saved R0 and R1 are popped back The local variables are removed from the stack frame Add#12,SP saved [R1] saved [R0] localvar2 localvar3 localvar1 Return address saved [FP] param1 param2 param3 param4 SP FP (frame pointer) Old TOS Layout of a Typical Stack Frame

20 20 The stack pointer moves during the execution of the subroutine and always points to the top of the stack The stack pointer moves during the execution of the subroutine and always points to the top of the stack FP provides convenient access to the parameters passed to the subroutine and to the local memory variables used by the subroutine FP provides convenient access to the parameters passed to the subroutine and to the local memory variables used by the subroutine 4 parameters are passed to the subroutine 4 parameters are passed to the subroutine 3 local variables are being used within the subroutine 3 local variables are being used within the subroutine R0 and R1 are saved because they will be used within the subroutine R0 and R1 are saved because they will be used within the subroutine The parameters can be accessed by using 8(FP), 12(FP), … The parameters can be accessed by using 8(FP), 12(FP), … The local variables can be accessed by using –4(FP), -8(FP) The local variables can be accessed by using –4(FP), -8(FP) The content of FP remains fixed throughout the execution of the subroutine The content of FP remains fixed throughout the execution of the subroutine saved [R1] saved [R0] localvar2 localvar3 localvar1 Return address saved [FP] param1 param2 param3 param4 SP FP (frame pointer) Old TOS stack frame for called subroutine Layout of a Typical Stack Frame

21 21 Returning the subroutine Restore R0 and R1 The saved old value of FP is popped back into FP SP points to the return address The return instruction is executed The calling program is responsible for removing the parameters from the stack. Some of these parameters may be results passed back to the calling program. saved [R1] saved [R0] localvar2 localvar3 localvar1 Return address saved [FP] param1 param2 param3 param4 SP FP (frame pointer) Old TOS Layout of a Typical Stack Frame

22 22 Stack Frame Procedure’s Local Variables Return Address Inputs to Procedure Saved Registers from Caller Top of stack before procedure call Top of stack after procedure call

23 23 Main Program 2000MovePARAM2, -(SP)place parameters on stack 2004MovePARAM1, -(SP) 2008CallSUB1 2012Move(SP), RESULTstore result 2016Add#8,SPrestore stack level 2020next instruction First subroutine 2100 SUB1MoveFP, -(SP)save frame pointer from main 2104MoveSP, FPload the frame pointer for sub 2108MoveMultiple R0-R3, -(SP)save registers 2112Move 8(FP), R0get first parameter Move 12(FP), R1get second parameter …. Move PARAM3, -(SP)place parameter on stack 2160call SUB2 2164Move(SP)+, R2Pop SUB2 result into R2 …. MoveR3, 8(FP)place answer on the stack MoveMultiple(SP)+, R0-R3restore registers Move (SP)+, FPrestore frame pointer register Return Stack Frame for nested subroutines Draw its stack frame

24 24 Second Subroutine 3000 SUB2MoveFP, -(SP)save frame pointer register MoveSP, FPload frame pointer for SUB 2 MoveMultiple R0-R1, -(SP)save registers R0 and R1 Move8(FP), R0get the parameter 3 ….Get the job done MoveR1, 8(FP)place SUB2 result on stack MoveMultiple(SP)+, R0-R1restore registers R0 and R1 Move(SP)+, FPrestore frame pointer register Returnreturn to subroutine 1. Stack Frame for nested subroutines

25 25 [R1] from SUB1 [R0] from SUB1 2164 [FP] from SUB1 param3 [R2] from main [R3] from main [R1] from main [R0] from main [FP] from main 2012 FP Old TOS param1 param2 FP+8 FP+12 For SUB 1 For SUB 2 FP+8

26 26 Additional Instructions Logic instructions  AND  OR  NOT dst  1 ’ s complement: complements all the bits of the destination register  2 ’ s complement NOT R0 ADD#1, R0

27 27 Additional Instructions Shift instructions  Logical shifts: LShiftL and LShiftR  LShiftL count, dst  Arithmetic shifts: left and right  Left: the same as LShiftL C R00 before after 0 1 1 1 0... 0 1 10 11 1 0... 0 1 1 0 0 Logical Shift Left LShiftL#2, R0

28 28 before Logical Shift Right LShiftR#2, R0 C R0 0 after 0 1 1 1 0... 0 1 1 0 10 0 0 1 1 1 0... 0 Arithmetic Shift Right AShiftR#2, R0 C R0 after 1 0 0 1 1... 0 1 0 0 11 1 1 0 0 1 1... 0 before LShiftR and AShiftR

29 29 C R0 before after 0 1 1 1 0... 0 1 10 11 1 0... 0 1 1 0 0 Rotate Left with Carry RotateLC#2, R0 C R0 before after 0 1 1 1 0... 0 1 10 11 1 0... 0 1 1 0 1 Rotate Left without Carry RotateL#2, R0 RotateL and RotateLC

30 30 Rotate Right without Carry RotateR#2, R0 C R0 after 0 1 1 1 0... 0 1 1 0 11 1 0 1 1 1 0... 0 before Rotate Right with Carry RotateR#2, R0 C R0 after 0 1 1 1 0... 0 1 1 0 11 0 0 1 1 1 0... 0 before RotateR and RotateRC

31 31 Multiplication  MultiplyR i, R j  The product of two n-bit numbers can be as large as 2n bits  Usually, R j and R j+1 are used to store the result, R j stores the lower order bits and R j+1 stores the higher order bits Division  Some instruction sets provide a divide instruction  Divide R i, R j R j  [R j ]/[R i ]  Remainder in R j+1 Computers that do not have multiplication or division instructions can perform them using a sequence of basic operations. Multiplication and Division

32 32 Example Programs Vector Dot Product Move#AVEC, R1R1 points to vector A Move#BVEC, R2R2 points to vector B MoveN, R3R3 serves as a counter ClearR0R0 computes the dot product LOOPMove(R1)+, R4compute the product of the Multiply(R2)+, R4next component AddR4, R0add to previous sum DecrementR3decrement the counter Branch>0LOOPloop again if not done MoveR0, DOTPRODstore the dot product Assumption: the dot product result fits in R4 ∑ i=N i=1 = A i xB i

33 33 Sort a list of bytes stored in the memory into ascending alphabetical order The bytes in the list are not necessarily distinct Each byte contains the ASCII code for a character from A through Z The list is in memory location LIST through LIST+n-1 n: the number of bytes in the list, 32 bit value stored at address N In place sorting Algorithm: the largest element is found and placed at the end of the list (location LIST+n-1), the second largest is found and placed in location LIST+n-2, etc … for (j =n-1; j > 0; j = j-1) {for (k =j-1; k >= 0 ; k = k-1) { if (LIST[k] > LIST[j])// Does a larger sit earlier? { TEMP = LIST[k]; LIST[k] = LIST[j];// Then, swap two numbers LIST[j] = TEMP; } }Location: 0, 1, 2, 3, 4, 5 }Values: 61, 75, 65, 80, 63, 70 Byte Sorting Program

34 34 Move#LIST, R0load LIST into base register R0 MoveN, R1Initialize the outer loop index Subtract#1, R1set register R1 to j=n-1 OUTERMoveR1, R2Initialize the inner loop index Subtract#1, R2set register R2 to k =j-1 MoveByte(R0, R1), R3load LIST(j) into R3, which holds the current maximum in the sublist INNERCompareByte(R0, R2), R3if LIST(k) <= [R3] Branch <= 0NEXTdo not exchange MoveByte(R0, R2), R4otherwise, exchange LIST(k) with MoveByteR3, (R0, R2)LIST(j) and load new maximum in R3 MoveByteR4, (R0, R1) MoveByteR4, R3R4 serves as TEMP NEXTDecrementR2 Branch >=0INNER DecrementR1 Branch >0OUTER Byte Sorting Program (cont.) for (j =n-1; j > 0; j = j-1) {for (k =j-1; k >= 0 ; k = k-1) { if (LIST[k] > LIST[j]) { TEMP = LIST[k]; LIST[k] = LIST[j]; LIST[j] = TEMP; } Location: 0, 1, 2, 3, 4, 5 Values: 61, 75, 65, 80, 63, 70

35 35 Linked Lists Please study by yourself, if you don’t understand, visit me during office hours. (One homework question)


Download ppt "Chapter 2-4 Subroutines and Stacks Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture   Chapter 3: ARM Instruction Set Architecture."

Similar presentations


Ads by Google