fork download
  1. section .data
  2. intro_msg db "Welcome to the Number Guessing Game!", 10, 0
  3. too_high_msg db "Your guess is too high!", 10, 0
  4. too_low_msg db "Your guess is too low!", 10, 0
  5. win_msg db "Congratulations! You guessed correctly!", 10, 0
  6. lose_msg db "You've run out of guesses. You lose!", 10, 0
  7.  
  8. section .bss
  9. guess resb 4 ; space to store the user's guess
  10. rand_num resb 4 ; space to store the random number
  11. guesses_left resb 4 ; space to store remaining guesses
  12.  
  13. section .text
  14. global _start
  15.  
  16. _start:
  17. ; Step 1: Display the introductory message
  18. mov rax, 1 ; syscall for write
  19. mov rdi, 1 ; file descriptor (stdout)
  20. mov rsi, intro_msg ; pointer to the message
  21. mov rdx, 40 ; length of the message
  22. syscall
  23.  
  24. ; Step 2: Generate a random number between 1 and 100
  25. mov rax, 318 ; syscall for getrandom
  26. mov rdi, rand_num ; buffer to store random number
  27. mov rsi, 4 ; size of the random number (32 bits)
  28. mov rdx, 0 ; flags
  29. syscall
  30.  
  31. ; Limit the random number to between 1 and 100
  32. mov eax, [rand_num] ; load the random number
  33. and eax, 0x63 ; limit to 0-99 (0x63 mask)
  34. add eax, 1 ; make it 1-100
  35. mov [rand_num], eax ; store the adjusted random number
  36.  
  37. ; Step 3: Initialize guesses_left and start guessing loop
  38. mov dword [guesses_left], 6 ; initialize number of guesses
  39.  
  40. guess_loop:
  41. ; Check if the user is out of guesses
  42. cmp dword [guesses_left], 0
  43. je out_of_guesses ; if no guesses left, go to lose
  44.  
  45. ; Ask the user for a guess (For simplicity, assume guess is loaded into [guess])
  46. ; Simulate user input here for simplicity; in real code, this would use syscalls
  47. ; mov eax, 50 ; Replace 50 with actual input capture mechanism
  48. ; mov [guess], eax
  49.  
  50. ; Step 4 & 5: Check if the guess is too high or too low
  51. mov eax, [guess] ; load the user's guess
  52. cmp eax, [rand_num] ; compare the guess to the random number
  53. jg too_high ; if guess > rand_num, jump to too_high
  54. jl too_low ; if guess < rand_num, jump to too_low
  55.  
  56. ; Step 6: If guess is correct
  57. mov rax, 1 ; syscall for write
  58. mov rdi, 1 ; file descriptor (stdout)
  59. mov rsi, win_msg ; pointer to the winning message
  60. mov rdx, 42 ; length of the message
  61. syscall
  62. jmp end_game
  63.  
  64. too_high:
  65. ; If the guess is too high, display a message
  66. mov rax, 1 ; syscall for write
  67. mov rdi, 1 ; file descriptor (stdout)
  68. mov rsi, too_high_msg ; pointer to the message
  69. mov rdx, 22 ; length of the message
  70. syscall
  71. jmp after_guess
  72.  
  73. too_low:
  74. ; If the guess is too low, display a message
  75. mov rax, 1 ; syscall for write
  76. mov rdi, 1 ; file descriptor (stdout)
  77. mov rsi, too_low_msg ; pointer to the message
  78. mov rdx, 21 ; length of the message
  79. syscall
  80. jmp after_guess
  81.  
  82. after_guess:
  83. ; Step 7: Decrement guesses_left and loop
  84. dec dword [guesses_left] ; decrease remaining guesses
  85. jmp guess_loop ; go back to guess loop
  86.  
  87. out_of_guesses:
  88. ; If no guesses are left, display a losing message
  89. mov rax, 1 ; syscall for write
  90. mov rdi, 1 ; file descriptor (stdout)
  91. mov rsi, lose_msg ; pointer to the losing message
  92. mov rdx, 37 ; length of the message
  93. syscall
  94.  
  95. end_game:
  96. ; Exit the program
  97. mov rax, 60 ; syscall for exit
  98. xor rdi, rdi ; exit code 0
  99. syscall
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Welcome to the Number Guessing Game!
YoYour guess is too lowYour guess is too lowYour guess is too lowYour guess is too lowYour guess is too lowYour guess is too lowYou've run out of guesses. You lose!