Upload
kristian-simpson
View
245
Download
1
Embed Size (px)
Citation preview
Chapter 2-4Subroutines and Stacks
Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture
Chapter 3: ARM Instruction Set Architecture
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
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
Memory Calling program Memory
Subroutinelocation location
SUB….
200 Call SUB 1000 first instruction
204 next instruction …..…. …..
Return
204
204
1000
PC
Link Register
ReturnCall
Subroutine Linkage
Q?
Performed by the processor automatically
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
Two methods i) Using the link register (ex. BL Sub) ii) Using the stack (ex. CALL Sub)
Subroutine Nesting
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
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
Calling programMove N, R1 R1 serves as a counterMove #NUM1, R2 R2 points to the listCall LISTADD Call subroutineMove R0, SUM Save result…
SubroutineLISTADD Clear R0 Initialize sum to 0LOOP Add (R2)+, R0 Add entry from list
Decrement R1Branch > 0 LOOPReturn Return 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
PC
Instructionmemory
Readaddress
Instruction[31–0]
Instruction [20–16]
Instruction [25–21]
Add
Instruction [5–0]
MemtoRegALUOpMemWrite
RegWrite
MemReadBranchRegDst
ALUSrc
Instruction [31–26]
4
16 32Instruction [15–0]
0
0Mux
0
1
Control
Add ALUresult
Mux
0
1
RegistersWriteregister
Writedata
Readdata 1
Readdata 2
Readregister 1
Readregister 2
Signextend
Shiftleft 2
Mux1
ALUresult
Zero
Datamemory
Writedata
Readdata
Mux
1
Instruction [15–11]
ALUcontrol
ALUAddress
Block Diagram of a Processor
Where is the stack located? Where are registers?
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
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
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 programMove #NUM1,-(SP) push parameters onto stackMove N,-(SP)Call LISTADD call subroutine
(top of stack at level 2)Move 4(SP),SUM save resultAdd #8,SP restore top stack
(top of stack at level 1)
Parameter Passing Using the Stack
14
LISTADD MoveMultiple R0-R2,-(SP) save registers to be used(top of the stack at level 3)
Parameter Passing Using the StackSubroutineSubroutine
SPSP
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
Parameter Passing Using the StackSubroutineSubroutine
SPSP
SP+4SP+4SP+8SP+8
SP+16SP+16SP+20SP+20
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
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
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
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
Move FP, -(SP)Move SP, 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 frameAdd #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
The stack pointer moves during The stack pointer moves during the execution of the subroutine the execution of the subroutine and always points to the top of and always points to the top of the stackthe stack
FP provides convenient access FP provides convenient access to the parameters passed to to the parameters passed to the subroutine and to the local the subroutine and to the local memory variables used by the memory variables used by the subroutinesubroutine
4 parameters are passed to the 4 parameters are passed to the subroutinesubroutine
3 local variables are being used 3 local variables are being used within the subroutinewithin the subroutine
R0 and R1 are saved because R0 and R1 are saved because they will be used within the they will be used within the subroutinesubroutine
The parameters can be The parameters can be accessed by using 8(FP), 12(FP) accessed by using 8(FP), 12(FP) , …, …
The local variables can be The local variables can be accessed by using –4(FP), -8(FP)accessed by using –4(FP), -8(FP)
The content of FP remains fixed The content of FP remains fixed throughout the execution of the throughout the execution of the subroutinesubroutine
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
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
Stack Frame
Procedure’s Local Variables
Return Address
Inputs to Procedure
Saved Registers from Caller
Top of stack beforeTop of stack beforeprocedure callprocedure call
Top of stack afterTop of stack afterprocedure callprocedure call
23
Main Program2000 Move PARAM2, -(SP) place parameters on
stack2004 Move PARAM1, -(SP)2008 Call SUB12012 Move (SP), RESULT store result2016 Add #8,SP restore stack level2020 next instruction
First subroutine2100 SUB1 Move FP, -(SP) save frame pointer from main2104 Move SP, FP load the frame pointer for
sub2108 MoveMultiple R0-R3, -(SP) save registers2112 Move 8(FP), R0 get first parameter
Move 12(FP), R1 get second parameter
….Move PARAM3, -(SP) place parameter on stack
2160 call SUB22164 Move (SP)+, R2 Pop SUB2 result into R2….
Move R3, 8(FP) place answer on the stack
MoveMultiple (SP)+, R0-R3 restore registersMove (SP)+, FP restore frame pointer
registerReturn
Stack Frame for nested subroutines
Draw its stack frameDraw its stack frame
24
Second Subroutine
3000 SUB2 Move FP, -(SP) save frame pointer register
Move SP, FP load frame pointer for SUB 2
MoveMultiple R0-R1, -(SP) save registers R0 and R1
Move 8(FP), R0 get the parameter 3…. Get the job done
Move R1, 8(FP) place SUB2 result on stack
MoveMultiple (SP)+, R0-R1 restore registers R0 and R1
Move (SP)+, FP restore frame pointer register
Return return to subroutine 1.
Stack Frame for nested subroutines
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
FP
Old TOS
param1
param2
FP+8
FP+12
For SUB 1
For SUB 2
FP+8
26
Additional Instructions
Logic instructions AND OR NOT dst
1’s complement: complements all the bits of the destination register
2’s complementNOT R0ADD #1, R0
27
Additional Instructions Shift instructions
Logical shifts: LShiftL and LShiftR LShiftL count, dst
Arithmetic shifts: left and right Left: the same as LShiftL
C R0 0
before
after
0 1 1 1 0 . . . 0 1 10
1 1 1 0 . . . 0 1 1 0 0
Logical Shift LeftLShiftL #2, R0
28
beforeLogical Shift RightLShiftR #2, R0
CR00
after
0 1 1 1 0 . . . 0 1 1 0
10 0 0 1 1 1 0 . . . 0
Arithmetic Shift RightAShiftR #2, R0
CR0
after
1 0 0 1 1 . . . 0 1 0 0
11 1 1 0 0 1 1 . . . 0
before
LShiftR and AShiftR
29
C R0
before
after
0 1 1 1 0 . . . 0 1 10
1 1 1 0 . . . 0 1 1 0 0
Rotate Left with CarryRotateLC #2, R0
C R0
before
after
0 1 1 1 0 . . . 0 1 10
1 1 1 0 . . . 0 1 1 0 1
Rotate Left without CarryRotateL #2, R0
RotateL and RotateLC
30
Rotate Right without CarryRotateR #2, R0
CR0
after
0 1 1 1 0 . . . 0 1 1 0
11 1 0 1 1 1 0 . . . 0
before
Rotate Right with CarryRotateR #2, R0
CR0
after
0 1 1 1 0 . . . 0 1 1 0
11 0 0 1 1 1 0 . . . 0
before
RotateR and RotateRC
31
Multiplication Multiply Ri, Rj
The product of two n-bit numbers can be as large as 2n bits
Usually, Rj and Rj+1 are used to store the result, Rj stores the lower order bits and Rj+1 stores the higher order bits
Division Some instruction sets provide a divide instruction Divide Ri, Rj Rj [Rj]/[Ri] Remainder in Rj+1
Computers that do not have multiplication or division instructions can perform them using a sequence of basic operations.
Multiplication and Division
32
Example Programs Vector Dot Product
Move #AVEC, R1 R1 points to vector AMove #BVEC, R2 R2 points to vector BMove N, R3 R3 serves as a counterClear R0 R0 computes the dot
productLOOP Move (R1)+, R4 compute the product
of theMultiply (R2)+, R4 next componentAdd R4, R0 add to previous sumDecrement R3 decrement the counterBranch>0 LOOP loop again if not doneMove R0, DOTPROD store the dot product
Assumption: the dot product result fits in R4
∑i=Ni=1 = AixBi
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
Move #LIST, R0 load LIST into base register R0Move N, R1 Initialize the outer loop indexSubtract #1, R1 set register R1 to j=n-1
OUTER Move R1, R2 Initialize the inner loop indexSubtract #1, R2 set register R2 to k =j-1MoveByte (R0, R1), R3 load LIST(j) into R3, which holds the
current maximum in the sublistINNER CompareByte (R0, R2), R3 if LIST(k) <= [R3]
Branch <= 0 NEXT do not exchangeMoveByte (R0, R2), R4 otherwise, exchange LIST(k) withMoveByte R3, (R0, R2) LIST(j) and load new maximum in R3MoveByte R4, (R0, R1)MoveByte R4, R3 R4 serves as TEMP
NEXT Decrement R2Branch >=0 INNERDecrement R1Branch >0 OUTER
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
Linked Lists
Please study by yourself,Please study by yourself,if you don’t understand, if you don’t understand,
visit me during office hours.visit me during office hours.(One homework question)(One homework question)