<?xml-stylesheet type="text/xsl" encoding="UTF-8" href="iform.xsl" version="1.0"?>
<!DOCTYPE instructionsection PUBLIC "-//ARM//DTD instructionsection //EN" "iform-p.dtd">
<!-- Copyright (c) 2010-2025 Arm Limited or its affiliates. All rights reserved. -->
<!-- This document is Non-Confidential. This document may only be used and distributed in accordance with the terms of the agreement entered into by Arm and the party that Arm delivered this document to. -->
<instructionsection id="CASB" title="CASB, CASAB, CASALB, CASLB -- A64" type="instruction">
  <docvars>
    <docvar key="address-form" value="base-register"/>
    <docvar key="instr-class" value="general"/>
    <docvar key="isa" value="A64"/>
  </docvars>
  <heading>CASB, CASAB, CASALB, CASLB</heading>
  <desc>
    <brief>
      <para>Compare and swap byte in memory</para>
    </brief>
    <authored>
      <para>This instruction
reads an 8-bit byte
from memory, and compares it against the value held in a first
register. If the comparison is equal, the value in a second register
is written to memory. If the comparison is not equal, the architecture permits writing
the value read from the location to memory.
If the write is performed, the read and write occur atomically such
that no other modification of the memory location can take place
between the read and write.</para>
      <list type="unordered">
        <listitem>
          <content>If the destination register is not one of <value>WZR</value> or <value>XZR</value>, <instruction>CASAB</instruction> and <instruction>CASALB</instruction> load from memory with acquire semantics.</content>
        </listitem>
        <listitem>
          <content>
            <instruction>CASLB</instruction> and <instruction>CASALB</instruction> store to memory with release semantics.</content>
        </listitem>
        <listitem>
          <content>
            <instruction>CASB</instruction> has neither acquire nor release semantics.</content>
        </listitem>
      </list>
      <para>The architecture permits that the data read clears any exclusive
monitors associated with that location, even if the compare
subsequently fails.</para>
      <para>If the instruction generates a synchronous Data Abort, the register
which is compared and loaded, that is <syntax>&lt;Ws&gt;</syntax>, is restored to
the values held in the register before the instruction was executed.</para>
      <para>For a <instruction>CASB</instruction> or <instruction>CASAB</instruction> instruction, when <syntax>&lt;Ws&gt;</syntax>
or <syntax>&lt;Xs&gt;</syntax> specifies the same register as <syntax>&lt;Wt&gt;</syntax> or <syntax>&lt;Xt&gt;</syntax>,
this signals to the memory system that an additional subsequent <instruction>CASB</instruction>,
<instruction>CASAB</instruction>, <instruction>CASALB</instruction>, or <instruction>CASLB</instruction>
access to the specified location is likely to occur in the near future. The memory system can respond by
taking actions that are expected to enable the subsequent <instruction>CASB</instruction>,
<instruction>CASAB</instruction>, <instruction>CASALB</instruction>, or <instruction>CASLB</instruction> access to succeed when it does occur.</para>
      <para>A code sequence starting with a <instruction>CASB</instruction> or <instruction>CASAB</instruction> instruction for which
<syntax>&lt;Ws&gt;</syntax> or <syntax>&lt;Xs&gt;</syntax> specifies the same register as <syntax>&lt;Wt&gt;</syntax>
or <syntax>&lt;Xt&gt;</syntax>, and ending with a subsequent <instruction>CASB</instruction>, <instruction>CASAB</instruction>,
<instruction>CASALB</instruction>, or <instruction>CASLB</instruction> to the same location, exhibits the following
properties for best performance when the location may be accessed concurrently, on one or more other PEs:</para>
      <list type="unordered">
        <listitem>
          <content>The sequence does not contain any direct system register writes, address translation instructions, cache or TLB
  maintenance operations, exception producing instructions, exception returns, or <instruction>ISB</instruction> barriers.</content>
        </listitem>
        <listitem>
          <content>The execution of the sequence includes 32 or fewer instructions.</content>
        </listitem>
        <listitem>
          <content>The value provided in <syntax>&lt;Ws&gt;</syntax> or <syntax>&lt;Xs&gt;</syntax> of the first <instruction>CASB</instruction> or <instruction>CASAB</instruction>
  is a value likely to result in the comparison failing.
  A failing comparison result may lead to better performance due to the hardware not performing a write to memory.</content>
        </listitem>
      </list>
      <note>
        <para>For a <instruction>CASB</instruction> or <instruction>CASAB</instruction> instruction, when <syntax>&lt;Ws&gt;</syntax> or
<syntax>&lt;Xs&gt;</syntax> specifies the same register as <syntax>&lt;Wt&gt;</syntax> or <syntax>&lt;Xt&gt;</syntax>, the
value in memory is not modified, because the <instruction>CASB</instruction> or <instruction>CASAB</instruction> either fails its compare or writes the same value back to memory.</para>
      </note>
      <para>For more information about memory ordering semantics, see <xref linkend="ARMARM_BEIHCHEF">Load-Acquire, Store-Release</xref>.</para>
      <para>For information about addressing modes, see
