Advertisement
Guest User

Untitled

a guest
Apr 16th, 2025
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 5.10 KB | None | 0 0
  1. @main.s
  2. .global main
  3. .extern generate_key
  4. .extern get_guess
  5. .extern find_correct
  6. .extern find_correct_color
  7. .extern print_game
  8. .extern printf
  9. .extern exit
  10.  
  11. .data
  12. key:        .word 0               @ 28-bit packed secret code
  13. guess:      .word 0               @ 28-bit packed user guess
  14.  
  15. msg_congrats: .asciz "\n Congratulations! You've cracked the code in %d tries!\n"
  16. msg_fail:     .asciz "\n Sorry! You've used all 10 tries without cracking the code.\n"
  17. msg_reveal:   .asciz "Secret Code: %c %c %c %c\n"
  18.  
  19. .text
  20. main:
  21.     @ Generate the secret key
  22.     bl generate_key
  23.  
  24.     @ Load key for printing
  25.     ldr r0, =key
  26.     ldr r1, [r0]
  27.  
  28.     @ Extract each 7-bit character and print the key (debug only)
  29.     mov r2, r1, lsr #21
  30.     and r2, r2, #0x7F
  31.  
  32.     mov r3, r1, lsr #14
  33.     and r3, r3, #0x7F
  34.  
  35.     mov r4, r1, lsr #7
  36.     and r4, r4, #0x7F
  37.  
  38.     and r5, r1, #0x7F
  39.  
  40.     ldr r0, =msg_reveal
  41.     mov r1, r2
  42.     mov r2, r3
  43.     mov r3, r4
  44.     mov r4, r5
  45.     bl printf
  46.  
  47.     mov r10, #1              @ guess counter (r10 = round number)
  48.  
  49. game_loop:
  50.     @ Prompt and get user guess
  51.     bl get_guess
  52.  
  53.     @ Store the guess from r0
  54.     ldr r1, =guess
  55.     str r0, [r1]
  56.  
  57.     @ Load key and guess again
  58.     ldr r1, =key
  59.     ldr r1, [r1]
  60.     ldr r2, =guess
  61.     ldr r2, [r2]
  62.  
  63.     @ Check exact matches (correct colors in correct positions)
  64.     mov r0, r1        @ r0 = key
  65.     mov r1, r2        @ r1 = guess
  66.     bl find_correct   @ returns correct positions in r0
  67.     mov r6, r0        @ save to r6
  68.  
  69.     @ Check color-only matches (correct color, wrong position)
  70.     mov r0, r1        @ r0 = key
  71.     mov r1, r2        @ r1 = guess
  72.     bl find_correct_color  @ returns misplaced matches in r0
  73.     mov r7, r0        @ save to r7
  74.  
  75.     @ Print guess and feedback
  76.     mov r0, r10       @ round number
  77.     ldr r1, =guess
  78.     ldr r1, [r1]      @ packed guess
  79.     mov r2, r6        @ correct positions
  80.     mov r3, r7        @ correct colors wrong place
  81.     bl print_game
  82.  
  83.     @ Check win condition: if r6 == 4
  84.     cmp r6, #4
  85.     beq win_game
  86.  
  87.     @ Check if max 10 rounds reached
  88.     cmp r10, #10
  89.     beq fail_game
  90.  
  91.     @ Increment round and loop
  92.     add r10, r10, #1
  93.     b game_loop
  94.  
  95. win_game:
  96.     ldr r0, =msg_congrats
  97.     mov r1, r10
  98.     bl printf
  99.     b end_program
  100.  
  101. fail_game:
  102.     ldr r0, =msg_fail
  103.     bl printf
  104.  
  105. end_program:
  106.     mov r0, #0
  107.     bl exit
  108.  
  109.  
  110. .section .note.GNU-stack,"",%progbits  @ Marks stack as non-executable
  111.  
  112. @print_game.s
  113.  
  114. .global print_game
  115. print_game:
  116.     // r0 = 32-bit packed value
  117.     mov r1, #0
  118. print_loop:
  119.     and r2, r0, #0x7F
  120.     bl putchar     // print r2
  121.     mov r3, #' '
  122.     bl putchar
  123.     lsr r0, r0, #7
  124.     add r1, r1, #1
  125.     cmp r1, #4
  126.     blt print_loop
  127.     bx lr
  128.  
  129. .section .note.GNU-stack,"",%progbits  @ Marks stack as non-executable
  130.  
  131. @find_correct.s .global find_correct
  132. find_correct:
  133.     // r0 = guess, r1 = key
  134.     mov r2, #0
  135.     mov r3, #0
  136.  
  137. loop_correct:
  138.     and r4, r0, #0x7F      // get 7 bits
  139.     and r5, r1, #0x7F
  140.     cmp r4, r5
  141.     addeq r2, r2, #1
  142.  
  143.     lsr r0, r0, #7
  144.     lsr r1, r1, #7
  145.     add r3, r3, #1
  146.     cmp r3, #4
  147.     blt loop_correct
  148.  
  149.     mov r0, r2
  150.     bx lr
  151.  
  152. .section .note.GNU-stack,"",%progbits  @ Marks stack as non-executable
  153.  
  154. @find_correct_color.s .global find_correct_color
  155. find_correct_color:
  156.     // r0 = guess, r1 = key
  157.     mov r2, #0
  158.     mov r3, #0
  159.  
  160. loop_correct:
  161.     and r4, r0, #0x7F      // get 7 bits
  162.     and r5, r1, #0x7F
  163.     cmp r4, r5
  164.     addeq r2, r2, #1
  165.  
  166.     lsr r0, r0, #7
  167.     lsr r1, r1, #7
  168.     add r3, r3, #1
  169.     cmp r3, #4
  170.     blt loop_correct
  171.  
  172.     mov r0, r2
  173.     bx lr
  174.  
  175. .section .note.GNU-stack,"",%progbits  @ Marks stack as non-executable
  176.  
  177. @generate_key.s .global generate_key
  178.  
  179. // ASCII color encoding
  180. .equ RED,     117     // 'r'
  181. .equ BLUE,    98      // 'b'
  182. .equ GREEN,   103     // 'g'
  183. .equ WHITE,   119     // 'w'
  184. .equ YELLOW,  121     // 'y'
  185. .equ PURPLE,  112     // 'p'
  186.  
  187. generate_key:
  188.     // r0 = address to store packed key
  189.     mov r1, #0          // accumulator
  190.     mov r2, #0          // shift amount
  191.  
  192. loop:
  193.     // Generate random value between 0 and 5
  194.     bl rand
  195.     mov r3, r0
  196.     and r3, r3, #0x07   // ensure 07 range
  197.     cmp r3, #5
  198.     bgt loop            // retry if > 5
  199.  
  200.     ldr r4, =color_table
  201.     ldrb r5, [r4, r3]   // load ASCII color
  202.     lsl r5, r5, r2
  203.     orr r1, r1, r5      // add to packed register
  204.  
  205.     add r2, r2, #7
  206.     cmp r2, #28
  207.     blt loop
  208.  
  209.     str r1, [r0]        // store packed key
  210.     bx lr
  211.  
  212. .data
  213. color_table:
  214.     .byte RED, BLUE, GREEN, WHITE, YELLOW, PURPLE
  215.  
  216. .section .note.GNU-stack,"",%progbits  @ Marks stack as non-executable
  217.  
  218. @get-guess.s .global get_guess
  219. get_guess:
  220.     mov r1, #0      // packed result
  221.     mov r2, #0      // shift amount
  222.  
  223. read_loop:
  224.     bl getchar
  225.     lsl r0, r0, r2  // shift input char left
  226.     orr r1, r1, r0  // accumulate in r1
  227.     lsr r0, r1, r2  // restore r0
  228.     add r2, r2, #7
  229.     cmp r2, #28
  230.     blt read_loop
  231.  
  232.     mov r0, r1      // return packed guess in r0
  233.     bx lr
  234.  
  235.  
  236. .section .note.GNU-stack,"",%progbits  @ Marks stack as non-executable
  237.  
  238.  
  239.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement