Topics

UTTORI ASM · IN DEVELOPMENT

Conditionals and loops

Write assembly using conditionals and loops, with examples and limits.

On this page

SNES behavior

if, elseif, else, endif

!Region = 1

if !Region == 0
  db "J"
elseif !Region == 1
  db "U"
else
  error "Unknown region"
endif

The first nonzero branch runs. Conditions use the same expression parser as data and assertions. Labels in conditions must resolve statically.

while, endwhile

!i = 0
while !i < 4
  db !i
  !i #= !i + 1
endwhile

The engine caps a while loop at 10,000 iterations. Define values modified as loop variables are restored after the loop, matching the assembler's scoped expansion behavior. For Asar compatibility, an endif may close an innermost while in legacy source. Use endwhile in new source.

for, endfor

for i = 0..3
  db i
endfor

The range is inclusive and may descend:

for i = 3..0
  db i
endfor

The loop variable is exposed through define resolution during the body and restored afterward. See forloop.asm.

6502 family behavior

Native if / while / for

!Region = 1
if !Region == 1
  db "U"
endif

!i = 0
while !i < 4
  db !i
  !i #= !i + 1
endwhile

for i = 0..3
  db i
endfor

The engine caps a while loop at 10,000 iterations. for ranges are inclusive.

ca65 Dotted Forms

.if, .elseif, .else, .endif, .ifdef, .ifndef lower to the shared conditional engine. CPU predicates .ifp02 … .ifpm740 become if __CA65_CPU_…__.

.repeat count[, ident] / .endrepeat rewrite to an inclusive for from 0 through count:

.repeat 3, I
  .byte I
.endrepeat

That emits $00 $01 $02. The optional name is an ordinary expression during the body.