Created
November 16, 2025 19:23
-
-
Save m-doughty/ac631281cafec0558fad7747d7c23eda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .global _main | |
| .align 2 | |
| .data | |
| random_path: .asciz "/dev/urandom" | |
| guess_msg: .ascii "GUESS A NUMBER (0-9): " | |
| guess_len = . - guess_msg | |
| equal_msg: .ascii "CORRECT - YOU WIN\n" | |
| equal_len = . - equal_msg | |
| lower_msg: .ascii "TRY AGAIN (HINT: LOWER): " | |
| lower_len = . - lower_msg | |
| higher_msg: .ascii "TRY AGAIN (HINT: HIGHER): " | |
| higher_len = . - higher_msg | |
| error_msg: .ascii "MUST INPUT 0-9: " | |
| error_len = . - error_msg | |
| buffer: .space 2 | |
| .text | |
| _main: | |
| ; Open /dev/urandom | |
| adrp x0, random_path@PAGE | |
| add x0, x0, random_path@PAGEOFF | |
| mov x1, #0 | |
| mov x2, #0 | |
| mov x16, #5 ; open | |
| svc #0x80 | |
| mov x19, x0 | |
| ; Read 1 byte | |
| mov x0, x19 | |
| adrp x1, buffer@PAGE | |
| add x1, x1, buffer@PAGEOFF | |
| mov x2, #1 ; 1 byte | |
| mov x16, #3 ; read | |
| svc #0x80 | |
| ; Reload the buffer address (clobbered by system call) | |
| adrp x1, buffer@PAGE | |
| add x1, x1, buffer@PAGEOFF | |
| ; Get byte & modulo 10 | |
| ldrb w20, [x1] | |
| mov w21, #10 | |
| udiv w22, w20, w21 ; w22 = w20 / 10 | |
| msub w24, w22, w21, w20 ; w24 = w20 - (w22 * 10) = remainder | |
| ; Close our file handle | |
| mov x0, x19 | |
| mov x16, #6 ; close | |
| svc #0x80 | |
| ; Prompt for a guess | |
| 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 | |
| GameLoop: | |
| ; Read the player's guess | |
| 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 w23, [x1] ; load the first byte | |
| sub w23, w23, #48 ; subtract 48 (ASCII 0) | |
| ; Ensure input is within range 0-9 | |
| cmp w23, #0 | |
| b.lt OutOfRange | |
| cmp w23, #9 | |
| b.gt OutOfRange | |
| cmp w23, w24 ; compare user input to our random number | |
| b.lt Higher ; prompt the user to guess higher | |
| b.gt Lower ; prompt the user to guess lower | |
| b Winner ; user guessed correctly | |
| PrintAndLoop: | |
| mov x16, #4 ; write | |
| svc #0x80 | |
| b GameLoop | |
| 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 | |
| b PrintAndLoop | |
| Higher: | |
| ; Prompt the player to guess higher | |
| mov x0, #1 ; stdout | |
| adrp x1, higher_msg@PAGE | |
| add x1, x1, higher_msg@PAGEOFF | |
| mov x2, higher_len | |
| b PrintAndLoop | |
| Lower: | |
| ; Prompt the player to guess lower | |
| mov x0, #1 ; stdout | |
| adrp x1, lower_msg@PAGE | |
| add x1, x1, lower_msg@PAGEOFF | |
| mov x2, lower_len | |
| b PrintAndLoop | |
| Winner: | |
| ; Print a winner message | |
| mov x0, #1 ; stdout | |
| adrp x1, equal_msg@PAGE | |
| add x1, x1, equal_msg@PAGEOFF | |
| mov x2, equal_len | |
| mov x16, #4 ; write | |
| svc #0x80 | |
| ; End the game | |
| mov x0, #0 | |
| mov x16, #1 | |
| svc #0x80 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment