AMOCAS.B

RISC-V AMOCAS.B Instruction Details

Instruction ManualR-type

Atomic compare-and-swap byte: compare memory with rd[7:0], store rs2[7:0] on match, return old value in rd

Instruction Syntax

amocas.b rd, rs2, (rs1)
Operand Breakdown
Destination rd: register receiving the operation result.
Source rs1: register holding the first operand.
Source rs2: register holding the second operand.
ZacasAtomic Operation

Instruction Behavior

AMOCAS.B (Zacas extension) atomically loads a 8-bit value from the address in rs1 and compares it with the low 8 bits of rd; on a bitwise match it stores the low 8 bits of rs2 back to the same address. The loaded old value is sign-extended to XLEN and written to rd regardless of success. aq/rl ordering bits are supported.

Quick Understanding & Search Notes

AMOCAS.B atomically loads a 8-bit memory value, compares it bitwise with the compare value supplied in rd, stores the rs2 swap value on equality, and always writes the old memory value back to rd.

Before execution rd supplies the compare value; after execution rd is overwritten by the old memory value, not a Boolean success flag.
AMOCAS.B comes from the combination of Zacas and Zabha; upper compare/swap input bits are ignored under Zabha subword rules.
The address must be naturally aligned to the operand size; aq/rl ordering on success and failure follows the Zacas rules.

Common Usage Scenarios

Atomic & Sync

Understand this scenario with real code like «amocas.b a0, a2, (a1)».

Network & Byte Order

Understand this scenario with real code like «amocas.b a0, a2, (a1)».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is R-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

The compare value is the input value in rd; rd is also overwritten with the old memory value and is not a Boolean success flag.
The swap value comes from the low bits of rs2; the width is selected by the .b/.h/.w/.d/.q suffix.
The target address must be naturally aligned to the operand width unless relaxed by the platform misaligned atomicity granule PMA.

FAQ

How do I tell whether AMOCAS.B succeeded?

Compare the old value returned in rd with the expected value; equality indicates that the swap store occurred.

Does AMOCAS.B take the compare value from rs2?

No. rs2 supplies the new value to store on success; the compare value comes from rd before execution, or from the rd register pair.