Search code examples
loopsassemblyproceduremasmirvine32

Assemlby Language Guess Number Game


I'm having trouble with this program and don't know what's the problem. In this "Guess the number game" the user tell the minimum number and the maximum number range for the game. Then the user tells how many opportunities he have to play.

Have two main problems: 1. The loop counter doesn't work 2. My conditionals jumps are behaving rare.

Notice that I'm using the Irvine Library for this Assembly Program. Need help and I need to get this to work. Thank a lot!!! Here is the code:

; Asignación Gabriel E. Rosario
INCLUDE Irvine32.inc

.data
; MSG
startMsg BYTE "Saludos! Tienes que adivinar un numero escondido.",0 
minMsg BYTE "Dame el valor minimo a adivinar: ",0
maxMsg BYTE "Dame el valor maximo a adivinar: ",0
intentos BYTE "Cuantos intentos deseas para adivinar este numero? ",0
esMenor BYTE "El numero es menor ",0
esMayor BYTE "El numero es mayor ",0
tienes BYTE "Tienes ",0
adivina BYTE " intentos. Adivina el numero: ",0
won BYTE "Felicidades, Adivinastes!!!",0
loss BYTE "Perdistes.",0
over BYTE "Juego Terminado",0
; Empty variables
minNum BYTE ?
maxNum BYTE ?
randomNumber BYTE ?
myNumber BYTE ?
loopCounter BYTE ?

.code
main proc

call Randomize  


; ============== Saludos ================

  mov edx, offset startMsg
  call writeString
  call Crlf

; ============== Give MinValue ================

mov edx, offset minMsg
call writeString
call readInt
mov minNum, al

; ============== Give MaxValue ================

mov edx, offset maxMsg
call writeString
call readInt
mov maxNum, al

;============ Opportunities ==================
mov edx, offset intentos
call writeString
call readInt
mov loopCounter, al   
call Crlf


;============= Make random num and save it =============

call Range       ; calling Range function 
call RandomRange 
add al, minNum
mov randomNumber, al


; ======= Call the big baby :) ======
call Looper

Losser:                  ; Display if you loose
mov edx, offset loss
call writeString

call Crlf
call WaitMsg

invoke ExitProcess,0
main endp

Menor:
mov edx, offset esMenor
call writeString
call Looper
call Crlf

Mayor:
mov edx, offset esMayor
call writeString
call Looper
call Crlf

Winner:
mov edx, offset won
call writeString
jmp Done
call Crlf

Done:
mov edx, offset over
call readString
call Crlf


; =============== funciones =============
Looper proc
; Cuantos intentos tienes?
movzx ecx, loopCounter
MSG_Intentos:
mov edx, offset tienes
call writeString

mov eax, ecx
call writeDec

mov edx, offset adivina
call writeString

;========= escribir numero =================

call readInt
mov myNumber, al

call Compara

loop MSG_Intentos
ret
Looper endp

Compara proc
mov myNumber, al
mov bl, myNumber
cmp bl, randomNumber
call Jumps
ret
Compara endp

Jumps proc    ; These aren't working so well, don't know why...
jg Menor
jl Mayor
je Winner
ret
Jumps endp

Range proc
movzx eax, maxNum
sub al, minNum
inc eax
ret
Range endp

end main

; Formula para sacar el range.
; Range = (max - min) + 1

Solution

    1. The loop counter doesn't work
    Menor:
    mov edx, offset esMenor
    call writeString
    ;;;;;;;;;;;;;;;;;;;;;;;;;;call Looper
    call Crlf
    RET
    
    Mayor:
    mov edx, offset esMayor
    call writeString
    ;;;;;;;;;;;;;;;;;;;;;;;;;;call Looper
    call Crlf
    RET
    

    You can't call Looper from these 2 codes! Just ret will bring you back to your loop.

    1. My conditionals jumps are behaving rare
    Compara proc
     mov myNumber, al
     mov bl, myNumber
     cmp bl, randomNumber
     jl Menor ;;;;;;;;jg Menor
     jg Mayor ;;;;;;;;jl Mayor
     je Winner
     ret
    Compara endp
    

    Use other conditional jumps and don't put the interpretation of the flags in a different procedure. Flags need not be defined at procedure entry!