Jump to content

IBM Basic assembly language and successors: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Line 80: Line 80:
The programmer can group instructions together into [[Macro (computer science)|macros]], which can then be invoked in other programs, much like the preprocessor facilities in C and related languages. Macros can include conditional assembler instructions, such as AIF (an IF construct), which can be used to generate different code under different conditions.
The programmer can group instructions together into [[Macro (computer science)|macros]], which can then be invoked in other programs, much like the preprocessor facilities in C and related languages. Macros can include conditional assembler instructions, such as AIF (an IF construct), which can be used to generate different code under different conditions.


===Operating system macros===
====Operating system macros====
Most programs will require services from the [[operating system]], and the OS provides macros for requesting the services. These are analogous to Unix [[system call]]s. For instance, in MVS (later z/OS), GETMAIN allocates a block of memory, and GET gets the next logical record from a file.
Most programs will require services from the [[operating system]], and the OS provides macros for requesting the services. These are analogous to Unix [[system call]]s. For instance, in MVS (later z/OS), GETMAIN allocates a block of memory, and GET gets the next logical record from a file.



Revision as of 07:19, 22 February 2008

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. 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 Common, or simply "assembler".

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 as close to the hardware as a typical programmer is likely to get.

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
  • "Execute Channel Program" (EXCP) - perform an I/O operation such as reading data from a disk or tape (usually by the Operating System).

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.

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 systems such as OS VS/1 and 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,4(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    R13,SAVE      Now point to our own embedded save area  
*                            -end of housekeeping (similar for most programs) -
         WTO   'Hello World' Write To Operator  (Operating System macro)
*                           
         LM    14,12,4(13)   Restore registers as on entry
         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

Four main types of instructions are found in an Assembler program.

Machine instructions

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 instruction set is described in the Principles of Operation manual for each processor. The general syntax for the instruction operands is fairly straightforward

  • 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. A line starting with an asterisk (*) is considered entirely comments.

Examples:

* This is a comment line
     L    3,ZIGGY         load the fullword integer stored at location ZIGGY into general register 3
     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'
     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'

Assembler instructions

The assembler itself provides instructions too. For instance, CSECT means "start a section of code here"; DC declares a constant value to be placed in the object code.

Macros and conditional assembly

The programmer can group instructions together into macros, which can then be invoked in other programs, much like the preprocessor facilities in C and related languages. Macros can include conditional assembler instructions, such as AIF (an IF construct), which can be used to generate different code under different conditions.

Operating system macros

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