Docsity

Recursive Subroutines with Stack Handling Macros: A Factorial Example - Prof. Edward Boswo, Study notes of Programming Languages

Toggle sidebar
bg1
Writing Recursive Subroutines
We note immediately that the IBM 370 does not directly support recursion.
The purpose of this lecture is to use the stack handling macros discussed
in a previous lecture to implement simple recursive subroutines.
Recursion is a process that requires a stack, provided either explicitly
or by the RTS (Run Time System).
Without native support for recursion, we must directly manage the call stack.
The simple protocol has two steps.
Subroutine Call: Push the return address onto the stack
Branch to the subroutine
Subroutine Return Pop the return address into a register
Return to that address.
Other protocols provide for using the stack to transmit variables.
We shall discuss those later in this lecture.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Recursive Subroutines with Stack Handling Macros: A Factorial Example - Prof. Edward Boswo and more Study notes Programming Languages in PDF only on Docsity!

Writing Recursive Subroutines

We note immediately that the IBM 370 does not directly support recursion. The purpose of this lecture is to use the stack handling macros discussed in a previous lecture to implement simple recursive subroutines. Recursion is a process that requires a stack, provided either explicitly or by the RTS (Run Time System). Without native support for recursion, we must directly manage the call stack. The simple protocol has two steps. Subroutine Call: Push the return address onto the stack Branch to the subroutine Subroutine Return Pop the return address into a register Return to that address. Other protocols provide for using the stack to transmit variables. We shall discuss those later in this lecture.

NUMOUT: The Old Version

Here is the original code for the packed decimal version of NUMOUT. NUMOUT CVD R4,PACKOUT CONVERT TO PACKED UNPK THENUM,PACKOUT PRODUCE FORMATTED NUMBER MVZ THENUM+7(1),=X’F0’ CONVERT SIGN HALF-BYTE BR 8 RETURN ADDRESS IN R

This is the calling sequence for NUMOUT, placed within its context. MVC PRINT,BLANKS CLEAR THE OUTPUT BUFFER BAL 8,NUMOUT PRODUCE THE FORMATTED SUM MVC DATAPR,THENUM AND COPY TO THE PRINT AREA Note that the BAL instruction saves the address of the next instruction into R before the branch is taken. The saved return address is then used by the BR 8 instruction to return from the subroutine.

Factorial: A Recursive Function

One of the standard examples of recursion is the factorial function. We shall give its standard recursive definition and then show some typical code. Definition : If N  1, then N! = 1 Otherwise N! = N(N – 1)! Here is a typical programming language definition of the factorial function. Integer Function FACT(N : Integer) If N  1 Then Return 1 Else Return N*FACT(N – 1) Such a function is easily implemented in a compiled high–level language (such as C++ or Java) that provides a RTS (Run Time System) with native support of a stack. As we shall see, a low–level language, such as IBM 370 assembler, must be provided with explicit stack handling routines if recursion is to be implemented.

Tail Recursion and Clever Compilers

Compilers for high–level languages can generally process a construct that is “tail recursive” , in which the recursive call is the last executable statement. Consider the above code for the factorial function. Integer Function FACT(N : Integer) If N  1 Then Return 1 Else Return NFACT(N – 1) Note that the recursive call is the last statement executed when N > 1. A good compiler will turn the code into the following, which is equivalent. Integer Function FACT(N : Integer) Integer F = 1 ; Declare a variable and initialize it For (K = 2, K++, K <= N) Do F = FK ; Return F ; This iterative code consumes fewer RTS resources and executes much faster. NOTE: For fullword (32–bit integer) arithmetic, the biggest we can calculate is 12!

Confusion with the Return Addresses

Suppose that FACTOR is called with N = 1. The following code executes. L 4,=F’1’ BAL 8,FACTOR The first call to FACTOR N1 ST 4,THERESULT If (R4  1) then the function code is invoked. This is all that happens. L R4, = F’1’ SET R4 EQUAL TO 1 A1 BR 8 RETURN TO CALLING ROUTINE But note at this point, register 8 contains the address N1. Return is normal.

Confusion with the Return Addresses (Page 2)

Suppose now that FACTOR is called with N = 2. L 4,=F’2’ BAL 8,FACTOR PLACE A(N1) INTO R N1 ST 4,THERESULT This code is executed. LR 7,4 COPY R4 INTO R S 4,=F’1’ BAL 8,FACTOR PLACE A(N2) INTO R N2 MR 6,4 MULTIPLY A2 BR 8 The above call causes the following code to execute, as N = 1 now. L R4, = F’1’ SET R4 EQUAL TO 1 A1 BR 8 RETURN TO CALLING ROUTINE Here is the trouble. For N = 1, the return is OK. Back at the invocation for N = 2. Compute 21 = 2. Try to return to N1. But R8 contains the address N2. The code is “trapped within FACTOR”. It can never return to the main program.

Proof of Principle: Code Fragment 1

Here is the complete code for the first proof of principle. The calling code is as follows. The function is now called DOFACT. LA R8,A94PUTIT ADDRESS OF STATEMENT AFTER CALL STKPUSH R8,R PUSH THE ADDRESS ONTO THE STACK STKPUSH R4,R PUSH THE ARGUMENT ONTO THE STACK B DOFACT CALL THE SUBROUTINE A94PUTIT BAL 8,NUMOUT FINALLY, RETURN HERE. The code for this “do nothing” version of DOFACT is as follows. DOFACT STKPOP R4,R POP RESULT BACK INTO R STKPOP R8,R POP RETURN ADDRESS INTO R BR 8 BRANCH TO THE POPPED ADDRESS Remember : 1. STKPOP R4,R is a macro invocation.

  1. The macros have to be written with a symbolic parameter as the label of the first statement.

The Stack for Both Argument and Return Address

