AMOCAS.B

RISC-V AMOCAS.B Instruction Details

Instruction ManualAMO-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)

Syntax is rd, rs2, (rs1), with no offset or rs3. Pre-execution rd supplies the comparison, rs2 the replacement, and rs1 the address.

32-bit encoding: funct5=00101, funct3=000, opcode=0101111; aq is bit26 and rl is bit25.

RV32/RV64, requiring Zacas (which depends on Zaamo) and Zabha. Compare 8-bit old memory and replace on equality; normal completion returns old memory, not a Boolean flag.

Use only the low 8 rd/rs2 bits for comparison/replacement; sign-extend old memory to XLEN. x0 sources are zero and x0 destination writes are discarded.

Every byte address is naturally aligned. The region must support this AMOCAS; overlapping registers read one pre-execution value, and aq/rl orders only the accessed domain.

ZabhaZacasAtomic Operation

Instruction Behavior

AMOCAS.B (the byte AMOCAS form provided by Zabha, with Zacas CAS semantics) atomically loads an 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. These data results assume normal completion of the access; rd=x0 discards writeback. Zacas depends on Zaamo.

AMOCAS.B Atomic Decode And Execute Animation

Shows Zabha + Zacas byte atomic field decode, aq/rl ordering bits, a memory read, and CAS state update.

rd
rs2
rs1
amocas.b
,
,(
)
Execution Context
31..27
26
25
24..20
19..15
14..12
11..7
6..0
00101
funct5
1
aq
0
rl
01011
rs2
01010
rs1
000
funct3
00101
rd
0101111
opcode
Atomic Data Path
instruction
0x2CB502AF
opcode
0101111 -> AMO
funct3
000 -> byte width
funct5
00101 -> AMOCAS.B
aq / rl
1/0 -> acquire ordering
rd / rs2 / rs1
t0(x5) compare=0xF2 / a1(x11) swap=0x7F / a0(x10)=0x00001000
memory read
mem[0x00001000] -> old byte 0xF2
RMW
Not computed
rd / memory
t0(x5) = 0x000000F2; mem[0x00001000] = 0xF2
Current Step

Concept Step: receive the 32-bit atomic instruction encoding

Zabha + Zacas-extension byte atomic instructions use a 32-bit encoding; the animation starts by splitting the fields.

encoding: 0x2CB502AF
syntax : amocas.b.aq t0(x5), a1(x11), (a0(x10))
Pending result : Not computed
Current architectural state
t0(x5) = 0x000000F2; mem[0x00001000] = 0xF2

Assumes enabled extensions at the selected XLEN, successful fetch, and a region supporting this AMOCAS with successful access; W/D/Q can select a missing-write-permission exception path. Misalignment requires the stated PMA condition. Sources are read before writeback; x0 sources are zero (both halves for a pair). aq/rl orders only the accessed domain. Steps are a teaching sequence.

Quick Understanding & Search Notes

AMOCAS.B atomically loads an 8-bit memory value, compares it bitwise with the compare value supplied in rd, stores the rs2 swap value on equality, and on normal completion returns the old value in rd (discarding the write for rd=x0).

After normal completion with nonzero rd, old memory overwrites the compare operand rd (rd/rd+1 for pair forms), 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; the A-extension exception options apply when it is not. aq/rl are Zabha's optional release-consistency ordering bits.

Common Usage Scenarios

Atomic & Sync

RV32/RV64 with required extensions enabled; assume a1=0x1000 and a supporting region with normal completion. With old memory=5, expected=5 and replacement=3, the example returns 5 and stores 3; changing expected to 4 returns 5 and leaves memory data at 5.

Pre-Use Checklist

Syntax Check
  • Syntax is rd, rs2, (rs1), with no offset or rs3. Pre-execution rd supplies the comparison, rs2 the replacement, and rs1 the address.
  • 32-bit encoding: funct5=00101, funct3=000, opcode=0101111; aq is bit26 and rl is bit25.
Semantic Check
  • RV32/RV64, requiring Zacas (which depends on Zaamo) and Zabha. Compare 8-bit old memory and replace on equality; normal completion returns old memory, not a Boolean flag.
  • Use only the low 8 rd/rs2 bits for comparison/replacement; sign-extend old memory to XLEN. x0 sources are zero and x0 destination writes are discarded.
  • Every byte address is naturally aligned. The region must support this AMOCAS; overlapping registers read one pre-execution value, and aq/rl orders only the accessed domain.

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.
Comparison and replacement use only the low 8 bits; upper register bits are ignored. rd=x0 compares zero; rs2=x0 replaces with zero.
Every byte address is naturally aligned; the region must still support this AMOCAS width and operation.

FAQ

How do I tell whether AMOCAS.B succeeded?

After normal completion with nonzero rd, compare the low 8 bits of the value returned in rd with the low 8 bits of the expected value saved before execution; equality indicates that the swap store occurred. The returned byte is sign-extended in rd.

Does AMOCAS.B take the compare value from rs2?

No. The low operand-width bits of rd before execution supply the compare value; the same-width low bits of rs2 supply the replacement.