Language Implementation
7-67
7
Example 5: atadd.c
The following example refers to the short C program shown in
Example 7-5 below. The asm containing the
atadd instruction is shown
in bold.
Example 7-5 atadd.c
static inline
int atadd(p, val)
volatile int *p;
int val;
{
int wtmp;
__asm__ __volatile__("atadd %4,%2,%1" : "=m"(*p),"=d"(wtmp)
: "dI"(val),"m" (*p),"d"(p));
return wtmp;
}
volatile int critical_var;
int other_var;
int add_crit()
{
atadd(&critical_var, 1);
if (atadd(&critical_var, 2) != 1)
atadd(&other_var, 1);
}
Consider the lines containing the asm:
__asm__ __volatile__("atadd %4,%2,%1" : "=m"(*p),"=d"(wtmp)
: "dI"(val),"m" (*p),"d"(p));
• "atadd %4,%2,%1" is the
asm-template
. atadd adds to memory
and locks the bus until it is finished. This feature is used by multi-
processor systems.
atadd takes three arguments. These values are
provided by the
out-list
and
in-list
.
•
"=m" (*p) is the first
output-spec
. It is the first operand,
i.e., operand 0. The
"=m"
constraint
indicates that any memory
operand can be used.