Reentrant Code
Total Page:16
File Type:pdf, Size:1020Kb
Reentrant Code a reentrant procedure can have several calls open to it at the same time in an interrupt environment, different ISRs may call the same routine in a multitasking or multiuser or multithreaded environment, a single copy of a subroutine may be shared by more than one task/user/thread Reentrant Task subroutine A Task B Reentrant Code Characteristics requirements: 1. any parameters or local variables must be on the stack frame 2. registers may be used for parameters and local variables but must be saved on or restored from the stack at beginning/end of the subroutine 3. guarantee that each call of the subroutine will create a separate stack frame to maintain data integrity 4. no self-modifying code Recursion recursive code must be reentrant recursive reentrant but reentrant recursive e.g. calculating 3! using recursion main move.w number,D0 pass the number jsr fact start recursive rtn move.w D0,result store the factorial stop #$2700 number dc.w 3 result ds.w 1 fact link A6,#-2 Stack structure move.w D0,-2(A6) subq.w #1,D0 bne push move.w -2(A6),D0 bra return push jsr fact mulu -2(A6),D0 return unlk A6 rts end main SP ->/////// A much more complex example: X: array [0..30] of words Y: longword Z, ALPHA, N: byte call order is S/R demo(X, Y, Z, ALPHA, N ) N is passed/returned by value, all others are passed by reference demo(X[w:31], Y[l], Z[b], ALPHA[b], N[b]) main CLR.W D2 zero entire word MOVE.B N,D2 N to low byte MOVE.W D2,-(SP) N on stack PEA ALPHA push arguments PEA Z in reverse order PEA Y PEA X JSR demo call the routine MOVE.B ___,___ get returned value ADDA.L #__,SP fix up stack … STOP #$2700 demo(X[w:31], Y[l], Z[b], ALPHA[b], N[b]) => demo local variables: a[l], b[w], c[b] demo LINK A6,#-8 MOVEM.L D1/A1-A2,-(SP) … CLR.W D0 MOVE.B __(A6),D0 ;get N MOVE.W D0,-(SP) ; and pass it PEA __(A6) ;pass c addr MOVE.L __(A6),-(SP) ;pass Z addr PEA __(A6) ;pass a addr MOVE.L __(A6),-(SP) ;pass X addr JSR demo ;call demo(X,a,Z,c,N) ADDA.L #__,SP ;fix stack MOVE.B ;get N … demo(X[w:31], Y[l], Z[b], ALPHA[b], N[b]) => demo local variables: a[l], b[w], c[b] * how to access some of the variables … MOVEA.L __(__),A0 1st array element MOVE.W ___,__ of X into “b” * Y into a MOVEA.L __(__),A1 MOVE.L ___,__ * access Z MOVEA.L __(__),A2 MOVE.B ___,D1 * change ALPHA MOVEA.L __(__),A3 CLR.B ___ Reading: Introduction to Reentrancy [Jack Ganssle, Embedded Systems Design (03/15/01, 01:03:35 PM EST)] -- excellent article that illustrates potential issues with high level languages and the importance of knowing how your high level language will be translated into assembly language Reentrancy [2008 The Ganssle Group] -- excellent article with different examples and discussion of potential issues Reentrancy in Protocol Stacks [T. Sridhar, Embedded Systems Design (11/01/01, 09:42:22 AM EST)] -- very well written but much more advanced discussion due to the networking example used, read only if interested essentially any book on programming for concurrent, parallel, or embedded systems will have a discussion on reentrant code Expectations: you should be able to identify reentrant code you should be able to write reentrant code you should be able to write recursive code .