<xref linkend="ARMARM_CHDIIIBB">Load/Store addressing modes</xref>.</para>
    </authored>
  </desc>
  <alias_list howmany="0"/>
  <classes>
    <iclass name="No offset" oneof="1" id="iclass_no_offset" no_encodings="4" isa="A64">
      <docvars>
        <docvar key="address-form" value="base-register"/>
        <docvar key="instr-class" value="general"/>
        <docvar key="isa" value="A64"/>
      </docvars>
      <iclassintro count="4"/>
      <arch_variants>
        <arch_variant feature="FEAT_LSE" name="v8Ap1"/>
      </arch_variants>
      <regdiagram form="32" psname="A64.ldst.comswap.CASB_C32_comswap" tworows="1">
        <box hibit="31" width="2" name="size" usename="1" settings="2" psbits="xx">
          <c>0</c>
          <c>0</c>
        </box>
        <box hibit="29" width="7" settings="7">
          <c>0</c>
          <c>0</c>
          <c>1</c>
          <c>0</c>
          <c>0</c>
          <c>0</c>
          <c>1</c>
        </box>
        <box hibit="22" width="1" name="L" usename="1">
          <c colspan="1"/>
        </box>
        <box hibit="21" width="1" settings="1">
          <c>1</c>
        </box>
        <box hibit="20" width="5" name="Rs" usename="1">
          <c colspan="5"/>
        </box>
        <box hibit="15" width="1" name="o0" usename="1">
          <c colspan="1"/>
        </box>
        <box hibit="14" width="5" name="Rt2" usename="1" settings="5" psbits="xxxxx">
          <c>1</c>
          <c>1</c>
          <c>1</c>
          <c>1</c>
          <c>1</c>
        </box>
        <box hibit="9" width="5" name="Rn" usename="1">
          <c colspan="5"/>
        </box>
        <box hibit="4" width="5" name="Rt" usename="1">
          <c colspan="5"/>
        </box>
      </regdiagram>
      <encoding name="CASB_C32_comswap" oneofinclass="4" oneof="4" label="CASB" bitdiffs="L == 0 &amp;&amp; o0 == 0">
        <docvars>
          <docvar key="instr-class" value="general"/>
          <docvar key="isa" value="A64"/>
          <docvar key="address-form" value="base-register"/>
          <docvar key="mnemonic" value="CASB"/>
        </docvars>
        <box hibit="22" width="1" name="L">
          <c>0</c>
        </box>
        <box hibit="15" width="1" name="o0">
          <c>0</c>
        </box>
        <asmtemplate><text>CASB  </text><a hover="Is the 32-bit name of the general-purpose register to be compared and loaded, encoded in the &quot;Rs&quot; field." link="WsOrWZR__3">&lt;Ws&gt;</a><text>, </text><a hover="Is the 32-bit name of the general-purpose register to be conditionally stored, encoded in the &quot;Rt&quot; field." link="WtOrWZR__3">&lt;Wt&gt;</a><text>, [</text><a hover="Is the 64-bit name of the general-purpose base register or stack pointer, encoded in the &quot;Rn&quot; field." link="XnSP_option">&lt;Xn|SP&gt;</a><text>{, #0}]</text></asmtemplate>
      </encoding>
      <encoding name="CASAB_C32_comswap" oneofinclass="4" oneof="4" label="CASAB" bitdiffs="L == 1 &amp;&amp; o0 == 0">
        <docvars>
          <docvar key="instr-class" value="general"/>
          <docvar key="isa" value="A64"/>
          <docvar key="address-form" value="base-register"/>
          <docvar key="mnemonic" value="CASAB"/>
        </docvars>
        <box hibit="22" width="1" name="L">
          <c>1</c>
        </box>
        <box hibit="15" width="1" name="o0">
          <c>0</c>
        </box>
        <asmtemplate><text>CASAB  </text><a hover="Is the 32-bit name of the general-purpose register to be compared and loaded, encoded in the &quot;Rs&quot; field." link="WsOrWZR__3">&lt;Ws&gt;</a><text>, </text><a hover="Is the 32-bit name of the general-purpose register to be conditionally stored, encoded in the &quot;Rt&quot; field." link="WtOrWZR__3">&lt;Wt&gt;</a><text>, [</text><a hover="Is the 64-bit name of the general-purpose base register or stack pointer, encoded in the &quot;Rn&quot; field." link="XnSP_option">&lt;Xn|SP&gt;</a><text>{, #0}]</text></asmtemplate>
      </encoding>
      <encoding name="CASALB_C32_comswap" oneofinclass="4" oneof="4" label="CASALB" bitdiffs="L == 1 &amp;&amp; o0 == 1">
        <docvars>
          <docvar key="instr-class" value="general"/>
          <docvar key="isa" value="A64"/>
          <docvar key="address-form" value="base-register"/>
          <docvar key="mnemonic" value="CASALB"/>
        </docvars>
        <box hibit="22" width="1" name="L">
          <c>1</c>
        </box>
        <box hibit="15" width="1" name="o0">
          <c>1</c>
        </box>
        <asmtemplate><text>CASALB  </text><a hover="Is the 32-bit name of the general-purpose register to be compared and loaded, encoded in the &quot;Rs&quot; field." link="WsOrWZR__3">&lt;Ws&gt;</a><text>, </text><a hover="Is the 32-bit name of the general-purpose register to be conditionally stored, encoded in the &quot;Rt&quot; field." link="WtOrWZR__3">&lt;Wt&gt;</a><text>, [</text><a hover="Is the 64-bit name of the general-purpose base register or stack pointer, encoded in the &quot;Rn&quot; field." link="XnSP_option">&lt;Xn|SP&gt;</a><text>{, #0}]</text></asmtemplate>
      </encoding>
      <encoding name="CASLB_C32_comswap" oneofinclass="4" oneof="4" label="CASLB" bitdiffs="L == 0 &amp;&amp; o0 == 1">
        <docvars>
          <docvar key="instr-class" value="general"/>
          <docvar key="isa" value="A64"/>
          <docvar key="address-form" value="base-register"/>
          <docvar key="mnemonic" value="CASLB"/>
        </docvars>
        <box hibit="22" width="1" name="L">
          <c>0</c>
        </box>
        <box hibit="15" width="1" name="o0">
          <c>1</c>
        </box>
        <asmtemplate><text>CASLB  </text><a hover="Is the 32-bit name of the general-purpose register to be compared and loaded, encoded in the &quot;Rs&quot; field." link="WsOrWZR__3">&lt;Ws&gt;</a><text>, </text><a hover="Is the 32-bit name of the general-purpose register to be conditionally stored, encoded in the &quot;Rt&quot; field." link="WtOrWZR__3">&lt;Wt&gt;</a><text>, [</text><a hover="Is the 64-bit name of the general-purpose base register or stack pointer, encoded in the &quot;Rn&quot; field." link="XnSP_option">&lt;Xn|SP&gt;</a><text>{, #0}]</text></asmtemplate>
      </encoding>
      <ps_section howmany="1">
        <ps name="A64.ldst.comswap.CASB_C32_comswap" sections="1" secttype="noheading">
          <pstext mayhavelinks="1" section="Decode" rep_section="decode">if !IsFeatureImplemented(FEAT_LSE) then EndOfDecode(Decode_UNDEF); end;
let s : integer{} = UInt(Rs);
let t : integer{} = UInt(Rt);
let n : integer{} = UInt(Rn);
let acquire : boolean = L == '1' &amp;&amp; t != 31;
let release : boolean = o0 == '1';
let tagchecked : boolean = n != 31;</pstext></ps>
      </ps_section>
    </iclass>
  </classes>
  <explanations scope="all">
    <explanation enclist="CASB_C32_comswap, CASAB_C32_comswap, CASALB_C32_comswap, CASLB_C32_comswap" symboldefcount="1">
      <symbol link="WsOrWZR__3">&lt;Ws&gt;</symbol>
      <account encodedin="Rs">
        <intro>
          <para>Is the 32-bit name of the general-purpose register to be compared and loaded, encoded in the "Rs" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="CASB_C32_comswap, CASAB_C32_comswap, CASALB_C32_comswap, CASLB_C32_comswap" symboldefcount="1">
      <symbol link="WtOrWZR__3">&lt;Wt&gt;</symbol>
      <account encodedin="Rt">
        <intro>
          <para>Is the 32-bit name of the general-purpose register to be conditionally stored, encoded in the "Rt" field.</para>
        </intro>
      </account>
    </explanation>
    <explanation enclist="CASB_C32_comswap, CASAB_C32_comswap, CASALB_C32_comswap, CASLB_C32_comswap" symboldefcount="1">
      <symbol link="XnSP_option">&lt;Xn|SP&gt;</symbol>
      <account encodedin="Rn">
        <intro>
          <para>Is the 64-bit name of the general-purpose base register or stack pointer, encoded in the "Rn" field.</para>
        </intro>
      </account>
    </explanation>
  </explanations>
  <ps_section howmany="1">
    <ps name="A64.ldst.comswap.CASB_C32_comswap" sections="1" secttype="Operation">
      <pstext mayhavelinks="1" section="Execute" rep_section="execute">var address : bits(64);
var comparevalue : bits(8);
var newvalue : bits(8);

let privileged : boolean = PSTATE.EL != EL0;
let accdesc : AccessDescriptor = CreateAccDescAtomicOp(MemAtomicOp_CAS, acquire, release,
                                                       tagchecked, privileged, t, s);
comparevalue = X{8}(s);
newvalue = X{8}(t);

if n == 31 then
    CheckSPAlignment();
    address = SP{64}();
else
    address = X{64}(n);
end;

let data : bits(8) = MemAtomic{}(address, comparevalue, newvalue, accdesc);
X{32}(s) = ZeroExtend{32}(data);</pstext></ps>
  </ps_section>
  <timestamp>2026-03-12 12:23:09</timestamp>
  <commit_id>2025-09_rel_asl1</commit_id>
</instructionsection>
