Mainframes 360
The one stop destination for System Z professionals

Monday, November 1, 2010

Learning Assembler Language

Q. Why learn the Assembler Language?
Mainframe-Assembler is an old programming platform. Mainframe-Assembler is complex and geeky. Only the so-called Grumpy Old Dinosaur(GOD) programmers of the last century wrote programs and worked extensively on Assembler. Assembler programs are hard-to-maintain. Some of you might be convinced, that Mainframe-Assembler is near extinction. The big question then is, why code in Assembler?

Mainframe-Assembler is very close to Machine itself, and requires a sound understanding or S/370 (or any other) Mainframe-Architecture. Hence,
Mainframe-Assembler is still used to avail MVS or Operating System Facilities that are not available in traditional-COBOL. In most production-shops, you would find that special Abend-Routines, which are called just before a Program Terminates are written in Assembler. The next situation is when the Data which is stored or presented is in a format, not directly readable in COBOL. The third reason is the one, which is most often cited – Speed. Programs coded in Mainframe Assembler run faster or beat their COBOL or PL/I counter-parts. Most free IBM-Software tools that you know, and use everyday like IEBGENER and DFSORT are coded in Assembler. In fact many companies like TPF(Transaction Processing Facility) still use Assembler heavily to write code, and Assembler-programmer as a profession is alive and kicking. I want to learn Assembler, to improve my debugging-skills, and be able to read those cryptic Hex-dumps, and  add to my overall understanding of Mainframe Systems.
Q. What are the differences between COBOL and Assembler?
Assembler Language differs from all of the other languages such as COBOL, PL/I, RPG etc. It resembles the actual machine-instructions that you are likely to find on a Mainframe. Assembler-Language is a Low-level language. For performing a complex-task, you need to write several instructions. On the other hand, COBOL and other modern platforms such as Java or C# are High-Level Languages(HLL). In these languages, its possible to represent a complex task by one single-instruction.

COBOL looks more English-like. Its quite verbose. Writing a program in Assembler requires a different line-of-thinking, different thought pattern. Unlike COBOL, Assembler-Language statements don’t terminate with a period. Assembler language commands don’t even come close to English.

Alright, now let's keep this between you, me and the gatekeeper – COBOL Programs are actually transformed to Machine code. In fact, you can see the equivalent Machine-code corresponding to your COBOL-Program, for yourself when you run the Compiler IGYCRCTL with the PARM='LIST' Option. You would find that 1 COBOL Instruction generates several Machine-Instructions. By contrast, 1 Assembler-Instruction generates only one single Machine-Instruction. Guess what, you need to code several lines-of-Assembler to achieve the same effect as one line-of-COBOL.

Another paradigm-shift of thinking is needed, with the order of operands. COBOL does a MOVE A TO B, or ADD 1 TO COUNTER, take the first thing and do something to the second. In Assembler, you’d have to code MVC B,A or AP COUNTER,=P'1', the second operand normally does something on the first.

COBOL Programs are broken down into divisions, sections and para's – its strictly organized and structured. In Assembler, you just start coding. You decide your own custom-organization and enforce it, follow it throughout the program.
Q. How do I setup the Assemble-Link Job(JCL)?
Before you start writing Programs, you need to set up an Assemble-Link job(JCL), that'll help assemble your program into a Load Module. Once you have a Load-Module ready, you can run it and see the magic.

Most installations have ready-to-use PROCS such as ASMHC to assemble a Program, ASMHCL to assemble-and-link a Program and ASMHCLG to Assemble-Link-Go(Run) the program. If these aren’t found at your shop, try looking for ASMAC, ASMACL and ASMACLG. Better even ask your System Programmer for a JCL. The Assembler-software on Mainframes is the Program ASMA90. This is how I have setup my Assemble-link Job. I am going to use the ready-made PROC ASMACL.  There are two important customizations or overrides you need to do. ASMACL expects you to supply two Files – //SYSIN, the Input Source Program, and //SYSLMOD, the output Load Module. I have stored my Source-Program in the Member ASPROGR01 under the Library SYSADM.DEMO.SRCLIB. I hooked up and attached SYSIN inlet to this file SYSADM.DEMO.SRCLIB(ASPROG01). The Assembler internally passes its output to the Linker. The Linker-Software HEWL, produces an Output Load Module and stores it in the SYSLMOD File. I would like the Executable Load Module ASPROG01 to be created under SYSADM.DEMO.LOADLIB. Hence, SYSLMOD output is hooked to the File SYSADM.DEMO.LOADLIB(ASPROG01).

Image417

Here’s your first Assembler Program ASPROG01. I wrote it in the File SYSADM.DEMO.SRCLIB(ASPROG01). In fact, the program really doesn't do anything, it’s the same as a famous IBM Software IEFBR4(Null Program). All the program does is that it stores the COND-CODE 0 in the RETURN-CODE Variable(represented) by Register 15. It then returns the control to MVS. Key in this program, just as it is, without any typo's.

Image418 

Assemble-Link the above Program. If you get condition codes of zero for the Assemble and Link Steps, you are all set to go. Here is my Assemble-Listing. The Assemble Step and Link Step set Condition-Codes 00, highlighted below.

Image419 
Run the Program ASPROG01, by writing a job with EXEC PGM=ASPROG01. This is the job, I wrote to run my Assembler Program.

Image420
Q. I want to write a bigger program that actually does something rather just setting some condition code!
Key in the below Program in the ISPF Editor under a new Member ASPROG02, under your Source Library. This Program reads the Data-Input from INFILE, and prints it to OUTFILE. Type the Initialization-Part First. This is like the IDENTIFICATION DIVISION of a COBOL Program.

Image421

Now, type the AROUND section, which stores address in the SAVEAREA and opens the INFILE in INPUT Mode, and OUTFILE in OUTPUT Mode. It also PUTs the Header-Line to the Output-File.

Image422

Next, is the MAINLINE PROCESSING part of the program Lines 27-33, which actually GETs one record from the Input-File, increments the Record-Count and PUTs the record to the Output-File. What do the Lines 34-39 mean? If the Line-Count is less-than 50, you repeat the Mainline-Processing. Else, it's the end-of-the-page, so PUT a new Header to the Output-File, reset the Line-Count and continue with MAINLINE PROCESSING. This process of GET'ting a record from the Input-File, adding one to record-count and PUT'ing it to Output-File is repeated over and over again, till you reach the EODAD(End-of-Data).

When that happens, the End-of-file processing is run.

Image423

The Data-Areas or Variable Declarations in this Program are given below.

Image424

Last, I have coded the Dataset Control Blocks for the Input-File and Output-File. They correspond to the ENVIRONMENT DIVISION of a COBOL Program.

Image425

Assemble-Link your Program. If you have not committed any typo-errors, you should get condition-codes 00. This Program expects 2 Files – INFILE and OUTFILE. Here’s how I am running the Program. The INFILE contains 3 Data-Records. The OUTFILE is pointing to the Spool.

Image426

This is the Output, that you would see in Spool.

Image427

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.