Neon Enterprise Software Blog

Welcome to Neon Enterprise Software Blog Sign in | Join | Help
in Search

Kristine Neely's Newbie Blog

Structured Programming in Assembler????????!!!!!!!!!

So you’ve read about some key elements of Assembler, some new reasons to consider this language, new enhancements and some upgraded tools.  To continue on my mission to convince my readers of the power of Assembler, in this blog I will highlight some new instructions and introduce structured programming. 

If you haven’t coded in Assembler in a while, you can still probably remember the classic instructions: L, LA, ST, J, B, MVC…etc.  While these still play an important role in any Assembler program, there is a wealth of new instructions that have been added to make coding simpler and more efficient, and to take advantage of all the hardware improvements. 

  •  BAS/BASR: Branch and Save (Register)
    • Replaces BAL/BALR
    • Added to support 31-bit return addresses
  • AHI, CHI, LHI, MHI: Halfword Immediates
    • Work same as AH, CH, LH and MH, but a 16-bit binary value is used instead
    • Ex: CH      R1,=H'3'  -->  CHI      R1,3
  • MSR: Multiply Single
    • Works like most Multiply's, but uses only 2 registers
  • CUSE: Compare Until String Equal
    • Compares 2 strings, looking for an identical substring that exists in both
  • CLST: Compare Logical String
    • Compares 2 strings whose addresses are in the 2 registers
  • MVST: Move Logical String
    • Moves the string at the location in the 2nd register to the address specified in the 1st
  • SRST: Search String
    • Searches a string for a specific character
    • Simpler and faster than TRT or CLI loop
  • CVBY, CVBG, CVDY, CVDG: Convert Binary and Decimal Extended
    • Allows for extended displacement
    • CVBG and CVDG use 64-bit register
  • CSP/CSPG: Compare and Swap Purge (Grande)
    • Same as CS, but a purging operation is performed if equal
  • IIHH, IIHL, IILH, IILL: Insert Immediate
    • Places halfword into specific register location
  • NIxx, OIxx, XIxx: Boolean Immediate
    • xx: HH, HL, LH or LL
    • Halfword Boolean operations
  • LPQ/STPQ: Load/Store Register Pair
    • Works with a pair of 64-bit registers and 16 bytes of storage
    • Can be used in place of two LGs/STGs
  • TMH/TML: Test Under Mask High/Low
    • Test the high/low order bytes of the register with a 16-bit operand
  • TRE: Translate Extended
    • Super translate instruction - no 256 byte limit!
  • TRTR: Translate and Test in Reverse
    • Similar to TRT, but proceeds right to left
  • FLOGR: Find Leftmost One Bit Grande Register
    • No, I did not make this one up!
    • Scans 64-bit register left to right to find 1st one bit
  • SLLG, SRLG, SLAG, SRAG: Shift Grande
    • 64-bit register shifting
    • Right, left, logical, arithmetic
  • MANY OTHERS!

These instructions are just a portion of the newer set that has popped up over the last few years.  A fun site to check out the many instructions now available is Systems z Instructions Mnemonic List. 

 

Now for some real fun in Assembler: structured programming!  Yes, it is possible in this language!  It is easy to give up your spaghetti code habits with the powerful macro facility of HLASM and its Structured Programming Macros (SPMs).  With structured programming:

  • You can create clean, readable and understandable programs
  • Program flow is easily understood just by looking at the code
  • HLL-like code is possible…just without the unwanted overhead
  • Branching is “hidden”
  • Code is easier to maintain…and pass on to the next generation of programmers J

Here are some of the key components:

  1. IF

·         IF  (condition)

Code

ELSEIF  (condition)              ß Optional

      Code

ELSE                                  ß Optional

      Code

ENDIF

·         Ex’s:           IF  (LTR,R15,R15,Z)                   If clean result

IF  (ICM,R14,15,FIELD1,NZ)        If FIELD1 has a value

IF  (CLI,0(R15),EQ,0),AND,         If byte is available

        (TM,FLAG1,FLAG1_ON,O)       And our flag is on

  1. DO Loops

·         DO, DO FROM/TO/BY, DO INF, DO WHILE/UNTIL

·         Can loop a certain number of times, while/until a condition is met, or infinitely

·         Can use DOEXIT to opt out of a loop

·         Can nest DO loops and Ifs

·         Ex’s:           DO  WHILE=(LTR,R1,R1,NZ)      While R1 has a value

DO  FROM=(R1)            From value in R1 (loops n times as specified in R1, decrementing R1 as it goes)   

DO   UNTIL=(CLI,0(R1),EQ,C’ ‘)               Until space is found

  1. SELECT

·         SELECT

WHEN (condition)

WHEN (condition)          ß Optional

OTHRWISE                   ß Optional

                  ENDSEL

·         Condition is an “IF-like” expression

·         …You only have to spell OTHRWISE correctly one time before you remember that it’s spelled incorrectly!

·         Ex’s:           SELECT  TM,FLAG1,O              Select based on data type

WHEN HEX                   Hex data

            BAS     R14,RTNHEX

WHEN CHAR                Character data

            BAS     R14,RTNCHAR

