TITLE ‘RE-ENTRANT          FIND TABLE ENTRY VIA BINARY SEARCH’
*---------------------------------------------------------------------*
* IMPORTANT: PLEASE READ/UNDERSTAND BEFORE USING.
* 1.INPUT TABLE MUST BE IN ASCENDING ORDER SEQUENCE.
* 2.KEY LENGTH(KEYLEN) MAY NOT EXCEED 256 BYTES.
* 3.KEY OFFSET(KEYOFF) IS FROM START OF ENTRY.
* 4.KEYOFF AND ENTRY LENGTH (ENTLEN) MAY EACH EXCEED 256 BYTES.
* 5.ENTRY COUNT(ENTCNT) MAY BE AN ODD NUMBER: (2)N-1.
* 6.LAST TABLE ENTRY SHOULD HAVE AN HEXFF'S KEY.
* 7.FOUND ENTRY ADR OR HEX00'S IS RETURNED VIA REG15.
* 8.ALL CALLER PARAMETERS AND TABLE ATTRIBUTES ARE ASSUMED VALID.
* 9.ARGUMENT LENGTH AND KEY LENGTH SHOULD BE EQUAL.
*---------------------------------------------------------------------*
*        CALL  BINSRCH,(ARGUMENT,TBPARMS)
*        LTR   3,15                Q,DESIRED ENTRY FOUND?
*        BZ    NOTFOUND             NO, HANDLE IT.
*        USING TBDSECT,3
*---------------------------------------------------------------------*
*                 REG7   REG8   REG9  REG10  REG11
*TBPARMS DC   A(INPTBL,ENTCNT,ENTLEN,KEYLEN,KEYOFF)
*---------------------------------------------------------------------*
         SPACE 2
BINSRCH  CSECT ,                   RE-ENTRANT                 1994ESOTO
         STM   14,12,12(13)        STORE CALLER REGISTERS.
         BALR  15,0                ESTABLISH ADDRESSABILITY.
         USING *,15
         LM    6,7,0(1)            LOAD CALLER PARAMETERS.
         LM    7,11,0(7)           LOAD INPUT TABLE ATTRIBUTES.
         LM    2,5,=A(BREG2,BREG3,BREG4,BREG5)
         AR    7,11                R7 NOW POINTS TO 1ST ENTRY KEY.
         BCTR  10,0                R10 NOW KEYLEN FOR EX/BTEST/CLC.
         LR    1,9                 INITIALLY R7 POINTS TO ENTRY ZERO
BREG2    DS    0H
         SR    7,1                 R7 NOW PREVIOUS INPTBL TEST ADR.
BREG3    DS    0H
         SRA   8,1                 DIVIDE CURRENT ENTCNT IN HALF.
         BNPR  4                   IF INITIALLY NOT FOUND, FAST FWD.
         LR    1,8                 CREATING NEW INPTBL TEST KEY ADR.
         MR    0,9                 R1 NOW NEW INPTBL TEST OFFSET.
         AR    7,1                 R7 NOW CURRENT INPTBL TEST ADR.
         EX    10,BTEST            Q,ARGUMENT = INPTBL ENTRY KEY?
         BLR   2                    USE PREVIOUS INPTBL TEST ADR.
         BHR   3                    USE CURRENT INPTBL TEST ADR.
         BR    5                    RETURN FOUND ENTRY ADR VIA R15.
BTEST    DS    0H
         CLC   0(0,6),0(7)         Q,ARGUMENT = INPTBL ENTRY KEY?
BREG4    DS    0H
         AR    7,9                 POINTS TO NEXT SEQUENTIAL ENTRY.
         EX    10,BTEST            Q,ARGUMENT = INPTBL ENTRY KEY?
         BHR   4                    CONTINUE FAST FORWARD SEARCH.
         BER   5                    RETURN FOUND ENTRY ADR VIA R15.
         LR    7,11                ENTRY NOT FOUND, RETURN HEX00'S.
BREG5    DS    0H
         SR    7,11                R7 FOUND ENTRY ADR OR HEX00'S.
         LR    15,7                R15 FOUND ENTRY ADR OR HEX00'S.
         LM    0,12,20(13)         RESTORE CALLER REGISTERS.
         BR    14                  RETURN TO CALLER CONTROL.
*
         END




         TITLE 'RE-ENTRANT     VALIDATE ASCENDING ORDER SEQUENCE TABLE'
*---------------------------------------------------------------------*
*        CALL  VALSEQ,(TBPARMS)
*        LTR   15,15               Q,TABLE IN SEQUENCE?
*        BNZ   ABORT                NO,ABORT JOB.
*---------------------------------------------------------------------*
*                 REG1   REG2   REG3   REG4   REG5                    *
*TBPARMS DC    A(INPTBL,ENTCNT,ENTLEN,KEYLEN,KEYOFF)                  *
*---------------------------------------------------------------------*
         SPACE 2
VALSEQ   CSECT ,                   RE-ENTRANT                 1999ESOTO
         STM   14,12,12(13)        SAVE CALLER REGISTERS.
         BALR  15,0                ESTABLISH ADDRESSABILITY.
         USING 15,*
         L     1,0(,1)             LOAD CALLER PARAMETER(TBPARMS).
         LM    1,5,0(1)            LOAD INPUT TABLE ATTRIBUTES.
         BCTR  2,0                 R2 NOW NUMBER OF TEST PAIRS.
         BCTR  4,0                 R4 NOW KEY LENGTH FOR VAL200.
         AR    5,1                 R5 POINTS TO 1ST ENTRY KEY.
         BALR  6,0                 R6 POINTS TO NEXT INSTR.
*
         LR    1,5                 R1 NOW 1ST KEY ADR.
         AR    5,3                 R5 NOW 2ND KEY ADR.
         EX    4,VAL300            Q,ENTRIES IN KEY SEQUENCE?
         BH    VAL200               N,SEQ-ERROR, EXIT.
         BCTR  2,6                 TEST NEXT PAIR OF ENTRY KEYS.
*
         XR    15,15               RETURN CODE: GOOD; IN-SEQUENCE.
VAL200   DS    0H
         LM    0,12,20(13)         RESTORE CALLER REGISTERS.
         BR    14                  RETURN TO CALLER CONTROL.
*
VAL300   DS    0H
         CLC   0(0,1),0(5)         Q,ENTRIES IN KEY SEQUENCE?
*
         END

© Copyright Edward Soto & IT Doctors.co.uk. 2003