Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @main.s
- .global main
- .extern generate_key
- .extern get_guess
- .extern find_correct
- .extern find_correct_color
- .extern print_game
- .extern printf
- .extern exit
- .data
- key: .word 0 @ 28-bit packed secret code
- guess: .word 0 @ 28-bit packed user guess
- msg_congrats: .asciz "\n Congratulations! You've cracked the code in %d tries!\n"
- msg_fail: .asciz "\n Sorry! You've used all 10 tries without cracking the code.\n"
- msg_reveal: .asciz "Secret Code: %c %c %c %c\n"
- .text
- main:
- @ Generate the secret key
- bl generate_key
- @ Load key for printing
- ldr r0, =key
- ldr r1, [r0]
- @ Extract each 7-bit character and print the key (debug only)
- mov r2, r1, lsr #21
- and r2, r2, #0x7F
- mov r3, r1, lsr #14
- and r3, r3, #0x7F
- mov r4, r1, lsr #7
- and r4, r4, #0x7F
- and r5, r1, #0x7F
- ldr r0, =msg_reveal
- mov r1, r2
- mov r2, r3
- mov r3, r4
- mov r4, r5
- bl printf
- mov r10, #1 @ guess counter (r10 = round number)
- game_loop:
- @ Prompt and get user guess
- bl get_guess
- @ Store the guess from r0
- ldr r1, =guess
- str r0, [r1]
- @ Load key and guess again
- ldr r1, =key
- ldr r1, [r1]
- ldr r2, =guess
- ldr r2, [r2]
- @ Check exact matches (correct colors in correct positions)
- mov r0, r1 @ r0 = key
- mov r1, r2 @ r1 = guess
- bl find_correct @ returns correct positions in r0
- mov r6, r0 @ save to r6
- @ Check color-only matches (correct color, wrong position)
- mov r0, r1 @ r0 = key
- mov r1, r2 @ r1 = guess
- bl find_correct_color @ returns misplaced matches in r0
- mov r7, r0 @ save to r7
- @ Print guess and feedback
- mov r0, r10 @ round number
- ldr r1, =guess
- ldr r1, [r1] @ packed guess
- mov r2, r6 @ correct positions
- mov r3, r7 @ correct colors wrong place
- bl print_game
- @ Check win condition: if r6 == 4
- cmp r6, #4
- beq win_game
- @ Check if max 10 rounds reached
- cmp r10, #10
- beq fail_game
- @ Increment round and loop
- add r10, r10, #1
- b game_loop
- win_game:
- ldr r0, =msg_congrats
- mov r1, r10
- bl printf
- b end_program
- fail_game:
- ldr r0, =msg_fail
- bl printf
- end_program:
- mov r0, #0
- bl exit
- .section .note.GNU-stack,"",%progbits @ Marks stack as non-executable
- @print_game.s
- .global print_game
- print_game:
- // r0 = 32-bit packed value
- mov r1, #0
- print_loop:
- and r2, r0, #0x7F
- bl putchar // print r2
- mov r3, #' '
- bl putchar
- lsr r0, r0, #7
- add r1, r1, #1
- cmp r1, #4
- blt print_loop
- bx lr
- .section .note.GNU-stack,"",%progbits @ Marks stack as non-executable
- @find_correct.s .global find_correct
- find_correct:
- // r0 = guess, r1 = key
- mov r2, #0
- mov r3, #0
- loop_correct:
- and r4, r0, #0x7F // get 7 bits
- and r5, r1, #0x7F
- cmp r4, r5
- addeq r2, r2, #1
- lsr r0, r0, #7
- lsr r1, r1, #7
- add r3, r3, #1
- cmp r3, #4
- blt loop_correct
- mov r0, r2
- bx lr
- .section .note.GNU-stack,"",%progbits @ Marks stack as non-executable
- @find_correct_color.s .global find_correct_color
- find_correct_color:
- // r0 = guess, r1 = key
- mov r2, #0
- mov r3, #0
- loop_correct:
- and r4, r0, #0x7F // get 7 bits
- and r5, r1, #0x7F
- cmp r4, r5
- addeq r2, r2, #1
- lsr r0, r0, #7
- lsr r1, r1, #7
- add r3, r3, #1
- cmp r3, #4
- blt loop_correct
- mov r0, r2
- bx lr
- .section .note.GNU-stack,"",%progbits @ Marks stack as non-executable
- @generate_key.s .global generate_key
- // ASCII color encoding
- .equ RED, 117 // 'r'
- .equ BLUE, 98 // 'b'
- .equ GREEN, 103 // 'g'
- .equ WHITE, 119 // 'w'
- .equ YELLOW, 121 // 'y'
- .equ PURPLE, 112 // 'p'
- generate_key:
- // r0 = address to store packed key
- mov r1, #0 // accumulator
- mov r2, #0 // shift amount
- loop:
- // Generate random value between 0 and 5
- bl rand
- mov r3, r0
- and r3, r3, #0x07 // ensure 0–7 range
- cmp r3, #5
- bgt loop // retry if > 5
- ldr r4, =color_table
- ldrb r5, [r4, r3] // load ASCII color
- lsl r5, r5, r2
- orr r1, r1, r5 // add to packed register
- add r2, r2, #7
- cmp r2, #28
- blt loop
- str r1, [r0] // store packed key
- bx lr
- .data
- color_table:
- .byte RED, BLUE, GREEN, WHITE, YELLOW, PURPLE
- .section .note.GNU-stack,"",%progbits @ Marks stack as non-executable
- @get-guess.s .global get_guess
- get_guess:
- mov r1, #0 // packed result
- mov r2, #0 // shift amount
- read_loop:
- bl getchar
- lsl r0, r0, r2 // shift input char left
- orr r1, r1, r0 // accumulate in r1
- lsr r0, r1, r2 // restore r0
- add r2, r2, #7
- cmp r2, #28
- blt read_loop
- mov r0, r1 // return packed guess in r0
- bx lr
- .section .note.GNU-stack,"",%progbits @ Marks stack as non-executable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement