functions / subroutines & multiple call levels



High Level Assembler(HLASM) for MVS & VM & VSE

functions / subroutines & multiple call levels

Postby flok » Thu Apr 13, 2017 2:43 pm

Hi,

I'm trying to create subroutines in hlasm.
I can do BAL R14,myroutine and then BR14 at the end of the function but that works only one level.
I searched for a stack push/pop hlasm instruction but couldn't find any.
So: how do I create subroutines that call subroutines that call subroutines [...] in hlasm?


regards
flok
 
Posts: 8
Joined: Mon May 09, 2016 2:25 pm
Location: Gouda, the Netherlands
Has thanked: 4 times
Been thanked: 0 time

Re: functions / subroutines & multiple call levels

 

Re: functions / subroutines & multiple call levels

Postby enrico-sorichetti » Thu Apr 13, 2017 3:05 pm

You have to establish the proper register save/restore chain

see here for tutorials on different topics

http://www.simotime.com/sim4home.htm
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2636
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: functions / subroutines & multiple call levels

Postby flok » Thu Apr 13, 2017 3:14 pm

enrico-sorichetti wrote:You have to establish the proper register save/restore chain

see here for tutorials on different topics

http://www.simotime.com/sim4home.htm


Yes, that's what I figured out ;-)
But how do I do that in HLASM?

In z80 and x86 asm it is just call / ret.

In the link you gave the closest to calling a routine is http://www.simotime.com/asmasm01.htm but that's for an external program. I would like to (recursively) call a local function.
flok
 
Posts: 8
Joined: Mon May 09, 2016 2:25 pm
Location: Gouda, the Netherlands
Has thanked: 4 times
Been thanked: 0 time

Re: functions / subroutines & multiple call levels

Postby enrico-sorichetti » Thu Apr 13, 2017 3:32 pm

the tutorials I pointed You to have the code to save and restore the registers

I would like to (recursively) call a local function.


then You would have to use a dynamically acquired save area and working storage.
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2636
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 130 times

Re: functions / subroutines & multiple call levels

Postby steve-myers » Thu Apr 13, 2017 4:26 pm

What I do is something like this -
MAIN     CSECT
         USING *,12
         LA    15,SAVEAREA
         ST    13,4(,15)
         ST    15,8(,13)
         LR    13,15
         BAS   14,SUBR1
         BAS   14,SUBR2
         L     13,4(,13)
         SR    15,15
         IC    15,RC
         RETURN (14,12)
         EJECT
SUBR1    BASR  15,0
         SAVE  (14,3),,SUBR1
         LA    15,72(,13)
         ST    13,4(,15)
         ST    15,8(,13)
         LR    13,15
         MVI   RC,16
         L     13,4(,13)
         RETURN (14,3)
         EJECT
SUBR2    BASR  15,0
         SAVE  (14,5),,SUBR2
         LA    15,72(,13)
         ST    13,4(,15)
         ST    15,8(,13)
         LR    13,15
         BAS   14,SUBR1
         L     13,4(,13)
         RETURN (14,5)
SAVEAREA DC    (5*18)F'0'

The BASR 15,0 before the SAVE macro for the function is required because a SAVE macro that specifies an identifier requires that register 15 has the address of the macro. Yes, I know there are “tricks” that bypass the requirement but I seldom bother. Some programmers that use this method apply careful methods to avoid overflowing the save area stack, but since I started using it I have never once overflowed the save area stack, though I think I've gone 4 levels down from time to time.

In the SAVE macro you specify registers to save. You only need to save and restore the registers the actual routine itself modifies. Any subroutine you call is responsible for the registers it uses. The STM and LM instructions in the SAVE and RETURN macros are the slowest part of this scheme; any register you do not have to save improves the performance. I have a function I regularly use that just saves register 14, calls an external function and then calls an internal function. The external function uses every register, the internal function uses several registers, but by rigorously following the SAVE and RETURN convention everyone is happy. You will find it's rarely necessary to save and restore registers 15, 0 and 1; most programmers assume they are trashed by the call. My example does not use recursion, though in the 1970s and 1980s I used essentially this scheme to implement a function stack that required recursion.

As you have noted, there are no PUSH and POP instructions. However, there is the BAKR instruction and the PC and PR instructions, which evidently you missed. I do not use them; they are extremely slow. The engineers that defined the System/360 architecture were very aware of architectures such as the B5500 that implemented the stack concept but had essentially no registers and thought using real registers rather than s-l-o-w storage for pseudo registers in a stack would yield better overall performance. The trade off is stacking concepts can mean faster context switching, say for subroutine calls or interrupts than with System/360 type architectures. In the 1980s I inherited a program that implemented PUSH and POP by macros, and grew to thoroughly detest the whole scheme.

These users thanked the author steve-myers for the post:
flok (Thu Apr 13, 2017 6:14 pm)
steve-myers
Global moderator
 
Posts: 1877
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 194 times

Re: functions / subroutines & multiple call levels

Postby NicC » Fri Apr 14, 2017 2:53 am

Locked - being asked on another forum.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 2687
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisys (almost)
Has thanked: 4 times
Been thanked: 105 times


Return to Assembler

 


  • Related topics
    Replies
    Views
    Last post