ARM64 assembly on macOS — why doesn’t my scanf + printf loop work?
16:16 06 Nov 2025

I ve been learning Assembly recently and I stumbled upon a problem in my code where I am trying to take user input (an integer) and print from 1 to n-1. I cant seem to find much info on this topic for arm64.

.global _main
.align 4

_main: 


ADRP X10, num@PAGE 
ADD X10, X10,num@PAGEOFF 
LDR X15, [X10]
STR X15, [SP, #-16]!
MOV X1,#1

loop:
cmp X1,X15
bgt exit

ADRP X0, output_format@PAGE 
ADD X0,X0,output_format@PAGEOFF


BL _printf
add X1,X1,#1
b loop

exit:
mov X16, #1
svc 0


read_from_keyboard:
    STP X29, X30, [SP, #-16]!
    ADRP    X0, input_format@PAGE
    ADD X0, X0, input_format@PAGEOFF
    ADRP    X11, num@PAGE
    ADD X11, X11, num@PAGEOFF
    STR X11, [SP, #-16]!
    BL _scanf
    ADD SP, SP, #16

    LDP X29, X30, [SP], #16
    ret

.data 
.balign 4
num:    .word 32
.balign 4
input_format:   .asciz "%d"
.balign 4
output_format: .asciz "%d\n"

When I run the program and enter an integer, the program immediately stops.
assembly arm64