Reentrant Code n a reentrant procedure can have

  • Slides: 9
Download presentation
Reentrant Code n a reentrant procedure can have several calls open to it at

Reentrant Code n 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 ¨ Task A Task B Reentrant subroutine

Reentrant Code Characteristics requirements: 1. any parameters or local variables must be on the

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 n recursive code must be reentrant ¨ recursive reentrant but reentrant recursive e.

Recursion n recursive code must be reentrant ¨ recursive reentrant but reentrant recursive e. g. calculating 3! using recursion main move. w jsr move. w stop number dc. w result ds. w number, D 0 fact D 0, result #$2700 3 1 pass the number start recursive rtn store the factorial

fact push link move. w subq. w bne A 6, #-2 D 0, -2(A

fact push link move. w subq. w bne A 6, #-2 D 0, -2(A 6) #1, D 0 push move. w bra -2(A 6), D 0 return jsr fact mulu return unlk rts end Stack structure -2(A 6), D 0 A 6 main SP ->///////

A much more complex example: X: array [0. . 30] of words n Y:

A much more complex example: X: array [0. . 30] of words n Y: longword n Z, ALPHA, N: byte n call order is S/R demo(X, Y, Z, ALPHA, N ) n N is passed/returned by value, all others are passed by reference n

demo(X[w: 31], Y[l], Z[b], ALPHA[b], N[b]) main CLR. W MOVE. B MOVE. W PEA

demo(X[w: 31], Y[l], Z[b], ALPHA[b], N[b]) main CLR. W MOVE. B MOVE. W PEA PEA JSR MOVE. B ADDA. L … STOP D 2 N, D 2, -(SP) ALPHA Z Y X demo ___, ___ #__, SP #$2700 zero N to N on push in entire word low byte stack arguments reverse order call the routine get returned value fix up stack

demo(X[w: 31], Y[l], Z[b], ALPHA[b], N[b]) => demo local variables: a[l], b[w], c[b] demo

demo(X[w: 31], Y[l], Z[b], ALPHA[b], N[b]) => demo local variables: a[l], b[w], c[b] demo LINK MOVEM. L … CLR. W MOVE. B MOVE. W PEA MOVE. L JSR ADDA. L MOVE. B … A 6, #-8 D 1/A 1 -A 2, -(SP) D 0 __(A 6), D 0 ; get N D 0, -(SP) ; and pass it __(A 6) ; pass c addr __(A 6), -(SP) ; pass Z addr __(A 6) ; pass a addr __(A 6), -(SP) ; pass X addr demo ; call demo(X, a, Z, c, N) #__, SP ; fix stack ; get N

demo(X[w: 31], Y[l], Z[b], ALPHA[b], N[b]) => demo local variables: a[l], b[w], c[b] *

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 __(__), A 0 1 st array element MOVE. W ___, __ of X into “b” * Y into a MOVEA. L __(__), A 1 MOVE. L ___, __ * access Z MOVEA. L __(__), A 2 MOVE. B ___, D 1 * change ALPHA MOVEA. L __(__), A 3 CLR. B ___

Reading: n Introduction to Reentrancy [Jack Ganssle, Embedded Systems Design (03/15/01, 01: 03: 35

Reading: n 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 n Reentrancy [2008 The Ganssle Group] n Reentrancy in Protocol Stacks [T. Sridhar, Embedded Systems Design (11/01/01, n essentially any book on programming for concurrent, parallel, or embedded systems will have a discussion on reentrant code -- excellent article with different examples and discussion of potential issues 09: 42: 22 AM EST)] -- very well written but much more advanced discussion due to the networking example used, read only if interested Expectations: n n n you should be able to identify reentrant code you should be able to write recursive code