Jump to content

IBM Basic assembly language and successors: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Line 101: Line 101:
Most programs will require services from the [[operating system]], and the OS provides standard macros for requesting those services. These are analogous to [[Unix]] [[system call]]s. For instance, in [[MVS]] (later z/OS), STORAGE (with the OBTAIN parameter) dynamically allocates a block of memory, and GET retrieves the next logical record from a file.
Most programs will require services from the [[operating system]], and the OS provides standard macros for requesting those services. These are analogous to [[Unix]] [[system call]]s. For instance, in [[MVS]] (later z/OS), STORAGE (with the OBTAIN parameter) dynamically allocates a block of memory, and GET retrieves the next logical record from a file.


Unlike Unix system calls, macro's are not standardized across operating systems though. Even something as simple as writing a "sequential file" is coded differently e.g. in Z/OS than in Z/VSE.
Unlike Unix system calls, macros are not standardized across operating systems though. Even something as simple as writing a "sequential file" is coded differently e.g. in Z/OS than in Z/VSE.


==External links==
==External links==

Revision as of 22:58, 23 September 2010

BAL (Basic Assembly Language) is a low-level language used on IBM mainframes from the earliest 360 series, through systems 370, 390 and z/Series, as well as the Univac 90/60, 90/70 and 90/80 mainframes made by Sperry Corporation. The earliest version was provided with the System/360 in 1964; the latest version is known as the IBM High Level Assembler (HLASM). Programmers utilizing this family of assemblers refer to them as ALC, for Assembly Language Coding, or simply "assembler".

A note on the name

Properly speaking, "Basic Assembly Language" was the name of the extremely restricted dialect designed to be assembled on early System/360 machines with only 8KiB of main memory, and only a card reader, a card punch, and a printer for input/output: thus the word "Basic". However, the full name and the initialism "BAL" somehow almost immediately attached themselves in popular use to all assembly-language dialects on the System/360 and its descendants.

General characteristics

The architecture of IBM mainframes has taken many forms over the years, including System/360, System/370 XA, ESA/390, and z/Architecture. Each of these architectures has retained compatibility with most of the features of its predecessor. BAL uses the native instruction set of these machines. It is thus closer to the hardware than third-generation languages such as COBOL.

The instruction set consists of the low-level operations supported by the hardware, such as

  • "Load" (L) - copy a value from memory to a register,
  • "Store" (ST) - copy a value from a register to memory,
  • "Compare" (C) - compare a register value with a value in memory,
  • "Shift" (SLL, SRL) - move the bits of a register left or right and
  • "Start Sub Channel" (SSCH) - start a sub-channel operation such as an I/O operation using a connected string of Channel Command Words (CCW).

The extreme simplicity of these operations means that a program written in Assembler will usually be much longer and harder to read than an equivalent program in, say, COBOL or Fortran. In the past, the speed of hand-coded Assembler programs was often felt to make up for this drawback, but with the advent of optimizing compilers, C for the mainframe, and other advances, Assembler has lost much of its appeal. IBM continues to upgrade the Assembler, however, and it is still used when the need for speed or very fine control is paramount.

Another reason to use Assembler is that not all operating system functions can be accessed in high level languages, Basically, the Application Program Interface of mainframe operating systems is defined as a (large) set of Assembly Language "macro" instructions, that typically invoke Supervisor Call (SVC) hardware instructions, that in turn invoke operating system routines through the interrupt system. But it is possible to invoke Assembler subroutines from programs written in high-level languages.

A small example

The following fragment shows how the logic "If SEX = 'M', add 1 to MALES; else, add 1 to FEMALES" would be performed in Assembler.

         CLI   SEX,'M'        Male?
         BNE   IS_FEM         If not, branch around
         L     7,MALES        Load current value of MALES into register 7
         LA    7,1(,7)        add 1 (pre-XA max value 24 bits)
         ST    7,MALES        and store back the result
         B     GO_ON          Finished with this portion
IS_FEM   EQU   *              A label
         L     7,FEMALES      If not male, load current value in FEMALES 
         LA    7,1(,7)        add 1 (pre-XA max value 24 bits)
         ST    7,FEMALES      and store
