@ ARM assembly for binary tree search with cdr-coding for @ left-subtree pointers .syntax unified .cpu cortex-m4 .globl tsearch .thumb_func .thumb tsearch: tst r1, r1 beq notfound 1: ldr r2, [r1], #8 @ point to following entry (no postincrement on Thumb-1) cmp r0, r2 beq found blo 1b @ if lower, continue inner loop ldr r1, [r1, -4] @ negative offset, not on Thumb-1 b.n tsearch etsearch: tst r1, r1 beq notfound add r1, r3 @ offset pointer by the file base address 1: ldr r2, [r1], #8 cmp r0, r2 blo 1b beq found ldr r1, [r1, #-4] b etsearch estsearch: sub r4, #7 @ adjust end of file 2: tst r1, r1 beq notfound add r1, r3 1: cmp r1, r4 @ check each pointer against end of file bhs outofbounds @ if pair not inside file, crap out here ldr r2, [r1], #8 cmp r0, r2 blo 1b beq found ldr r1, [r1, #-4] b 2b separator: 2: ldr r1, [r1, #-4] @ right-subtree case: load pointer stsearch: tst r1, r1 @ check pointer for null beq notfound 1: ldr r2, [r1], #8 cmp r0, r2 blo 1b bhi 2b b found sep2: 2: ldr r1, [r1, #-4] zsearch: cbz r1, notfound 1: ldr r2, [r1], #8 cmp r0, r2 blo 1b bhi 2b b found notfound: nop @ Hypothetically dynamically-generated code for a couple of tree nodes: tree: movw r2, #0x38a3 cmp r0, r2 beq.n found bhi 1f @ branch to right subtree movw r2, #0x2023 cmp r0, r2 beq.n found bhi 2f @ branch to right subtree 1: nop 2: nop