We now examine a slightly non–standard approach to using the stack to store both arguments to the function and the return address. In general, the stack can be used to store any number of arguments to a function or subroutine. We need only one argument, so that is all that we shall stack. Remember that a stack is a Last In / First Out data structure. It could also be called a First In / Last Out data structure; this is seldom done. Recall the basic structure of the function DOFACT DOFACT Use the argument from the stack STKPOP R8,R POP RETURN ADDRESS INTO R BR 8 BRANCH TO THE POPPED ADDRESS Since the return address is the last thing popped from the stack when the routine returns, it must be the first thing pushed onto the stack when the routine is being called.

When Do We Pop the Argument?

The position of the STKPOP depends on the use of the argument sent to the function. There are two considerations, both of which are quite valid. Assume that register R7 contains the argument. We shall get it there on the next slide. Consider the fragment of code corresponding to NFACT(N – 1). FDOIT S R7,=F'1' SUBTRACT 1 FOR NEW ARGUMENT LA 8,FRET GET THE ADDRESS OF RETURN STKPUSH R8,R STORE NEW RETURN ADDRESS STKPUSH R7,R NOW, PUSH NEW ARG ONTO STACK B DOFACT MAKE RECURSIVE CALL FRET L R2,=F'0' At this point, the return register (say R4) will contain FACT(N – 1). At this point, the value of N should be popped from the stack and multiplied by the result to get the result NFACT(N – 1), which will be placed into R4 as the return value.

When Do We Pop the Argument? (Part 2)

But recall that the basic structure of the factorial function calls for something like: STKPOP R7,R If the value in R7 is not greater than 1, execute this code. L R4,=F’1’ SET THE RETURN VALUE TO 1 STKPOP R8,R POP THE RETURN ADDRESS BR 8 RETURN TO THE CALLING ROUTINE. If the value in R7 is larger than 1, then execute this code. FDOIT S R7,=F'1' SUBTRACT 1 FOR NEW ARGUMENT LA 8,FRET GET THE ADDRESS OF RETURN STKPUSH R8,R STORE NEW RETURN ADDRESS STKPUSH R7,R NOW, PUSH NEW ARG ONTO STACK B DOFACT MAKE RECURSIVE CALL FRET L R2,=F'0' But, there is only one copy of the argument value. How can we pop it twice. Answer: We push it back onto the stack.

The Factorial Function DOFACT

DOFACT STKPOP R7,R GET THE ARGUMENT AND EXAMINE

STKPUSH R7,R BUT PUT IT BACK ONTO THE STACK

C R7,=F'1' IS THE ARGUMENT BIGGER THAN 1

BH FDOIT YES, WE HAVE A COMPUTATION

L R4,=F'1' NO, JUST RETURN THE VALUE 1

STKPOP R7,R ARG IS NOT USED, SO POP IT

B FDONE AND RETURN

FDOIT S R7,=F'1' SUBTRACT 1 FOR NEW ARGUMENT

LA 8,FRET GET THE ADDRESS OF RETURN

STKPUSH R8,R STORE NEW RETURN ADDRESS

STKPUSH R7,R NOW, PUSH NEW ARG ONTO STACK

B DOFACT MAKE RECURSIVE CALL

FRET STKPOP R7,R POP THIS ARGUMENT FROM STACK

MR 6,4 PUT R4*R7 INTO (R6,R7)

LR 4,7 COPY PRODUCT INTO R

FDONE STKPOP R8,R POP RETURN ADDRESS FROM STACK

BR 8 BRANCH TO THAT ADDRESS

Analysis of DOFACT (Part 1)

Let’s start with the code at the end. At this point, the register R4 contains the return value of the function, and the argument has been removed from the stack. FDONE STKPOP R8,R POP RETURN ADDRESS FROM STACK BR 8 BRANCH TO THAT ADDRESS The label FDONE is the common target address for the two cases discussed above. Again, it cannot be a macro invocation, so I place a dummy statement there. Again, here is the top–level structure.

  1. Get the argument value, N, from the stack.
  2. If ( N  1 ) then Set the return value to 1 B FDONE
  3. If ( N  2) then Handle the recursive call and return from that call.
  4. FDONE: Manage the return from the function

DOFACT Part 3: Handling the Case for N > 1

Here is the code for the case N > 1. FDOIT S R7,=F'1' SUBTRACT 1 FOR NEW ARGUMENT LA 8,FRET GET THE ADDRESS OF RETURN STKPUSH R8,R STORE NEW RETURN ADDRESS STKPUSH R7,R NOW, PUSH NEW ARG ONTO STACK B DOFACT MAKE RECURSIVE CALL FRET L R2,=F'0' NON-MACRO STATEMENT AS TARGET STKPOP R7,R POP THIS ARGUMENT FROM STACK *HERE

  • R7 CONTAINS THE VALUE N
  • R4 CONTAINS THE VALUE FACT(N – 1)

MR 6,4 PUT R4*R7 INTO (R6,R7) LR 4,7 COPY PRODUCT INTO R The code then falls through to the “finish up” code at FDONE. Note the structure of multiplication. Remember that an even–odd register pair, here (6, 7) is multiplied by another register.

Sample Run for DOFACT

We shall now monitor the state of the stack for a typical call to the recursive function DOFACT. Here is the basic structure for the problem. First we sketch the calling code. LA 8,A1 STATEMENT AFTER CALL TO SUBROUTINE STKPUSH R8,R PLACE RETURN ADDRESS ONTO STACK B DOFACT BRANCH DIRECTLY TO SUBROUTINE A1 The next instruction. Here is the structure of the recursive function DOFACT DOFACT Check value of argument Branch to FDONE if the argument < 2. Call DOFACT recursively FRET Return address for the recursive call FDONE Close–up code for the subroutine