BNE is one of the 14 condition-code branches in the ARM/Thumb instruction set.
It is executed only when the Z (Zero) flag is clear (Z = 0) in the APSR/CPSR, i.e. when the last arithmetic/compare operation did not produce equality.
1. Quick reference
Item
Thumb-1 (Cortex-M0/M0+)
Thumb-2 (M3/M4/M7/…)
Mnemonic (UAL)
BNE label or B.NE label
Condition tested
Z == 0 (not equal)
Encoding size
16-bit (always present)
16-bit or 32-bit
Branch range (16-bit form)
±256 bytes (±128×2) from the instruction after BNE
Cycles on M0+
1 S + 1 I (pipeline flush) if taken, 1 S if not
(“S = fetch stage, I = execution stage” for the simple 2-stage Cortex-M0+ pipeline.)
2. How the condition flag is set
BNE itself never modifies flags; it only inspects Z.
Typical instruction that sets Z:
assembly
CMP r0, r1 ; subtract r1 from r0, update N Z C V
TST r2, r3 ; AND r2 and r3, update N Z
SUBS r4, r4, #1 ; ALU ops with the “S” suffix also update flags
If the result of the operation is zero, Z = 1 → BEQ would branch, BNE would not.
If the result is non-zero, Z = 0 → BNE branches.
3. Typical usage patterns
assembly
CMP r0, r1 ; compare two registers
BNE not_equal ; jump if they differ
; ...code executed only when r0 == r1...
equal:
; fall-through
not_equal:
; ...code executed only when r0 != r1...
Therefore the jump span is –256 … +254 bytes from the instruction after the BNE.
On cores that support Thumb-2, the assembler can emit a 32-bit long-branch form automatically when the label is out of 16-bit range; the programmer doesn’t need to care.
5. Relationship to other branch mnemonics
Condition
Mnemonic
Flag test
equal
BEQ / B.EQ
Z = 1
not equal
BNE / B.NE
Z = 0
greater-than, unsigned
BHI
C = 1 and Z = 0
etc.
A full table of the 14 conditions is in any ARM reference manual.
6. Tips & gotchas
On Cortex-M0/M0+, all conditional branches are always 16-bit; watch the ±256-byte limitation when hand-coding.
The assembler treats BNE and B.NE identically; the dot-form just emphasises the unified syntax (B.<cond>).
If you only need to branch on a register being zero/non-zero, Thumb-2 gives CBZ / CBNZ, which are smaller/faster than CMP reg,#0 + Bxx, but these do not exist on the M0/M0+.
TL;DR
BNE label in Thumb assembly means “branch to label if the previous operation did not produce zero.”
It checks the Zero flag (Z = 0), is encoded in 16 bits on a Cortex-M0+, and can reach any label within ±256 bytes.
User
what is VTOR for raspberry pi pico?
Assistant
VTOR = Vector Table Offset Register
Peripheral group = SCB (System-Control Block, inside the NVIC)
Address = 0xE000 ED08 (in the PPB memory region)
Width = 32 bits (read / write)
Reset value = 0x0000 0000 (vector table starts at 0x0000 0000 after POR)
What it does
The Cortex-M processor looks at the address stored in VTOR to find the exception / interrupt vector table.
By writing a new (properly aligned) base address into VTOR you can relocate the vector table, e.g. from the Boot-ROM/Flash to RAM so that you can patch handlers at run-time or give each core its own table.
Alignment rules on the RP2040 (Cortex-M0+)
• The bottom 7 bits of VTOR are “Implementation Defined” and are ignored.
• Therefore the address written to VTOR must be 128-byte aligned (bit [6:0] = 0).
• The SDK aligns the table to 256 bytes (__attribute__((aligned(256)))) which is also valid.
Two cores, two VTORs
Each of the RP2040’s cores has its own SCB block, so you can relocate the tables independently:
After power-on the core starts in the Boot ROM mapped at 0x0000 0000, VTOR = 0.
The 1st-stage boot copies a 2nd-stage loader into SRAM and executes it.
The SDK’s start-up code copies the default vector table from Flash to SRAM (vector_table_in_ram) and sets
c
SCB->VTOR = (uint32_t)vector_table_in_ram; // now IRQ handlers are writable
Your application can later patch individual entries (irq_set_exclusive_handler() etc.).
Practical uses
• Install fast, RAM-resident handlers (e.g. USB, PIO, DMA)
• Redirect exceptions during a boot-loader / application switch
• Give core-1 its own table while core-0 keeps the default one
Summary
On the Raspberry Pi Pico (RP2040) VTOR is fully present and writable; just remember the 128-byte alignment requirement when moving your vector table to RAM or elsewhere.
User
how can i execute code from flash memory without copying it to sram with raspberry pi pico?
Assistant
Running code straight from the external Q-SPI flash (a.k.a. “execute-in-place” / XIP) is actually the normal operating mode of the RP2040 – nothing special has to be done.
Nevertheless, there are a few practical details worth knowing, especially if you are trying to:
• make sure the linker keeps a certain function in flash,
• jump to code you put at a non-default flash offset (e.g. a 2nd-stage boot-loader / OTA image), or
• understand the limitations when the flash is busy.
Below is a concise “how-to” that covers all of those angles.
“Plain” Pico program – it already runs from flash
────────────────────────────────────────────────────────────────────────────
CMakeLists.txt with the official SDK:
The CPU fetches instructions directly from the flash window; the cache in the XIP block hides much of the latency.
────────────────────────────────────────────────────────────────────────────
2. Making sure a function stays in flash
────────────────────────────────────────────────────────────────────────────
The SDK supplies the opposite attribute – __not_in_flash_func() – used when you need a routine in RAM (e.g. to execute while the flash is being erased).
If you never apply that attribute, the function will be linked in flash and executed there.
c
void my_flash_code(void) // ← executed directly from flash
{
...
}
__not_in_flash_func(void my_ram_code)(void)
{ // ← copied to SRAM at start-up
...
}
(The alias forms NOT_IN_FLASH() and not_in_flash_func() do the same.)
────────────────────────────────────────────────────────────────────────────
3. Jumping to other flash regions (boot-loader / OTA image)
────────────────────────────────────────────────────────────────────────────
The XIP window exposes up to 16 MiB of the Q-SPI device:
code
0x1000_0000 ‑- first byte of flash
0x1080_0000 ‑- 8 MiB
0x10FF_FFFF ‑- last byte of 16 MiB window
If you, for example, place a 2nd program at flash offset 0x0010 0000
(→ XIP address 0x1010 0000) you can start it like this:
• Make sure the 2nd image is linked with flash-address awareness (link-script –Tlinker_script.ld, or in the SDK use pico_set_binary_type(foo copy_to_ram), etc.).
• The vector table must be 128-byte aligned because of the VTOR rule.
────────────────────────────────────────────────────────────────────────────
4. The one time you cannot execute from flash
────────────────────────────────────────────────────────────────────────────
While the Q-SPI flash is in erase or program mode, the XIP port is busy → any attempt to fetch instructions will stall the core.
The usual pattern is therefore:
c
__not_in_flash_func(void flash_write_block)(...)
{
// run from RAM
// 1. put flash in program mode
// 2. send data
// 3. wait for WIP bit to clear
}
Outside of those operations you can run from flash at will.
────────────────────────────────────────────────────────────────────────────
5. Quick checklist
────────────────────────────────────────────────────────────────────────────
✓ Build your firmware with the default SDK rules → code is XIP.
✓ Do not mark a function with not_in_flash_func unless you want it in RAM.
✓ To jump to code at another flash offset: calculate the XIP address, load SP & PC from its vector table, and write VTOR if you need interrupts.
✓ Remember that flash is unavailable while it is being written/erased – move only those helper routines to RAM.
That’s all that’s required: the RP2040’s hardware lets you “execute in place” from Q-SPI flash automatically, so keeping code out of SRAM is, by default, already taken care of.