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:
- 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
- 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
- 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
- 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
- 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~