Question: Change this code by getting rid of loops and implementing the code in an easier way, but, most importantly, change the code for it to
Change this code by getting rid of loops and implementing the code in an easier way, but, most importantly, change the code for it to be two players playing RPS with each other, not one player and the computer. Thank you!
section .data
input db 'Enter your choice (R = rock, P = paper, S = scissors): ', 0
win_msg db 'You win!', 0
lose_msg db 'You lose!', 0
tie_msg db "It's a tie!", 0
section .text
global _start
_start: ; display prompt
mov eax, 4 ; syscall for writing to stdout
mov ebx, 1 ; file descriptor for stdout
mov ecx, input ; address of prompt string
mov edx, 46 ; length of prompt string
int 0x80 ; invoke syscall
; read user input
mov eax, 3 ; syscall for reading from stdin
mov ebx, 0 ; file descriptor for stdin
mov ecx, input ; address to store input
mov edx, 1 ; number of bytes to read
int 0x80
; compare user input to computer's choice
cmp byte [input], 'R' ; rock
je rock
cmp byte [input], 'P' ; paper
je paper
cmp byte [input], 'S' ; scissors
je scissors
jmp invalid ; otherwise, input is invalid
rock: ; generate random choice for computer
mov eax, 0 ; clear eax for storing result
mov ebx, 3 ; range for random number generator
mov ecx, 1 ; request 1 random number
int 0x80
; computer chooses paper, user loses
cmp eax, 1
je lose
; computer chooses scissors, user wins
cmp eax, 2
je win
jmp tie ; otherwise, it's a tie
paper: ; generate random choice for computer
mov eax, 0 ; clear eax for storing result
mov ebx, 3 ; range for random number generator
mov ecx, 1 ; request 1 random number
int 0x80
; computer chooses scissors, user loses
cmp eax, 2
je lose
; computer chooses rock, user wins
cmp eax, 0
je win
; otherwise, it's a tie
jmp tie
scissors:
; generate random choice for computer
mov eax, 0 ; clear eax for storing result
mov ebx, 3 ; range for random number generator
mov ecx, 1 ; request 1 random number
int 0x80 ; invoke syscall
; computer chooses rock, user loses
cmp eax, 0
je lose
; computer chooses paper, user wins
cmp eax, 1
je win
; otherwise, it's a tie
jmp tie
win: ; display win message
mov eax, 4
mov ebx, 1
mov ecx, win_msg ; address of win message
mov edx, 8 ; length of win message
int 0x80 ; interrupt
jmp end
lose: ; display lose message
mov eax, 4 ; syscall for writing to stdout
mov ebx, 1 ; file descriptor for stdout
mov ecx, lose_msg ; address of lose message
mov edx, 9 ; length of lose message
int 0x80 ; interrupt
jmp end
tie: ; display tie message
mov eax, 4 ; syscall for writing to stdout
mov ebx, 1 ; file descriptor for stdout
mov ecx, tie_msg ; address of tie message
mov edx, 10 ; length of tie message
int 0x80 ; interrupt
jmp end
invalid: ; display invalid message
mov eax, 4 ; syscall for writing to stdout
mov ebx, 1 ; file descriptor for stdout
; compare user input to options
cmp dword [ecx], "R"
je rock_selected
cmp dword [ecx], "P"
je paper_selected
cmp dword [ecx], "S"
je scissors_selected
cmp dword [ecx], "tie"
je tie_selected
; if input is not recognized, display error message and loop
mov ecx, error_msg
mov edx, 12
int 0x80
jmp _start
rock_selected:
; Display that rock has been selected
mov ecx, rock_msg
mov edx, 9
int 0x80
jmp _exit
paper_selected:
; Display that paper has been selected
mov ecx, paper_msg
mov edx, 10
int 0x80
jmp _exit
scissors_selected:
; Display that scissors has been selected
mov ecx, scissors_msg
mov edx, 12
int 0x80
jmp _exit
tie_selected:
; Display that it's a tie
mov ecx, tie_msg
mov edx, 11
int 0x80
jmp _exit
_exit: ; exit program
mov eax, 1 ; syscall for exit
xor ebx, ebx ; exit status code
int 0x80
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
