Home Back to 370 Assembler Tips & Tricks Index Links

Using Translate & Test (TRT) to Parse Parameter Lists

By Marc Reibstein, Systems Programmer

  • Over the years we've seen many methods to parse delimited parameter lists;
    e.g., (P1,P2(P21,P22),P3,P4(P41,P42)).

  • Most methods are incredibly ugly where the programmer's chose to use Compare Logical Immediate (CLI) loops or worse, Compare Logical Character (CLC) loops.

  • There really is only ONE way to correctly parse parameters from a parameter list.  That is by using Translate & Test (TRT) tables.

  • The advantages of using TRT tables are:
    The table is easily maintainable should additional delimiter characters be introduced.
    General Register 2 always contains the matching byte from the TRT table.
    Vector tables are incredibly easily created using this technique.
    Very few instructions are necessary.
    It processes quickly.

  • Since the TRT instruction requires the use of General Register 2, it is important to keep this register free for TRT instructions.  One technique might be to simply push R2 onto a stack for the TRT and pop it off the stack again once finished with the TRT processing.

  • The following is a code "snippet" for a simple parameter list parser using Translate and Test:


*----------------------------------------------------------------------
* On entry:
*        R4 -> start of buffer
*        R5 -> length of buffer
*----------------------------------------------------------------------
DELIM    DS    0H
           .
           .
           .
         NI    FLAG,X'7F'
*
* If TRT sets a non-zero condition code, it has fetched a 
* non-zero value from the table. Use the value as an index
* to an address vector.
*
DELIM10  DS    0H
         BCTR  R5,0                Decrement length for execute
         EX    R5,SCANTRT          Scan input buffer
         BC    8,DELIM90           End of string without delimiter
         BC    4,DELIM20           More data after this delimiter
         OI    FLAG,X'80'          No more after this delimiter
DELIM20  DS    0H
         LR    R3,R1 R1            has the address of the delimiter
         SR    R3,R4               Length not including delimiter
         L     R15,VECTOR(R2)      R2 contains value from table
         BASR  R14,R15             Call processing routine
           .
           .
           .
         TM    FLAG,X'80'          Exit if no more data
         BO    DELIM99
         LA    R4,1(R3,R4)         Start of data after delimiter
         SR    R5,R3               Remaining length
         B     DELIM10             Continue scanning input buffer
           .
           .
           .
*----------------------------------------------------------------------
*        No delimiters in the input string
*----------------------------------------------------------------------
DELIM90  DS    0H
           .
           .
           .
DELIM99  DS    0H

*----------------------------------------------------------------------
*        Current delimiter is a comma ","
*----------------------------------------------------------------------
COMMA    DS    0H
           .
           .
           .
         BR    R14

*----------------------------------------------------------------------
*        Current delimiter is a semicolon ";"
*----------------------------------------------------------------------
SEMICOLN DS    0H
           .
           .
           .
         BR    R14

*----------------------------------------------------------------------
*        Current delimiter is a left parenthesis "("
*----------------------------------------------------------------------
PARENL   DS    0H
           .
           .
           .
         BR    R14

*----------------------------------------------------------------------
*        Current delimiter is a right parenthesis ")"
*----------------------------------------------------------------------
PARENR   DS    0H
           .
           .
           .
         BR    R14

*----------------------------------------------------------------------
*        Vector for token processing
*----------------------------------------------------------------------
         DS    0A
         ORG   *-4
VECTOR   DS    0A
         ORG
         DC    A(COMMA)            Comma delimiter
         DC    A(SEMICOLN)         Semicolon delimiter
         DC    A(PARENL)           Left Parenthesis delimiter
         DC    A(PARENR)           Right Parenthesis delimiter

SCANTRT  TRT   0(0,R4),DELIMTAB    Executed TRT 

DELIMTAB DS    0F
*----------------------------------------------------------------------
*        This TRT table will scan for , and ; and ( and )
*----------------------------------------------------------------------
         DC    XL16'00000000000000000000000000000000' 00
         DC    XL16'00000000000000000000000000000000' 10
         DC    XL16'00000000000000000000000000000000' 20
         DC    XL16'00000000000000000000000000000000' 30
         DC    XL16'000000000000000000000000000C0000' 40
         DC    XL16'00000000000000000000000000100800' 50
         DC    XL16'00000000000000000000000400000000' 60
         DC    XL16'00000000000000000000000000000000' 70
         DC    XL16'00000000000000000000000000000000' 80
         DC    XL16'00000000000000000000000000000000' 90
         DC    XL16'00000000000000000000000000000000' A0
         DC    XL16'00000000000000000000000000000000' B0
         DC    XL16'00000000000000000000000000000000' C0
         DC    XL16'00000000000000000000000000000000' D0
         DC    XL16'00000000000000000000000000000000' E0
         DC    XL16'00000000000000000000000000000000' F0

I will continue to add tips and techniques as time permits.

Please direct any inquiries or problems regarding this web to webmaster@marcsweb.com


Page design, composition and HTML by Marc Niegowski
Copyright © 1998-2012, Marc Niegowski - Connectivity, Inc., All Rights Reserved
23 W. Fourth Street • Media • Pennsylvania • 19063-2805 • USA
Phone: 610-566-0227 • Fax: 610-566-0641 • Email: Marc@Tech-Center.com

Revision Date: Wednesday, November 15, 2006 09:56:11 AM