Question
Fix this code to make it work with symbols, such as R, S, P, tie, because right now it is giving me an error. Additionally,
Fix this code to make it work with symbols, such as "R", "S", "P", "tie", because right now it is giving me an error. Additionally, please, fix other errors to make the file work.
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 ; invoke syscall
; convert user input to integer
sub eax, '0'
; generate random choice for computer
mov ebx, 3 ; range for random number generator
mov eax, 0 ; clear eax for storing result
mov edx, 0 ; clear edx for storing remainder
mov ecx, 1 ; request 1 random number
int 0x80 ; invoke syscall
; compare user input to computer's choice
cmp eax, ebx
jge _start ; loop if computer's choice is out of range
mov ebx, eax ; store user input in ebx for later use
cmp dword [eax], R ; rock
je rock
cmp dword [eax], P ; paper
je paper
cmp dword [eax], S ; scissors
je scissors
jmp _start ; loop if user input is invalid
rock:
; computer chooses paper, user loses
cmp dword [ebx], P
je lose
; computer chooses scissors, user wins
cmp dword [ebx], S
je win
; otherwise, it's a tie
jmp tie
paper:
; computer chooses scissors, user loses
cmp dword [ebx], S
je lose
; computer chooses rock, user wins
cmp dword [ebx], R
je win
; otherwise, it's a tie
jmp tie
scissors:
; computer chooses rock, user loses
cmp dword [ebx], R
je lose
; computer chooses paper, user wins
cmp dword [ebx], P
je win
; otherwise, it's a tie
jmp tie
win:
; display win message
mov eax, 4 ; syscall for writing to stdout
mov ebx, 1 ; file descriptor for stdout
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;
mov ebx, 1 ; file descriptor for stdout
mov ecx, tie_msg ; address of tie message
mov edx, 6 ; length of draw message
int 0x80 ; interrupt
end:
; exit program
mov eax, 1 ; syscall for exit
xor ebx, ebx ; exit status code
int 0x80 ; interrupt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started