Skip to content

Instantly share code, notes, and snippets.

@m-doughty
Created November 19, 2025 11:09
Show Gist options
  • Select an option

  • Save m-doughty/53c03b78239423f374ccc160f4bf8fd2 to your computer and use it in GitHub Desktop.

Select an option

Save m-doughty/53c03b78239423f374ccc160f4bf8fd2 to your computer and use it in GitHub Desktop.
.global _main
.align 2
.data
guess_msg: .ascii "INPUT A NUMBER (0-9):"
guess_len = . - guess_msg
equal_msg: .ascii "BOTH NUMBERS ARE EQUAL!\n"
equal_len = . - equal_msg
lower_msg: .ascii "FIRST NUMBER IS HIGHER!\n"
lower_len = . - lower_msg
higher_msg: .ascii "SECOND_NUMBER IS HIGHER!\n"
higher_len = . - higher_msg
error_msg: .ascii "INVALID INPUT! MUST INPUT 0-9!\n"
error_len = . - error_msg
buffer: .space 2
.text
_main:
mov x20, #0
mov x21, #0
mov x22, #0
ReadNumber:
; Prompt for a number
mov x0, #1 ; stdout
adrp x1, guess_msg@PAGE
add x1, x1, guess_msg@PAGEOFF
mov x2, guess_len
mov x16, #4 ; write
svc #0x80
; Read the number
mov x0, #0 ; stdin
adrp x1, buffer@PAGE
add x1, x1, buffer@PAGEOFF
mov x2, #2 ; read digit & newline
mov x16, #3 ; read
svc #0x80
; Reload buffer address, clobbered by syscall
adrp x1, buffer@PAGE
add x1, x1, buffer@PAGEOFF
; Convert input to digit
ldrb w20, [x1] ; load the first byte
sub w20, w20, #48 ; subtract 48 (ASCII 0)
; Ensure input is within range 0-9
cmp w20, #0
b.lt OutOfRange
cmp w20, #9
b.gt OutOfRange
add x22, x22, #1
cmp x22, #2 ; do we have 2 numbers?
b.eq Compare
mov w21, w20 ; put our first number in x21 so 2nd run
; doesn't clobber it
b ReadNumber
Compare:
cmp w20, w21 ; compare user input to our random number
b.lt Lower ; prompt the user to guess higher
b.gt Higher ; prompt the user to guess lower
b Equal
Higher:
; Second number is higher
adrp x1, higher_msg@PAGE
add x1, x1, higher_msg@PAGEOFF
mov x2, higher_len
b PrintAndExit
Lower:
; First number is higher
adrp x1, lower_msg@PAGE
add x1, x1, lower_msg@PAGEOFF
mov x2, lower_len
b PrintAndExit
Equal:
; Numbers Are Equal
adrp x1, equal_msg@PAGE
add x1, x1, equal_msg@PAGEOFF
mov x2, equal_len
PrintAndExit:
mov x0, #1 ; stdout
mov x16, #4 ; write
svc #0x80
; Exit with status 0
mov x0, #0
mov x16, #1
svc #0x80
OutOfRange:
; Prompt the user for a valid input
mov x0, #1 ; stdout
adrp x1, error_msg@PAGE
add x1, x1, error_msg@PAGEOFF
mov x2, error_len
mov x16, #4 ; write
svc #0x80
b ReadNumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment