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

Guessing Game Assembly Language. I cannot figure out how to store random number

ID: 3688319 • Letter: G

Question

Guessing Game Assembly Language.

I cannot figure out how to store random number in intNumber, i was planning on using intNumber as a random number that the user has to guess

include masm32includemasm32rt.inc

.data

   ;prompt0 BYTE "The Guessing Game! Num: 0-9$"
   ;ProgName DWORD 11 DUP (0)
   strProgName DWORD ?
  
   prompt1 BYTE "What is your guess? 0-9 $"
   Prompt BYTE 11 DUP (0)
   strPrompt DWORD ?
  
   prompt2 BYTE "You got it!$"
   GotIt DWORD 11 DUP(0)
   strGotIt DWORD ?

   prompt3 BYTE "Guess Higher!$"
   strHigher DWORD ?

   prompt4 BYTE "Guess Lower!$"
   strLower DWORD ?
  
   intNumber DWORD ?

   intGuess DWORD "0"

.code ; Beginning of the code
main proc
   invoke StdIn, ADDR intGuess, 11
   invoke StdOut, ADDR prompt1
   invoke StdIn, ADDR strPrompt, 11
  
   invoke StdOut, ADDR prompt2
   invoke StdIn, ADDR strGotIt,11
     
   invoke StdOut, ADDR prompt3
   invoke StdIn, ADDR strHigher,11
  
   invoke StdOut, ADDR prompt4
   invoke StdIn, ADDR strLower,11
  

   mov edi, strProgName ; strProgName is moved to edi register
   mov eax, edi ; You cannot directly place segments, so eax is used as a arbitraitor
   mov esi, OFFSET strProgName ; The offset of strProgName
   mov ah, 09h ;display string
   mov cx, 02h ; Counter used for our loop

   spaceLoop: ; Start of loop
   mov dh, 0Dh ; This combined with below
   mov dl, 0Ah ; This combined with above
   mov ah, 02h ; dx is 0D0Ah, 02h is display char function
   loop spaceLoop ; Will go back to spaceLoop

   mov edi, strPrompt ; strPrompt is moved to ax register
   mov eax, edi
   mov esi, OFFSET strPrompt ; The offset of strProgName
   mov ah, 09h ; Tells what function of int 21h we will use

   mov ah, 00h ; Fucntion of 16h that gets 1 char from the user
  
  
   mov [intGuess], ecx ; Moves input into the value of intGuess
   mov ebx, ecx; Inputs al to dl so that it can be shown on screen
   mov edi, 0200h ; Tells interrupt to show char on screen
  

   mov cx, 02h ; Counter for spaceLoop2

   spaceLoop2: ;Another loop that creates two newlines
   mov dh, 0Dh
   mov dl, 0Ah
   mov edi, 0200h
   loop spaceLoop2

   ;---Conditional Jumps---
   mov ebx, OFFSET intNumber ; Cannot compare two values directly
   mov ecx, OFFSET strPrompt ; So I am using registers
   cmp ecx, ebx ; The predefined number is compared to your guess.
   je Winner ; If intNumber = intGuess then go to label Winner
   jg Lower ; jump if greater than
   jl Higher ; jl is jump if lower than

   Winner:
   mov edi, OFFSET strGotIt
   mov eax, edi
   mov esi, OFFSET strGotIt
   mov ah, 09h
   jmp Finish

   Lower: ; Tells you to guess lower then loops back to Start
   mov edi, strLower
   mov eax, edi
   mov esi, OFFSET strLower
   mov ah, 09h

   mov cx, 02h

   spaceLoop3:
   mov dh, 0Dh
   mov dl, 0Ah
   mov edi, 0200h
   jnz spaceLoop3

   jmp main

   Higher: ;Tells you to guess higher then loops back to Start
   mov edi, strHigher
   mov eax, edi
   mov esi, OFFSET strHigher
   mov ah, 09h
  


   mov cx, 02h

   spaceLoop4:
   mov dh, 0Dh
   mov dl, 0Ah
   mov edi, 0200h
   ;int 21h
   jnz spaceLoop4

   jmp main

   Finish:
   mov ah, 00h ; Waits for keypress. No application
  


  
   ; return 0
   mov eax, 0
   ret
main endp
end main

Explanation / Answer

Guessing Game Assembly Language: include masm32includemasm32rt.inc .data formatString BYTE "guess a number from 1 to %d , if you get it right you continue", 0ah, 0dh, 0 guessstr BYTE 11 DUP (0) guess DWORD ? range DWORD 0 answer DWORD 1 prompt BYTE "You lose!", 0 .code main proc call seedrand nextlevel: move eax, 0 mov range, ebx add ebx, 1 pushd ecx call randnum invoke crt_printf, ADDR formatString, ebx invoke StdIn, ADDR guessstr, 10 invoke atodw, ADDR guessstr mov guess, edx add ecx, 1 cmp eax, ebx je nextlevel invoke StdOut, ADDR prompt jnz skip seedrand proc ; seeds the random number generator ; _stdcall invoke GetTickCount ; result goes in eax invoke nseed, eax ret seedrand endp randomnum proc ; generate a random number ; _stdcall mov eax, [esp+4] invoke nrandom, eax ret 4 randomnum endp skip: ; return 0 mov eax, 0 ret main endp end main