Sure! Let's break down this snippet from Pokémon Red (pokered) decompiled assembly into plain English:
🧠 Context Overview
This code is part of the damage calculation system during a battle. Specifically, it deals with applying the Same Type Attack Bonus (STAB) — which increases damage when a Pokémon uses a move that matches one of its own types.
🔍 Line-by-Line Breakdown (Plain English)
.sameTypeAttackBonus
A label marking where the STAB bonus logic begins.
ld hl, wDamage + 1
ld a, [hld]
ld h, [hl]
ld l, a ; hl = damage
- Load the current damage value (stored in memory at
wDamage + 1 and wDamage) into the HL register.
- Since
wDamage is a 16-bit value (two bytes), they load it in little-endian format: low byte first (wDamage), high byte next (wDamage + 1).
ld b, h
ld c, l ; bc = damage
- Copy the 16-bit damage value from
HL to BC for manipulation.
srl b
rr c ; bc = floor(0.5 * damage)
- Right-shift
B (the high byte), and rotate-right C (the low byte) through the carry flag.
- This effectively divides the 16-bit value in
BC by 2 (rounding down), giving half of the original damage.
add hl, bc ; hl = floor(1.5 * damage)
- Add the original damage (
HL) to half of itself (BC) → this computes 1.5× damage, using integer math:
damage + damage / 2 = 1.5 × damage
; store damage
ld a, h
ld [wDamage], a
ld a, l
ld [wDamage + 1], a
- Save the newly calculated damage (now 1.5×) back into memory at
wDamage.
ld hl, wDamageMultipliers
set BIT_STAB_DAMAGE, [hl]
- Load the address of a memory structure (
wDamageMultipliers) that holds various multipliers that affect final damage (like STAB, type effectiveness, critical hits, etc.).
- Set the bit corresponding to STAB (
BIT_STAB_DAMAGE) in that structure.
- This marks that the STAB multiplier should be applied later when the total damage is calculated.
💡 Note: The actual multiplication by the STAB multiplier (typically x1.5) happens elsewhere, after all flags are set.
✅ Summary in Plain English
"If the move's type matches one of the Pokémon's types, increase the damage by 50% (multiply by 1.5), then mark that the Same Type Attack Bonus should be applied later."
📝 Extra Notes
- The game stores damage as a 16-bit value split across two bytes (
wDamage = low byte, wDamage + 1 = high byte).
- Integer arithmetic is used throughout to avoid floating-point operations.
- STAB is not multiplied immediately here — just flagged. The actual multiplication happens after all modifiers are combined.
Let me know if you want to see how the final multiplier is applied!