GO_ON    EQU   *              - rest of program -
*
MALES    DC    F'0'            defines 31 bit memory location (initially=0)
FEMALES  DC    F'0'            ""     ""

The following is the ubiquitous Hello world program, and would, executing under an IBM operating system such as OS VS/1 or MVS, display the words 'Hello World' on the operator's console:

HELLO    CSECT               The name of this program is 'HELLO'
*                            Register 15 points here on entry from Op/sys or caller.
         USING *,12          Tell assembler what register we are using for pgm. base
         STM   14,12,12(13)  Save registers 14,15,and 0 thru 12 in callers Save area
         LR    12,15         Set up base register with programs entry point address       
         LA    15,SAVE       Now Point at our own save area
         ST    15,8(13)      Set forward chain
         ST    13,4(15)      Set back chain               
         LR    13,15         Now switch to new save area
*                            -end of housekeeping (similar for most programs) -
         WTO   'Hello World' Write To Operator  (Operating System macro)
*
         L     13,4(13)      point at caller's-provided save area                     
         LM    14,12,12(13)  Restore registers as on entry
         SR    15,15         Set register 15 to 0 so that the return code is Zero
         BR    14            Return to caller
*           
SAVE     DS    18A           Define 18 fullwords for calling
         END  HELLO          This is the end of the program           

Note: "WTO" is an Assembler macro that generates an Operating System call. Because of saving registers and later restoring and returning, this small program is usable as a batch program invoked directly by the operating system Job control language (JCL) like this

// EXEC PGM=HELLO

or, alternatively, it can be CALLed as a subroutine from such a program.

CALL HELLO

Types of instructions

Three main types of instructions are found in the source code of a program written in Assembler.

Machine instructions (Mnemonic)

As with any language, the heart of Assembler programming is understanding the instructions that usually have, in this case, a "one-to-one" relationship with machine instructions. The full mnemonic instruction set is described in the Principles of Operation manual for each processor. In most instructions, the target for an instruction appears first, then the source on the right (as with "a = 6" in C or Algol programming). After one or more blanks, any comment may follow on the same line. A line starting with an asterisk (*) is considered entirely comments. Examples:

* This is a comment line
* Load the fullword integer stored at location labeled 'ZIGGY' into general register 3:
      L    3,ZIGGY
      SLA  4,5             shift the value in general register 4 left by 5 bits
      MVC  TARGET,SOURCE   move characters from memory location 'SOURCE' to 'TARGET'
      AP   COUNT,=P'1'     add 1 to value in memory location 'COUNT' (packed decimal format)
      B    NEXT            unconditional branch to label 'NEXT'
HERE  EQU   *              This is a label
      CLC   TARGET,=C'ADDRESS'  Compare memory location 'TARGET' to string 'ADDRESS'
      BE    THERE               branch if equal to program label 'THERE'

Generally accepted standards, although by no means mandatory, include the identification of General Purpose Registers with the nemonic '"R" through EQU statements elsewhere in the program to ease the readability of Assember language programs. Thus typically you may see the following in an assembler program:-

      L    R3,ZIGGY

Assembler instructions

The assembler itself needs instructions from the programmer too. For instance, CSECT means "start a section of code here"; DC defines a constant to be placed in the object code.

Macros and conditional assembly

The programmer can group instructions together into macros and add them to a library, which can then be invoked in other programs, usually with parameters, like the preprocessor facilities in C and related languages. Macros can include conditional assembler instructions, such as AIF (an IF construct), used to generate different code according to the chosen parameters. That makes the macro facility of this Assembler very powerful. While multiline macro's in C are an exception, macro definitions in Assembler can easily be hundreds of lines.

Operating system macros

Most programs will require services from the operating system, and the OS provides standard macros for requesting those services. These are analogous to Unix system calls. For instance, in MVS (later z/OS), STORAGE (with the OBTAIN parameter) dynamically allocates a block of memory, and GET retrieves the next logical record from a file.

Unlike Unix system calls, macros are not standardized across operating systems though. Even something as simple as writing a "sequential file" is coded differently e.g. in Z/OS than in Z/VSE.