What does it do?
The EX instruction is used to modify an existing instruction and then execute it. The format looks like this:
EX R,D(X,B)
Here D(X,B)) is the address of an instruction. Normall this is not an instruction we want to use otherwise, and we list in our source code file among the variables after the "BR 14" line.
When EX is executed, it does the following, in this order:
second byte of copy = second byte of copy OR fourth byte of R
The instruction at D(X,B) is not changed.
What is an OR operation
An OR operation is an operation done on a pair of bits. Suppose we have bits A and B. Then:
A OR B = the larger of the two values
or (equivalent):
A OR B = 0 if both A and B are 0 and = 1 otherwise
When we have an OR operation on two bytes X and Y, we work with corresponding pairs of bits, one from X and one from Y,left to right.
If one of the bytes is X'00', then we are simply making a copy of the other byte. This is often the case when we use EX.
Why is this useful?
It allows us to execute an instruction while providing (at execution time) some information about its arguments. What kind of information can we provide?
and you may be able to come up with more.
Example
Suppose we have these variables:
BUFFER DS CL80 PNUM DS PL8
Suppose we have just read some data into BUFFER with XREAD. On the input line is a zoned decimal number, and we want to use PACK to convert this to a packed decimal value in PNUM. For the sake of this example, assume the zoned decimal number starts at the first byte of BUFFER.
If we know in advance exactly how long the zoned decimal number is, this is not hard to do. Sometimes, however, we may not know how long the zoned decimal number is until we have read the actual data.
Suppose we happen to know that the actual length of the zoned decimal number is in register 5. Then we can do the following:
BCTR 5,0 EX 5,PACKIT
where we have defined PACKIT somewhere as:
PACKIT PACK PNUM(8),BUFFER(0)
What will happen? The second byte of the encoded form of PACKIT will be X'70'. (The second length is 0 and will be encoded as 0.) When EX is executed, we will have an OR operation between X'70' and the fourth byte of register 5, which will be of the form X'0N', where N = one less than the actual length. The result of the OR operation will be a byte of the form X'7N'.
The result of the EX instrcuction is that we will execute our PACK instruction with the lengths we want.
We could have a similar example using MVC. The MVC instruction would initially have a length of 0, which would be encoded as a byte of 0. The OR operation would replace the X'00' byte with the fourth byte of the register.
By the way, how are we discovering this information about lengths at execution time? Typically we do so using the TRT instruction.
EX with EX
It is not permitted to use EX to execute an EX instruction. If you attempt to do so, your program should ABEND with interruption code 0003.