3.5 INSTRUCTION EXAMPLES
The following paragraphs provide examples of how to use selected instructions.
3.5.1 Using the CAS and CAS2 Instructions
The CAS instruction compares the value in a memory location with the value in a data
register, and copies a second data register into the memory location if the compared values
are equal. This provides a means of updating system counters, history information, and
globally shared pointers. The instruction uses an indivisible read-modify-write cycle; after
CAS reads the memory location, no other instruction can change that location before CAS
has written the new value. This provides security in single-processor systems, in
multitasking environments, and in multiprocessor environments. In a single-processor
system, the operation is protected from instructions of an interrupt routine. In a multitasking
environment, no other task can interfere with writing the new value of a system variable. In
a multiprocessor environment, the other processors must wait until the CAS instruction
completes before accessing a global pointer.
The following code fragment shows a routine to maintain a count, in location SYS_CNTR,
of the executions of an operation that may be performed by any process or processor in a
system. The routine obtains the current value of the count in register D0 and stores the new
count value in register D1. The CAS instruction copies the new count into SYS_CNTR if it
is valid. However, if another user has incremented the counter between the time the count
was stored and the read-modify-write cycle of the CAS instruction, the write portion of the
cycle copies the new count in SYS_CNTR into D0, and the routine branches to repeat the
test. The following code sequence guarantees that SYS_CNTR is correctly incremented.
MOVE.W
INC_LOOP
MOVE.W
ADDQ.W
CAS.W
BNE
MOTOROLA
SYS_CNTR,D0
get the old value of the counter
D0,D1
make a copy of it
#1,D1
and increment it
D0,D1,SYS_CNTR
if countr value is still the same, update it
INC_LOOP
if not, try again
MC68030 USER'S MANUAL
Instruction Set Summary
3-25