Trouble with assembler subroutines



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

Trouble with assembler subroutines

Postby RISCCISCInstSet » Mon Nov 21, 2011 10:32 am

I've been having trouble with assembler subroutines - can you give me some help as to how they work?

Thanks
RISCCISCInstSet
User avatar
RISCCISCInstSet
 
Posts: 121
Joined: Mon Oct 17, 2011 1:46 pm
Has thanked: 146 times
Been thanked: 0 time

Re: Subroutines

Postby enrico-sorichetti » Mon Nov 21, 2011 12:41 pm

I posted this link already quite a few times
http://www.simotime.com/
the examples, COBOL,JCL,VSAM,ASSEMBLER are pretty clear and well documented/explained.

it Does not make any difference from a coding point of view if the Subroutine is <inline>( in the same source file) or <external> ( in a different source file )
only the compile link jcl will be different
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

These users thanked the author enrico-sorichetti for the post:
RISCCISCInstSet (Mon Nov 12, 2012 4:49 am)
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: Subroutines

Postby BillW » Mon Nov 21, 2011 6:39 pm

There are multiple reasons to use subroutines in your assembler program. After awhile of writing assembler, you will notice yourself repeating a section of logic, changing only the variables that require manipulation,inspection,etc. You may think that you are clever and actually use the editor and copy the section of code that performs the logic you intend, then manually go in and change the variable names to the new ones you are working on, yet not change what you are doing to them. A real life example you ask. Well, suppose you want to look at a variable and determine the first blank character position (address) and the length of the non-blank portion. You could code up a small section of logic to do this, looping through the variable, inspecting each character in turn (from left to right, as that is how we store character variables in English (not sure what one would do in those cultures that do not). You have just learned a reason to use a subroutine.

The subroutine allows the programmer to write a section of logic and use it multiple times to perform the same (similar) action on different variables. To the previous example. We could write a small subroutine that takes as parameters the character variable starting address and the length of the variable, performs the search for the first blank character in the character variable, then returns the address of the first blank character and the count of the number of characters in the character variable from the start to the blank character. An important item here is that usually, we pass to the subroutine the address of a variable that we wish to work with (although there are other ways and methods). By passing the address of the variable, and having the subroutine use this address, we can make the subroutine useful for differing variables and hence not repeat common logic in the program.

Now that is one reason to use subroutines and there are many nuances to using subroutines. One of the larger things to think about is how to pass the values the subroutine requires to do it's work (or function). For an internal subroutine, this is easier, as you have complete control of how it is written and what it does. This is not necessarily the case for those routines written by others and linked in to your program. You have to be very careful to match what the subroutine expects for parameters (and how they are to be passed). Also, most external routines require that you provide the standard save area so that they may preserve the contents of the registers and restore them to the original values when it returns to you (unless a register is supposed to pass a value back in a register, like a return code in R15.

So there you have a brief introduction into why you would use a subroutine (but not so much how, as there are multiple ways to do so).

Hope this helps you.

These users thanked the author BillW for the post:
RISCCISCInstSet (Mon Nov 12, 2012 4:49 am)
BillW
 
Posts: 20
Joined: Thu Nov 10, 2011 8:21 am
Has thanked: 0 time
Been thanked: 3 times

Re: Trouble with assembler subroutines

Postby steve-myers » Tue Nov 22, 2011 6:26 am

It would help us if you provided some idea of the type of trouble you are having. Both BillW and Enrico are helpful but very generic. When I first looked at this Monday I thought of a generic response, but different than Enrico's and BillW's posts, but I think we need something more specific to really assist you.

These users thanked the author steve-myers for the post:
RISCCISCInstSet (Mon Nov 12, 2012 4:49 am)
steve-myers
Global moderator
 
Posts: 2105
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 243 times

Re: Trouble with assembler subroutines

Postby RISCCISCInstSet » Tue Nov 22, 2011 7:27 am

What I need to understand is: how shall I start up a simple internal subroutine such that I can call it? To call the subroutine, I invoke BAL
R14,subroutine - with subroutine being a selected name. To return from a subroutine, I code BR R14.

According to the material, a program begins with
ONE CSECT
       SAVE      (14,12)
       BALR      12,0
       USING     *,12
       LA          2,SAVEAREA
       ST          2,8(,13)
       ST          13,4(,2)
       LR          13,2
       â€¦
SAVEAREA DS 18F


A New Instruction - BAL

— The Branch and Link instruc4on or the Branch and Link Register
instruction is used, normally, for subroutine calls
— Their formats are:
     BAL  R_1,D_2(X_2,B_2)
     BALR  R_1,R_2
— The address of the next instruc4on is stored in R_1  before branching


Save Areas
A 72-­‐byte area
− SAVEAREA + 0 = unused
− SAVEAREA + 4 = Previous save area
− SAVEAREA + 8 = Next save area − SAVEAREA+12 = Return Address − SAVEAREA + 16 = EPA
− SAVEAREA+20 = Register 0
− SAVEAREA+24=Register 1
− …
− SAVEAREA + 68 = Register 12


Leaving a Program
       L    13,4(,13) 
−  Unchain save areas 
−  Point register 13 to the previous save area 
       RETURN  (14,12) 
−  An IBM macro 
−  Restores registers 14 through 12 from the values in the save area 
addressed by register 13 
−  Branches to the address in register 14


SUMRTN DS 0H
             PACK WORK8,0(5,R1)
             AP TOTAL,WORK8
             BR R15
RISCCISCInstSet
User avatar
RISCCISCInstSet
 
Posts: 121
Joined: Mon Oct 17, 2011 1:46 pm
Has thanked: 146 times
Been thanked: 0 time

Re: Trouble with assembler subroutines

Postby steve-myers » Tue Nov 22, 2011 8:23 am

You didn't show us how you call SUMRTN, perhap like this -
         LA    R1,DATA
         BAL   R15,SUMRTN
         ...
DATA     DC    CL5'00800' or perhaps ZL5'800'

These users thanked the author steve-myers for the post:
RISCCISCInstSet (Mon Nov 12, 2012 4:49 am)
steve-myers
Global moderator
 
Posts: 2105
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 243 times

Re: Trouble with assembler subroutines

Postby RISCCISCInstSet » Tue Nov 22, 2011 8:34 am

Actually I presented a subroutine call (abridged from that) in the form of BAL R14,subroutine, and a new line cut it off. I'm copying and pasting from a pdf, and it's treating me weird.

Is there any other information you have to offer?
RISCCISCInstSet
User avatar
RISCCISCInstSet
 
Posts: 121
Joined: Mon Oct 17, 2011 1:46 pm
Has thanked: 146 times
Been thanked: 0 time

Re: Trouble with assembler subroutines

Postby RISCCISCInstSet » Tue Nov 22, 2011 10:04 am

Complete Information about subroutine calls:

— Because the next instruc4on address is place in R1 before 
branching, R1 can be used as a return address from a 
subroutine 
— To enter a subroutine, you would code 
     BAL  R14,subroutine 
— To return from a subroutine, you would code 
     BR    R14
RISCCISCInstSet
User avatar
RISCCISCInstSet
 
Posts: 121
Joined: Mon Oct 17, 2011 1:46 pm
Has thanked: 146 times
Been thanked: 0 time

Re: Trouble with assembler subroutines

Postby steve-myers » Tue Nov 22, 2011 11:39 am

RISCCISCInstSet wrote:Complete Information about subroutine calls:...

— Because the next instruc4on address is place in R1 before 
branching, R1 can be used as a return address from a 
subroutine 
— To enter a subroutine, you would code 
     BAL  R14,subroutine 
— To return from a subroutine, you would code 
     BR    R14
This is far from complete, and it is not correct.
  • In the first bullet replace "R1" with "the link register".
  • There are many ways to call a subroutine.
    • The BAL, BAS, or BRAS instructions as indicated in the second and third bullets. These instructions do not place the address of the subroutine into register 15
    • The CALL macro. CALL is not commonly used for an internal routine, but there is no reason why it cannot be used.
    • The LINK macro to call a program in another load module.
    • The ATTACH macro to call a program in another load module and create a subtask to run the module asynchronously from the task that performs the ATTACH.
    • The LOAD macro to load a subroutine in another load module followed by the CALL macro to call the subroutine and the DELETE macro to remove the load module from storage. This sequence may be preferable to the LINK macro if it is done many times and the subroutine is reusable or reenterable.

These users thanked the author steve-myers for the post:
RISCCISCInstSet (Mon Nov 12, 2012 4:49 am)
steve-myers
Global moderator
 
Posts: 2105
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 243 times

Re: Trouble with assembler subroutines

Postby RISCCISCInstSet » Tue Nov 22, 2011 4:35 pm

There are many ways to call a subroutine.
Sorry; I didn't mean to imply it was the only possible way to call a subroutine.
RISCCISCInstSet
User avatar
RISCCISCInstSet
 
Posts: 121
Joined: Mon Oct 17, 2011 1:46 pm
Has thanked: 146 times
Been thanked: 0 time

Next

Return to Assembler

 


  • Related topics
    Replies
    Views
    Last post