Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Work on this portion of the assignment individually. Nobody else should see your

ID: 3785863 • Letter: W

Question

Work on this portion of the assignment individually. Nobody else should see your code. You may discuss high-level strategies as in other programming courses. You are to write a program in MIPS assembly for use in the SPIM simulator. The program will implement a single player guessing game. The program will ask the player try to guess the secret number. The program will output a message to indicate if the number guessed is too high or too low, and ask the player to enter a new number. If the number guessed by the player matches the secret number, a congratulatory message will be displayed, followed by the number of tries it took the player to guess the secret number. The program should end at this point. Your code must be well-commented to receive full credit. This means any nontrivial line of code should have an accompanying comment. For examples of appropriate comments, refer to any of the code examples on Canvas. Add appropriate welcome/goodbye messages to the program (in addition to the prompt messages, etc. as part of the game). The program should allow for a maximum of 10 guesses by the player. If the user makes 10 incorrect guesses, print an appropriate "you lose" message and end the program. You should hardcode the "secret" number as a single word in the .data section of the assembly code. Change the number for each test case below. Test your program using the following inputs. Secret number: 4 Guesses: 42 23 16 15 8 4 Secret number: 17 Guesses: 5 20 17 Secret number: 11 Guesses: 123456789 10

Explanation / Answer

section   .text
   global _start    ;must be declared for using gcc
_start: ;tell linker entry point
  
  
mov ecx, msg1 ;welcome user
mov edx, len1
mov eax, 4
mov ebx, 1
int 80h
  
MOV   CL, 10
  
l1:   ;loop started here
   mov   edx,len2 ;message length
   mov   ecx,msg2 ;Guess a number message to write
   mov   ebx,1 ;file descriptor (stdout)
   mov   eax,4 ;system call number (sys_write)
   int 80h
  
mov eax, 3
mov ebx, 2
mov ecx, num ;insert number
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
  
   cmp ecx,secret
   je match
   jl low
   jg high
match:  
   mov   edx,len3 ;message length
   mov   ecx,msg3 ;congratulation
   mov   ebx,1 ;file descriptor (stdout)
   mov   eax,4 ;system call number (sys_write)
   int 80h

  
high:  
   mov   edx,len4 ;message length
   mov   ecx,msg4 ;high num string
   mov   ebx,1 ;file descriptor (stdout)
   mov   eax,4 ;system call number (sys_write)
   int 80h
low:  
   mov   edx,len5 ;message length
   mov   ecx,msg5 ;low number message to write
   mov   ebx,1 ;file descriptor (stdout)
   mov   eax,4 ;system call number (sys_write)
   int 80h
  
   DEC   CL
   loop l1
  
   ;mov ecx,CL
   cmp CL,0
je exi  
  
exi:  
   mov   edx,len6 ;message length
   mov   ecx,msg6 ;10 time gussed message to write
   mov   ebx,1 ;file descriptor (stdout)
   mov   eax,4 ;system call number (sys_write)
   int 80h

  
  
section   .bss
num resb 5

section   .data

secret dd '4' ;secret number hard coded
z dd '0'
msg1   db   'Welcome user '       ;welcome string
msg2   db   'Guess a number '       ;Guess a number string
msg3   db   'congratulation you guess correct '   ;congratulation string
msg4   db   'you guess high number '           ;result string
msg5   db   'you guess low number '           ;result string
msg6   db   'you exhaust all 10 chances..better luck next time. '   ;final message

len1   equ   $ - msg1                   ;length of our dear string
len2   equ   $ - msg2
len3   equ   $ - msg3
len4   equ   $ - msg4
len5   equ   $ - msg5
len6   equ   $ - msg6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote