Mar 21, 2010

Assembler - Basics

1. What is base register in assembler program?

When a program is loaded into memory, assembler stores the address of the starting point of the program into base register. The assembler doesn’t produce direct addresses but produces an address in the base displacement format. Addresses in this form consist of 4 hexadecimal digits or two bytes, BDDD, where B represents a base register and DDD represents a displacement.
Thus smallest displacement from base register that can be calculated is x’000’ = 0 and the largest displacement is x’FFF’ = 4096 bytes.
If the program has size of more than 4096 bytes, then one base register is not sufficient. We may have to define one/two more base register depending upon the size of the program


2. Assembler is difficult language to code. Since COBOL compilers can covert COBOL code into machine language, what is need of assembler?
COBOL is high level language. Due to limitations of the compilers, the machine code generated by COBOL is not always efficient. A good assembler program can write more efficient code with assembler.
This where efficiency matters such as online programs where response time required is minimum, assembler is the preferred language.


3. What is difference between CSECT and DSECT?
A DSECT or Dummy Section provides the programmer with the ability to describe the layout of an area of storage without reserving virtual storage for the area that is described. The DSECT layout can then be used to reference any area of storage which is addressable by the program. Think of the DSECT as a template, or pattern, which can be “dropped” on any area of storage. Once the DSECT is “positioned”, the symbolic names in the DSECT can be used to extract data from the underlying storage area
By definition, a control section (CSECT) is “a block of coding that can be relocated (independent of other coding) without altering the operating logic of the program”.For the modern programmer, a CSECT might be considered as an independentcode module or package. The match in terminology is not precise, but suggestive.


Very large assembly language programs will require more than one control section.In any case, the programs we write will require only one CSECT, even assuming the addressing constraints imposed by the old System/370 architecture.
A program with a single CSECT may be started in one of two ways. For example,


the program LAB01 may be started in one of two ways.


LAB01 START
LAB01 CSECT


The assembler allows for the specification of a load address in the START or CSECT.
This is probably a bad idea; it may be ignored by a modern operating system.
The last line in the CSECT must be END Name, such as END LAB01.


4. What is the functionality of LTORG statement?
The assembler instruction that directs the location of the literal pool is LTORG. It does not have any operands. If you do not use any LTORG instructions in your program, the literal pool is placed at the end of the first control section (CSECT) of your program.


When the assembler encounters a LTORG instruction within a program, it creates a literal pool at that point, containing all the literals specified since the last LTORG instruction, or since the beginning of the program, if this is the first LTORG


To avoid addressability problems in multi modular programs, you should code a LTORG instruction as the last instruction of each control section in the program. So, each CSECT will have its own literal pool at the end of the module.
MOD1 CSECT
LTORG
MOD2 CSECT
LTORG
MOD3 CSECT
LTORG

5. How base register is defined in an assembler program?
Base registers is defined as below


BALR R12,R0
USING *, R12

These statements are coded immediately below CSECT.
BALR instruction is RR type of instruction, which loads the address of the next instruction into operand 1 i.e. R12. Second operand is specified as R0 so no branch takes place.

USING establishes addressability. The * is used to represent the current location counter in the assembler program. The USING instruction tells assembler that R12 is to be used as the base register and that R12 will contain the address of the current instruction.

Since BALR has loaded R12 with the address of the instruction following BALR, this correctly Establishes address in the program.


6. How multiple base registers are defined in an assembler program?
Multiple Base registers are defined as below


        BALR R12,R0
      USING *, R12,R10,R11
BASE  LM R10,R11,BASES
      B MAIN
BASES DC A(BASE+4096)
      DC A(BASE+8192)
MAIN  MVC IN,OUT


In above example three base registers are defined i.e. R12,R10,R11.
R12 is loaded with the address of the instruction following USING. R10 is loaded with the address of R12+4096 and R11 is loaed with address of R12+8192.

7. How standard entry statements are coded into assembler program?

MAIN CSECT                           SUB CSECT

     STM R14,R12,12(13)                  STM R14,R12,12(R13)
     BASR R12,R0                         BASR R12,R0
     USING *,R12                         USING *,R12
     ST R13,SAVE+4                       ST R13,SAVE+4
     LA R13,SAVE                         LA R13,SAVE
... ...
    LA R1,=A(X,Y,Z)                     
    L R15,=V(SUB) 


    BASR R14,R15 

In above scenario, MAIN is main program which calls subprogram SUB. Standard entry statements would be same in the both cases, since Main program also gets called by OS modules first.

Since we have very limited GPRS which should be shared between many programs, it is the programmers responsibilty to save the contenet of the register in the start of the program and restore the content of the registers back when exit.

To store the contents, SAVE is delcared as 18 fullword.First fullword is used by operating system. Second fullword stores address of the save area of the calling program.Third fullword contains address of save area of any called program from the current program.

Contents of registers 14,15,0,1,2,3,4,5,6,7,8,9,10,11,12 are stored in this fullword thereafter.

Register 13 contains address of the save area. Following is how standard entry statements are coded?
1. store the contents of the 14 to 12 into save area of the calling program at displacement of 12.( R13 points to save area of calling program)
2.Define base register and establish addressibility.
3.store the address of the save area of calling program (which is stored in R13) into SAVE+4. (SAVE is save area of the current program)
4. Then load address of the save area in the current program into R13.


8. How standard entry statements are coded into assembler program?
This is how standard exit statements are coded into any assembler program.

L R13,SAVE+4 
LM R14,R12,12(R13)
LA R15,0
BR 14                           

1. Address of the save area into calling program is again restored in R13. This address is saved in save area of the current program (save+4)
2. Contents of the registers 14 to 12 are restored back.
3. Return code is set to 0 by LA R15,0
4. And branched to the R14, which store the address of the insturction to be executed in the calling program.

                                                                                    BACK TO ASSEMBLER

6 comments:

  1. CSECT and DSECT needs to be defined properly , its not very descriptive and easy to understand.

    ReplyDelete
  2. From where i can get more FAQ's on Mainframe Assembler. Please help me on this,

    ReplyDelete
  3. Is there any difference between BASR and BALR ?

    ReplyDelete
  4. I wanted to know how many base register can be defined in a single assembler program without being dropped the base register

    ReplyDelete
    Replies
    1. I think that is solely depends upon the availability of register. You can use save area to save the content of the register. We can't use R0 as assembler think it as zero instead of R0. some registers may get corrupted when you are calling macros like R14, R15. So theoretically, we can use R1- R15. But practically, it all depends upon the program we are handling.

      This is purely based on my experience and I am not sure this is a perfect or not.

      Delete