LA 6,X'80' Put bit to be ORed into register 6 SLL 6,24 Shift left 24 places to align the byte
to beORed with the location of FLAGS within WORD L S,WORD Get original flag bit values
RETRY LR 4,5 Put flags to modify into register 4OR 4,6 Turn on bit in new copy of flags CS S,4,WORD Store new flags unless original flags
were changed
BNE RETRY If new flags not stored, try again
The format of theCS instruction is:
Machine FormatOp Code S1 BA ****
Assembler FormatOp Code R
1
, R
3
,S1 CS 5,4,WORD
TheCS instruction compares the first operand (register 5
containing the original flag values) to the second operand
(word) while storage access to the subject location is not
permitted to anyCPU other than the one executing the CS instruction.
If the compare is successful, indicating that FLAGS still
has the same value that it originally had, the modified copy
in register 4 is stored into FLAGS. If FLAGS has changed
since it was loaded, the compare will not be successful, and
the current value of FLAGS is loaded into register S.
TheCS instruction sets condition code 0 to indicate a
successful compare and swap, and condition code 1 toindi cate an unsuccessful compare and swap.
The program executing the example instructions tests the
condition code following theCS instruction and reexecutes
the flag-modifying instructions if theCS instruction indi cated an unsuccessful comparison. When the CS instruction
is successful, the program continues execution outside the
loop and FLAGS contains valid data.
Updating Counters
In this example, a 32-bit counter is updated by a program
using theCS instruction to ensure that the counter will be
correctly updated. The original value of the counter isob tained by loading the word containing the counter into a
register. The original counter is then moved into another
register to provide a modifiable copy, and a register(con taining an increment to the counter) is added to the modi fiable copy to provide the updated tounter value. The CS instruction is then used to ensure a (-'".lid store of the
counter. The following instruction sequence performs this
procedure:
LA
LLOOP LR
AR
6,1
S,CNTR
4,5
4,6
Put increment (1) in register
Get original counter valueSet up copy to modify Update counter in register CS 5,4,CNTR
BNELOOP Page of GA22-7000-4 Revised September 1,1975
By TNL: GN22-0498Update counter in storage
If original value changed, update new
value
CNTR (before):00 00 00 10
CNTR (after):00 00 00 11
The program updating the counter byte then checks the
result by examining the condition code.Condition code 0 indicates a successful update, and the program can proceed.
If the counter byte had been changed between the time that
the program loaded its original value and the time that it
executed theCS instruction, the CS instruction would have
loaded the new control byte value into register 5 and set
the condition code to 1, indicating an unsuccessful update.
The program would then have to update the new counter
value in register 5 and retry theCS instruction, retesting the
condition code, and retrying, until a successful update is
completed.
The following shows twoCPUs, A and B, executing the
above program simultaneously. That is, bothCPUs desire
to add one toCNTR. CPUA Reg4 Reg5
16 16
16 16
17 16
17 16CPUB Comments
CNTR Reg4 Reg5
16
16
16
16
16
17
17
18
16
17
17
18
18
16
16CPU A loads RegS and Reg4
from CNTRCPU B loads Reg5 and Reg4
from CNTRCPU B adds 1 to Reg4 CPU A adds 1 to Reg4 CPU A executes CS; successful
match store
17 CPU B executesCS; no match,
Reg5 changed
17CPU B loads RegS into Reg4
and adds 1 to Reg4
17CPU B executes CS; successful
match store
EXAMPLESOF THE USE OF COMPARE AND SWAP The following examples of the use of the COMPARE AND
SWAP instruction illustrate the applications for which the
instruction is intended. It is important to note that these
are examples of functions that can be performed bypro grams running enabled or by programs that are running on
a shared-main-storage multiprocessor.I Interlocked Sing1e-Word, or Smaller, Serially Reusable Resource (SR R)
The following routine allows a program to modify thecon tents of a storage location while running enabled, even
though the possibility exists that anotherCPU may simul taneously update the same location and even though the
routine may be interrupted by another program that updates
the location.
Appendix I. Number Representation and Instruction-Use Examples 311
to be
RETRY LR 4,5 Put flags to modify into register 4
were changed
BNE RETRY If new flags not stored, try again
The format of the
Machine Format
Assembler Format
1
, R
3
,
The
containing the original flag values) to the second operand
(word) while storage access to the subject location is not
permitted to any
If the compare is successful, indicating that FLAGS still
has the same value that it originally had, the modified copy
in register 4 is stored into FLAGS. If FLAGS has changed
since it was loaded, the compare will not be successful, and
the current value of FLAGS is loaded into register S.
The
successful compare and swap, and condition code 1 to
The program executing the example instructions tests the
condition code following the
the flag-modifying instructions if the
is successful, the program continues execution outside the
loop and FLAGS contains valid data.
Updating Counters
In this example, a 32-bit counter is updated by a program
using the
correctly updated. The original value of the counter is
register. The original counter is then moved into another
register to provide a modifiable copy, and a register
counter. The following instruction sequence performs this
procedure:
LA
L
AR
6,1
S,CNTR
4,5
4,6
Put increment (1) in register
Get original counter value
BNE
By TNL: GN22-0498
If original value changed, update new
value
CNTR (before):
CNTR (after):
The program updating the counter byte then checks the
result by examining the condition code.
If the counter byte had been changed between the time that
the program loaded its original value and the time that it
executed the
loaded the new control byte value into register 5 and set
the condition code to 1, indicating an unsuccessful update.
The program would then have to update the new counter
value in register 5 and retry the
condition code, and retrying, until a successful update is
completed.
The following shows two
above program simultaneously. That is, both
to add one to
16 16
16 16
17 16
17 16
CNTR Reg4 Reg5
16
16
16
16
16
17
17
18
16
17
17
18
18
16
16
from CNTR
from CNTR
match store
17 CPU B executes
Reg5 changed
17
and adds 1 to Reg4
17
match store
EXAMPLES
SWAP instruction illustrate the applications for which the
instruction is intended. It is important to note that these
are examples of functions that can be performed by
a shared-main-storage multiprocessor.
The following routine allows a program to modify the
though the possibility exists that another
routine may be interrupted by another program that updates
the location.
Appendix I. Number Representation and Instruction-Use Examples 311








































































































































































































































































































































