Unformatted text preview:

Stack Frames: Using LINKAssembling source codeUsing multiple source filesLocal Variables in SubroutinesRecursionLocal Variables and RecursionUsing the stack for local variablesWhat LINK does. (Conceptually)What LINK does. (Exactly)UNLKA subroutine using LINKThe stack for that subroutine:What if the subroutine calls itself?ExampleExample StackLINK An,#0 – Why would I do that??The stack and the heapA Recursive SubroutineHow C uses LINK…You should know…Stack Frames: Using LINKCEG 320/520 8: Stack frames and LINK 2Assembling source code•One source file:–Use ORG statements for data and code–Assemble and link in one step using:asm68k file.asm –lx•Multiple source files:–ORG statement is not allowed.–New assembler directives:•SECTION name,code — Needed for linking.•XDEF label — Make a label available to the world•XREF label — Import a label from another fileCEG 320/520 8: Stack frames and LINK 3Using multiple source files•First assemble into relocatable object files:asm68k file1.asm –lx –rasm68k file2.asm –lx –r–Both of the .o files will have internal addresses starting at $0000 0000•Then link into an S-record file:link 1000 –m prog.s file1.o file2.o–Use link68k on UNIX machines–The 1000 is the base address for the program–Use the .map file and the .lst file to find addresses for your code.CEG 320/520 8: Stack frames and LINK 4Local Variables in Subroutines•What if we need more than 8 local variables?What if we need more than 8 local variables?•If we use DC and DS, then all local variables for all subroutines, take up memory all the time.–For large programs, this can be a problem.•Using DC and DS also causes problems with recursion – only one copy of the variables.CEG 320/520 8: Stack frames and LINK 5Recursion•A recursive function to compute Fibonacci #’s:int fib(int N) { int prev, pprev; if (N == 1) { return 0; } else if (N == 2) {return 1; } else { prev = fib(N-1); pprev = fib(N-2); return prev + pprev; }}fib(5)fib(5)CEG 320/520 8: Stack frames and LINK 6Local Variables and Recursion•We could use named memory locations, but then what happens if a subroutine calls itself (recursion)?SUB1 MOVE.W 4(SP),VAR1MOVE.W 8(SP),VAR2…JSR SUB1MOVE.W VAR2,D0RTSVAR1 DS.W 1VAR2 DS.W 1•Anything that can be done in a high level language must be do-able in assembly!CEG 320/520 8: Stack frames and LINK 7Using the stack for local variables•To allow for the possibility of recursion, we keep local variables on the stack.•The LINK statement allocates space for local variables in subroutines.LINK A6,#-8•Allocates 8 bytes (4 words) for local variables. Called a stack frame.•Saves the value of A6 and establishes A6 as a frame pointer.CEG 320/520 8: Stack frames and LINK 8What LINK does. (Conceptually)•Conceptually, link makes some room on the stack for local variables.•UNLK puts things back “like they were”.–Much like RTS$7000?$6FFE$6FFCx x$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEAA6:$6FE8xxLINK A6,#-8Old SPNew SPCEG 320/520 8: Stack frames and LINK 9What LINK does. (Exactly)LINK A6,#-8MOVEM.L …•SP <- [SP] – 4•[SP] <- [An]•An <- [SP]•SP <- [SP] + disp•Parameters:–8(A6) = P2–10(A6) = P1•Local variables:–-2(A6) through -8(A6)•Now MOVEM doesn’t affect these values!$7000?rtrnrtrn$6FFE$6FFCOld A6Old A6x xP1P2$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA6FF4A6:$6FE8xxCEG 320/520 8: Stack frames and LINK 10UNLKUNLK A6•SP <- [An]•An <- [[SP]]•SP <- [SP] + 4•Back to where we started, ready to execute RTS.$7000?rtrnrtrn$6FFE$6FFCOld A6Old A6x xP1P2$6FFA$6FF8$6FF6$6FF4$6FF2$6FF0$6FEE$6FEC$6FEA6FF4A6:$6FE8xxCEG 320/520 8: Stack frames and LINK 11A subroutine using LINKSUB1 LINK A6,#-4MOVEM.L D2-D3/A2-A3,-(SP)MOVE.L 8(A6),D2 ; Param2 = 8(A6)MOVE.L 12(A6),D3 ; Param1 = 12(A6)MOVE.W D3,VAR1(A6) ; Local Var1 = -2(A6)MOVE.W D2,VAR2(A6) ; Local Var2 = -4(A6)…MOVEM.L (SP)+,D2-D3/A2-A3UNLK A6RTSVAR1 EQU -2VAR2 EQU -4Assume we are expecting two parameters, and both are long words:CEG 320/520 8: Stack frames and LINK 12The stack for that subroutine:?P2P2RTNARTNAOld A6 Old A6P1P1X(v1)X(V2)D2D2D3A2A3A3D3A2Original SPSP afterLINKTemp SP, then A6SP afterMOVEMLINK A6,#-4MOVEM.L D2-D3/A2-A3,-(SP)MOVE.L 8(A6),D2MOVE.L 12(A6),D3MOVE.W D3,-2(A6)MOVE.W D2,-4(A6)…MOVEM.L (SP)+,D2-D5/A2-A5UNLK A6RTSCEG 320/520 8: Stack frames and LINK 13What if the subroutine calls itself??P2P2RTNARTNAOld A6 Old A6P1P1X(V1)X(V2)D2D2D3A2A3A3D3A2Current SPP1P2RTNARTNAP1P2SP afterBSROld A6 Old A6D2D2D3xxD3A3A3A2A2SP afterLINKTemp SP,then A6A6SP afterMOVEMCEG 320/520 8: Stack frames and LINK 14ExampleMAIN MOVE.W #$12,-(SP)MOVE.W #$20,-(SP)BSR MYSUBADDA.L #4,SP…MYSUB LINK A6,#-8MOVEM.L D2-D3,-(SP);DRAW THE STACK…MOVEM.L +(SP),D2-D3UNLK A6;IS SP pointing at the return address?RTSCEG 320/520 8: Stack frames and LINK 15Example Stack?RTNARTNAFFFFCCCCxx1220xxD2D2A6CCCCFFFFA6:00006FF4A6:$7000$6FFE$6FFC$6FFA$6FF6$6FF8$6FF2$6FF4$6FEE$6FF0$6FEA$6FEC$6FE8MAIN MOVE.W #$12,-(SP)MOVE.W #$20,-(SP)BSR MYSUBADDA.L #4,SP…MYSUB LINK A6,#-8MOVEM.L D2-D3,-(SP)…MOVEM.L +(SP),D2-D3UNLK A6RTSCEG 320/520 8: Stack frames and LINK 16LINK An,#0 – Why would I do that??MAIN MOVE.W #$12,-(SP)MOVE.W #$20,-(SP)BSR MYSUBADDA.L #4,SP…MYSUB LINK A6,#0MOVEM.L D2-D7/A2-A6,-(SP)… ; Params are easy to find!MOVEM.L +(SP), D2-D7/A2-A6UNLK A6RTS?RTNARTNAOld A6Old A6D2 D21220D3D3D4D4Old SPA6CEG 320/520 8: Stack frames and LINK 17The stack and the heap•C and C++ allocate all local variables in a stack frame, using registers only temporarily.•Malloc’d memory comes from a different place: the heap.int main() {int a, b;a = 3;b = 7;}MAIN LINK A6,#-8MOVE.L #3,-4(A6)MOVE.L #7,-8(A6)…A Recursive Subroutinemain MOVE.W NUM,-(SP) ; Push NUM by valueJSR fact ; Puts result in D0MOVE.W #228,D7 ; Magic exit codeTRAP #14fact LINK A6,#0 ; Prepare for recursionMOVE.L D1,-(SP) ; Save D1 to the stackMOVE.W 8(A6),D0 ; D0 = numCMP.W #1,D0 ; if (num <= 1)BLE done ; return(num)MOVE.W D0,D1 ; else { D1 = num;SUBQ.W #1,D0 ;MOVE.W D0,-(SP) ; D0 = fact(num-1);JSR factADDA.L #2,SP ; clean the stackMULU.W D1,D0 ; D0 *= num;done MOVE.L (SP)+,D1 ; restore D1UNLK A6 ; clean upRTS ; Return the result in D0NUM DC.W 7 ; # to factorializeendCEG 320/520 8: Stack frames and LINK 19How C uses LINK…_sub1 LINK A6,#-28…MOVE.L #7,-4(A6)MOVE.L -12(A6),8(A6)MOVE.L 8(A6),D0ADDI.L #15,D0MOVE.L D0,8(A6)…MOVE.L #32, D0UNLK A6RTSint sub1(int a, int b) {int var1, var2, var3;int


View Full Document

Wright CEG 320 - Stack Frames- Using LINK

Download Stack Frames- Using LINK
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Stack Frames- Using LINK and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Stack Frames- Using LINK 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?