Skip to content

Add complex-app example: loops, pointers, structs, bit shifts/masks#10

Merged
corepunch merged 2 commits into
mainfrom
copilot/add-complex-example-with-functions
Apr 16, 2026
Merged

Add complex-app example: loops, pointers, structs, bit shifts/masks#10
corepunch merged 2 commits into
mainfrom
copilot/add-complex-example-with-functions

Conversation

Copilot AI commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Adds examples/complex-app/ — a richer ARM VM example alongside the existing simple-app, covering the full range of typical embedded/systems code patterns.

New files

  • input.c — C source with three internal functions plus main:
    • compute_flags(value, shift, mask) — register-controlled shift + AND mask
    • sum_array(arr*, count) — counted loop over a pointer/array
    • process_record(record*) — struct-like field access via pointer offsets + bitwise OR
  • complex.s — hand-written ARMv7 assembly (Apple/ARM UAL syntax); pre-generated so it runs on Linux without a cross-compiler
  • main.c — host registering print_int and print_string via avm_register()
  • Makefilemake run uses the checked-in assembly; make all regenerates from input.c on macOS with Xcode

Assembly highlights

@ Register-controlled bit shift + mask
_compute_flags:
    mov r0, r0, lsl r1      @ value << shift
    and r0, r0, r2          @ & mask
    bx lr

@ Loop with scaled-register array indexing
_sum_loop:
    cmp r4, r1
    bge _sum_done
    ldr r2, [r0, r4, lsl #2]   @ arr[i] = *(arr + i*4)
    add r5, r5, r2
    add r4, r4, #1
    b _sum_loop

@ Struct-like field access via immediate offsets
_process_record:
    ldr r1, [r0, #4]        @ record[1] (value)
    ldr r2, [r0, #8]        @ record[2] (flags)
    mov r2, r2, lsl #8
    orr r0, r1, r2
    bx lr

Expected output

Running complex ARM32 program...
compute_flags result: 3840
sum_array result:     150
process_record result: 1834
Program returned: 150

@corepunch

Copy link
Copy Markdown
Owner

@copilot Add also structs in app and pointers to functions in those structs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants