Instructions of the week -- LA and EX 
Author Message
 Instructions of the week -- LA and EX

EXLA     TITLE '                                   Use of EX and LA Ins?
               tructions'
* The module discusses the EX and LA instructions, and provides an
* example that makes use of both instructions.
*
* THE LA INSTRUCTION
*
* The LA instruction is one of the most useful of the original
* System/360 instructions.  It is useful because of the surprising
* fact that it is the only instruction that acts as an adder with
* three inputs.
*
* The adder inputs are --
*   - The 12-bit "displacement" field in the instruction
*   - The low order 31 bits of two 32-bit registers
*
* The restrictions on the adder inputs seem disabling at first, but
* they hardly matter for most real problems.
*
* Beware of the "address" in the instruction name.  It can be used for
* address arithmetic, but it can be used for many other purposes.
*
* What are some of the more common uses?
*
* Insert a small value (e.g., a value less than 4096 decimal) into a
* register
*
*          LA    1,1                 Insert 1 into register 1
*
*   (The registers are specified as register 0, which means that no
*   register will be used in resolving the address.  The only input to
*   the adder is the displacment field)
*
* Add a small value to a register
*
*          LA    1,1(,1)             Add 1 to register 1
*
*   (The inputs to the adder are the contents of the displacment field,
*   1, and the contents of register 1)
*
* Add two registers together, and place the result into a third
* register.
*
*          LA    0,0(1,1)            Add register 1 to register 1,
*                                     and place the result into reg 0
*
*
* Update a counter
*
*          LA    0,1
*          A     0,counter
*          ST    0,counter
*
*   (This method is slower than the classic idea of a counter update,
*   but also safer.  It is safer because it uses reg 0 as a work
*   register; reg 0 is often the forgotten register, so any use
*   is also a help.  It is also safer because the arithmetic
*   uses standard rules, rather than the address computation rules
*   followed by LA.)
*
* The following code fragment reads a partitioned data set directory,
* and extracts each member entry.  Extended notes are indicated by a
* field like (1) before the normal comment area.
*
* DSCN0100 GET   pdsdcb          (1) Read a directory block
*          LH    15,0(,1)            Load bytes used in the block
*          LA    15,0(15,1)      (2) Compute end of data in the block
*          BCTR  15,0                Compute address of last byte of
*                                     data in the directory block
*          LA    1,2(,1)         (3) Compute addr of first directory
*                                     entry
* DSCN0200 CLC   =FL8'-1',0(1)       Test if last entry in the
*                                     directory
*          BE    direof          (4) Br if so
* ******************* Process the directory entry *******************
* DSCN0300 IC    14,11(,1)           Load "indicator" byte
*          N     14,=A(X'1F')    (5) Turn off alias and # of TTR
*                                     entries in "indicator" byte
*          LA    14,12(14,14)    (6) Compute length of the current
*                                     directory entry
*          BXLE  1,14,DSCN0200       Compute addr of the next
*                                     directory entry, br if entry
*                                      is in current data area
*          B     DSCN0100            Go read the next directory block
*
* Notes --
*  (1) The GET macro returns the address of a directory block in reg 1.
*  (2) LA to add two registers together
*  (3) LA to add a small value to the contents of a register
*  (4) The branch defines the "normal" end of a directory.  If the
*      end of data for the data set is entered before this
*      instruction branches, it generally indicates a structural
*      error in the directory that should be investigated.
*  (5) The N instruction also 0's the high order 24 bits of register
*      14, as well as the high order 3 bits of the "indicator" byte,
*      so there is no need to preset the register to 0 before
*      executing the IC instruction, which is usually required.
*  (6) The instruction doubles the value in register 14, and then
*      adds 12 to the result.  This is too simplistic: on most modern
*      systems the computation is done in parallel somehow, not as
*      two serial adds.
*
* THE EX INSTRUCTION
*
* The EX instruction executes an instruction outside of the
* "normal" instruction stream.  There are advantages and
* disadvantages to this.
*
* The main disdvantage is the instruction stack loader considers
* an EX instruction to be a "successful" branch, so it stops loading
* the stack.
*
* In the 24-bit addressing mode, there may also be unexpected
* results if the program depends on the ILC in the PSW following
* execution of the instruction.
* _____________________________________________________________________
*
*   (From Principles of Operation)
*
*    EX     R1,D2(X2,B2)     RX
*
*     ________ ____ ____ ____ ____________
*    3  '44'  3 R1 3 X2 3 B2 3     D2     3
*    3________3____3____3____3____________3
*    0         8   12   16   20          31
* _____________________________________________________________________
*
* The R1 operand designates a register if it is not 0.  The
* low order 8 bits of this registers are ORed with the contents
* of the second byte of the instruction pointed to by the address
* D2(X2,B2).
*
* The ability to "alter" the contents of the instruction that is the
* target of the EX instruction is its most useful feature.
*
* For example, in OS/360 derived systems, like MVS, the PARM
* field specified on an EXEC JCL statement is passed to the program
* as a two byte binary length field followed by the data.
* For example, PARM='123' appears to the program as
*
*          DC    H'3',C'123'
*
* The following code will convert PARM field to binary
*
* S1       SAVE  (14,12)             Save caller's registers
*          BALR  12,0                Store current address into R12
*          USING *,12                Establish addressability
*          L     1,0(,1)             Load R1 with address of the
* *                                   parameter text
*          LH    15,0(,1)            Load length of the parameter text
*          BCTR  15,0                Reduce length by 1 for hardware
*          EX    15,PACK             Convert text data to packed
* *                                   decimal in DWORK
*          CVB   15,DWORK            Convert packed decimal in DWORK
* *                                   to binary
*          .
*          .
*          .
* PACK     PACK  DWORK,2(*-*,1)
* DWORK    DC    D'0'
*
* The PACK instruction expands as --
*
*         DC    0AL2(0),AL1(X'F2'),AL.4(L'DWORK-1,*-*),S(DWORK),S(2(1))
*
* When the EX instruction executes, the second byte of the instruction
* contains AL.4(7,0).  For those not familiar with IBM Assembler
* nomenclenture, a length attribute that starts with a . implies bits,
* so the Assembler generates 7 in 4 bits, 0111, and then 0 in 4 bits,
* 0000, and the field as a whole contains X'70'.
*
* For PARM='123', the length is 3, and register 15 contains 2.
* ORing the low order 8 bits of register 15 generates X'72' that is
* used as though it were the second byte of the PACK instruction.
* In other words, the PACK instruction executes as though
*
*          PACK  DWORK,2(3,1)
*
* was in the program, which is just what we want.
         EJECT
         MACRO
         SETR
         LCLA  &A
L       AIF   (&A GT 15).X
R&A      EQU   &A
&A       SETA  &A+1
         AGO   .L
X       MEND
         SPACE 5
DIRLIST  CSECT
         PUSH  PRINT
         PRINT NOGEN
         DCBD  DSORG=QS,DEVD=DA
         SETR  ,
         POP   PRINT
         EJECT
DIRLIST  CSECT
         USING *,R12               ESTABLISH ADDRESSABILITY
         SAVE  (14,12),,*          SAVE REGISTERS
         LR    R12,R15             COPY EP ADDR TO R12
         LA    R15,S               LOAD ADDR OF MY SAVE AREA
         ST    R15,8(,R13)         ADD NEW SAVE AREA TO THE
         ST    R13,4(,R15)          SAVE AREA CHAIN
         LR    R13,R15             ESTABLISH A NEW SAVE AREA
         LA    R2,OPENPARM         LOAD ADDR OF THE OPEN PARM LIST
         OPEN  MF=(E,(R2))         OPEN THE DATA SETS
         SR    R3,R3               SET FLAG = 0
         USING IHADCB,R1           ESTABLISH DCB ADDRESSABILITY
OTST0100 L     R1,0(,R2)           LOAD DCB ADDR FROM PARM LIST ENTRY
         TM    DCBOFLGS,DCBOFOPN   TEST IF OPEN OK
         BO    OTST0200            BR IF SO
         LA    R3,4                SERT FLAG = 4
OTST0200 TM    0(R2),X'80'         TEST IF LAST ENTRY IN PARM LIST
         LA    R2,4(,R2)           BUMP R2 TO NEXT PARM LIST ENTRY
         BZ    OTST0100            BR IF LAST ENTRY NOT END OF LIST
         LTR   R3,R3               TEST IF OPEN OK
         BNZ   EXIT                BR IF NOT
         DROP  R1                  KILL DCB ADDRESSABILITY
         EJECT
         CNOP  0,8
DSCN0100 GET   SYSUT1              READ A DIRECTORY BLOCK
         LA    R15,1               SET R15 = 1
         L     R0,DIRBLOCK         LOAD NUMBER OF BLOCKS
         AR    R0,R15              ADD 1
         ST    R0,DIRBLOCK
         L     R0,UBLOCK           LOAD NUMBER OF USED BLOCKS
         AR    R0,R15              ADD 1
         ST    R0,UBLOCK           STORE
         LH    R3,0(,R1)           LOAD LENGTH OF DIRECTORY ENTRIES
         LA    R3,0(R3,R1)         COMPUTE END OF DIRECTORY ENTRIES
         BCTR  R3,0                COMPUTE ADDR OF LAST BYTE IN        ?
                                    DIRECTORY ENTRIES
         LA    R4,2(,R1)           COMPUTE ADDR OF FIRST DIRECTORY ENT
DSCN0200 CLC   =FL8'-1',0(R4)      TEST IF LAST DIRECTORY ENTRY
         BE    LASTMEMB            BR IF SO
         MVI   OUTLINE+4,C' '      CLEAR THE FIRST PART OF
...

read more »



Mon, 15 Jan 2001 03:00:00 GMT  
 Instructions of the week -- LA and EX

Quote:

>                tructions'
> * The module discusses the EX and LA instructions, and provides an
> * example that makes use of both instructions.
> *
> * THE LA INSTRUCTION
> *
> * The LA instruction is one of the most useful of the original
> * System/360 instructions.  It is useful because of the surprising
> * fact that it is the only instruction that acts as an adder with
> * three inputs.
> *
> * The adder inputs are --
> *   - The 12-bit "displacement" field in the instruction
> *   - The low order 31 bits of two 32-bit registers
> *
> * The restrictions on the adder inputs seem disabling at first, but
> * they hardly matter for most real problems.
> *
> * Beware of the "address" in the instruction name.  It can be used for
> * address arithmetic, but it can be used for many other purposes.
> *
> * What are some of the more common uses?
> *
> * Insert a small value (e.g., a value less than 4096 decimal) into a
> * register
> *
> *          LA    1,1                 Insert 1 into register 1
> *
> *   (The registers are specified as register 0, which means that no
> *   register will be used in resolving the address.  The only input to
> *   the adder is the displacment field)
> *
> * Add a small value to a register
> *
> *          LA    1,1(,1)             Add 1 to register 1
> *
> *   (The inputs to the adder are the contents of the displacment field,
> *   1, and the contents of register 1)
> *
> * Add two registers together, and place the result into a third
> * register.
> *
> *          LA    0,0(1,1)            Add register 1 to register 1,
> *                                     and place the result into reg 0

No!  This will double the content of Register 1, and place the content
in Register 0.

You need LA 0,1(1,0) in order to increment.

Quote:
> *
> *
> * Update a counter
> *
> *          LA    0,1
> *          A     0,counter
> *          ST    0,counter
> *
> *   (This method is slower than the classic idea of a counter update,
> *   but also safer.

"Safer" is an understantment.  Register 1 cannot be incremented by LA,
so you need to use someother method, such as below.

However, one important point seems to have been overlooked,
namely, that the sum produced by LA is truncated to 24 bits,
and hence is always positive.  (the upper 8 bits are set to zero.).

- Show quoted text -

Quote:
>  It is safer because it uses reg 0 as a work
> *   register; reg 0 is often the forgotten register, so any use
> *   is also a help.  It is also safer because the arithmetic
> *   uses standard rules, rather than the address computation rules
> *   followed by LA.)
> *



Wed, 17 Jan 2001 03:00:00 GMT  
 Instructions of the week -- LA and EX
[snip]
: > * Add two registers together, and place the result into a third
: > * register.
: > *
: > *          LA    0,0(1,1)            Add register 1 to register 1,
: > *                                     and place the result into reg 0

: No!  This will double the content of Register 1, and place the content
: in Register 0.

: You need LA 0,1(1,0) in order to increment.

Huh?  His comment clearly says "add two registers together and place the
result into a third register."  That's exactly what his code does.

: > * Update a counter
: > *
: > *          LA    0,1
: > *          A     0,counter
: > *          ST    0,counter
: > *
: > *   (This method is slower than the classic idea of a counter update,
: > *   but also safer.

: "Safer" is an understantment.  Register 1 cannot be incremented by LA,
: so you need to use someother method, such as below.

Register 1 *can* be incremented by LA (see Pop).  Perhaps you are thinking
of register 0.

: However, one important point seems to have been overlooked,
: namely, that the sum produced by LA is truncated to 24 bits,
: and hence is always positive.  (the upper 8 bits are set to zero.).

No.  Only in 24-bit mode are the upper 8-bits set to zero.  In 31-bit mode
(arguably the "norm"), only the first bit is set to zero.  I agree the sum
is always positive in either case.

--
| Edward E. Jaffe                | Voice:      (310) 338-0400 x318     |
| Mgr., Research & Development   | Facsimile:  (310) 338-0801          |

| 9841 Airport Blvd, Suite 700   | IBM Mail:   USS24J24 at IBMMAIL     |        
| Los Angeles, CA 90045          | Web page:   www.phoenixsoftware.com |



Wed, 17 Jan 2001 03:00:00 GMT  
 Instructions of the week -- LA and EX

writes:

Quote:
>> * Add two registers together, and place the result into a third
>> * register.
>> *
>> *          LA    0,0(1,1)            Add register 1 to register 1,
>> *                                     and place the result into reg 0

>No!  This will double the content of Register 1, and place the content
>in Register 0.

>You need LA 0,1(1,0) in order to increment.

I think you have been reading messages a little bit too quickly. 8)

Mark A. Young



Wed, 17 Jan 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Instructions of the week -- LA and EX

2. EX instruction: a question

3. Why use an LA instruction?

4. Proposed D & DR instructions of the week

5. Instruction for next week: BC

6. Instruction of the Week--LOCTR anyone?

7. Instruction of the week -- EDMK

8. Instruction of the week - BXLE

9. Week number + firstDay of week

10. DAY OF THE WEEK ROUTINE - day-week.txt (1/1)

11. DAY OF THE WEEK ROUTINE - day-week.txt (0/1)

12. Instruction Speed and instruction availability

 

 
Powered by phpBB® Forum Software