WHEN NUM                  Numeric data

            BAS     R14,RTNNUM

OTHRWISE                   All other data

            BAS     R14,RTNOTHER

                                    ENDSEL

  1. CASENTRY

·         CASENTRY Rx,VECTOR=listtype,POWER=n

CASE a,c,d

CASE b,c

CASE f

                    ENDCASE

·         Not used often, but works well as a branch table after returning from a lower-level module

·         POWER=n (optional)

a.      Case numbers are multiples of that power of 2

b.      POWER=3 means case numbers are multiples of 8

·         VECTOR=B/BR

a.      Specifies that a branch vector is to be generated instead of an address vector (which means fewer instructions)

·         Ex’s:           CASENTRY R14,VECTOR=B,POWER=2            Act on Return Code

CASE 4                                    Macro error

            BAS     R14,MACROERROR

CASE 8                                    Invalid value error

            BAS     R14,INVALERR

CASE 12                       Severe error

            BAS     R14,ABORTERR

                                    ENDCASE

  1. STRTSRCH

·         STRTSRCH (any DO operands)

EXITIF (condition)

ORELSE

ENDLOOP

                  ENDSRCH

·         Also not used too often, but it self documenting

·         Can be used to code more complex loops

·         Ex’s:           STRTSRCH UNTIL=(LTR,R2,R2,Z)           Search for space

LR        R3,R6                           Save previous offset

LR        R6,R2                           Save current offset

LH        R15,BLK_LEN(R2)         Get length

EXITIF   (C,R15,GE,MAXLEN)     Length ok?

            ST        R3,PREV_VAL  Save value – we’re done

ORELSE                                   Length too short

            LH        R2,BLK_OFFSET  Try next value

ENDLOOP                                R2 is zero

            LA        R15,8                Set return code

                                    ENDSRCH

 

These SPMs give you so much flexibility and muscle when coding in Assembler – they really are an indispensable part of this language now.  The potential to write efficient and resourceful programs has really increased with the introduction of structured programming.  SPMs may take some time getting used to, but trust me…once you start to develop an SPM mindset when coding, the power of Assembler will become much clearer to you! 

 

I hope by now I have doused any previous negative impressions you may have had about Assembler.  Yes, this is my favorite language, and yes, I think it’s cool.  But I do recognize that many other languages play a key role in supporting the mainframe.  My goal in these last few blogs was simply to show another side of what I have heard been called an ugly and outdated language.  Assembler may be legacy code, but look how it has evolved to become a language to contend with in 2007! 

 

Thanks for reading, and until next time,

~Kristine~

 

 

Published Thursday, January 18, 2007 11:13 AM by kharper

Comments

 

robscott said:

Kristine,

Great blog - and there was me thinking that at 40 I was still in nappies as far as software development was concerned!

If you like the HLASM SPMs - you just *MUST* get hold of Ed Jaffe's FLOWASM exit.

It allows you to relax the continuation syntax rules in assembler so that you do *not* have to worry about columns 16 and 72 anymore. Continuation is indicated by just using a comma and then you can start the continued line anywhere.

All this means is that your code can indent nicely as you nest down and your macro calls can align with the rest of the code.

He also includes a rather nice register stack macro as well.

FLOWASM can be gotten from :

ftp://ftp.phoenixsoftware.com/pub/demo/asmmods.xmi

Rob Scott

Rocket Software

January 26, 2007 6:02 AM
 

dsh33782 said:

Kristine

Cool blog!  If your readers are intereseted in actually trying out assembler, they can now download the z390 portable mainframe assembler frow www.z390.org for both Windows and Linux.  It includes a mainframe compatible macro assembler, linker, and emulator plus lots of demos and regression tests.  z390 is written entirely in J2SE Java and is distributed with open source under GPL lincense.

z390 includes features such as structured programming macros, trace, and interactive debug, plus all the new z/Acrchitecture instructions including Decimal Floating Point as well as hex and binary floating point.  A recent addition includes a Service Oriented Architecture (SOA) Application Generator to enable creation of SOA type client server applications running on multiple processors on a TCP/IP network.

Don Higgins

don@higgins.net

January 27, 2007 6:42 AM
Anonymous comments are disabled

About kharper

Kristine H. Neely is a developer for NEON Enterprise Software. Kristine graduated from the University of Arizona in 2005, but prior to graduation, completed internships with NEON Enterprise Software in the summers from 2001-2005. Kristine works as an assembler programmer in the area of research & development, on IMS projects. Specifically, she has worked on NEON’s D2 and Mission Control products. As a young female, Kristine is somewhat of an anomaly in the mainframe industry. That drove her to become involved with zNextGen, a user-driven community that aims to connect peers and facilitate shared experiences among emerging mainframe professionals. Kristine is currently a project manager for zNextGen, and is a frequent presenter at SHARE. She has also been included in media coverage with the following outlets: Search CIO Tech Target, KUHF Radio, eWeek, Reuters, and more. Visit her blog to read about the trends in her world: http://www.neonesoft.com/blog/blogs/kharper/default.aspx
Powered by Community Server, by Telligent Systems