🎮
Assembly for the SNES
  • Introduction
  • Getting started
  • Contributing
  • The fundamentals
    • Hexadecimal
    • Binary
    • The SNES memory
    • The SNES registers
    • Addressing modes
    • Little-endian
    • Glossary
  • The basics
    • Loading and storing
    • Shorter addresses
    • 8-bit and 16-bit mode
    • Comparing, branching, labels
    • Jumping to subroutines
  • Collection of values
    • Tables and indexing
    • The stack
    • Copying data
  • Processor flags and registers
    • The processor flags
    • Changing the processor flags
    • Transfers
    • Stack pointer register
  • Mathemathics and logic
    • Arithmetic operations
    • Bit shifting operations
    • Bitwise operations
    • Hardware math
  • Deep dives
    • Addressing modes revisted
    • Miscellaneous opcodes
    • Machine cycles
    • Hardware vectors
    • Techniques
    • Common assembler syntax
    • Programming cautions
Powered by GitBook
On this page

Was this helpful?

  1. Deep dives

Machine cycles

PreviousMiscellaneous opcodesNextHardware vectors

Last updated 3 years ago

Was this helpful?

The SNES processes instructions, but each instruction takes up a predetermined amount of time to execute. The time an instruction takes to execute is called "machine cycle" (or "cycle" in short).

Each instruction has its own cycle. has a full reference of how many cycles each instruction takes. Pay attention to the footnotes, as the amount of used cycles can differ depending on the context of the code. For example, a taken branch takes 1 cycle longer compared to a branch that's not taken.

The less cycles, the less slowdown the code suffers from. Slowdown is often noticeable in games with many sprites on the screen. To avoid slowdown, you need to write efficient code. Here is an example of inefficient vs. efficient code:

; Inefficient
LDA #$00           ; 2 cycles
STA $7E0000        ; 5 cycles
                   ; = 7 cycles
; Efficient
LDA #$00           ; 2 cycles
STA $00            ; 3 cycles
                   ; = 5 cycles

; Very efficient
STZ $00            ; 3 cycles
                   ; = 3 cycles

At first, we use a full notation to write the value $00 to address $7E0000. But then, in the next example, we shorten the address, saving 2 cycles. Finally, we figure that we can use STZ, as we store zero to an address anyway.

Having a low cycle count is especially important when executing code during an NMI, because there are limited machine cycles there. Exceeding that limit causes black scanlines to flicker on the top of the SNES display.

This page