; avch. Guess the secret number. Random routine need to be improved, but it should work
; hit CTRL+C at any time to exit
org 100h
xor ah, ah
int 1ah
mov word [RandSeed], dx
@main:
mov cx, 30
call Random
inc dx
mov byte [Secret], dl
xor ah, ah
mov al, dl
mov cl, 10
div cl
or ax, 3030h
mov word [Text3+17], ax ; The secret number in final text
mov cl, 5
@Inner:
mov ch, cl
or ch, 30h
mov byte [Text1+27], ch ; Attempts left in input text
; Input message
mov dx, Text1
mov ah, 9
int 21h
; Get Response
mov dx, Input
mov ah, 0Ah
int 21h
call GetResp
call Success
jc @Exit
jnz @Inner
@Exit:
; ¿Exit?
mov dx, Text4
mov ah, 9
int 21h
@inkey:
xor ah, ah
int 16h
or al, 20h
cmp al, 'y'
jz @main
cmp al, 'n'
jz @exit_dos
jmp @inkey
@exit_dos:
mov dx, Text5
mov ah, 9
int 21h
ret
Success:
clc ; There are attempts, mark it
cmp al, byte [Secret] ; Are we success?
mov dx, Text2 ; we suppose so
jz @SExit
cmp cl, 1
ja @Next1
; We have finished attempts
mov dx, Text3 ; We have finished, failed
stc ; Mark it
jmp @SExit
@Next1:
; We have not finished yet and we have not succeeded
dec cl ; One attempt less
sub al, byte [Secret] ; Which is greather?
mov dx, Text6 ; Suppose lower
js @SExit0
mov dx, Text7 ; no, greather
@SExit0:
clc ; There are attempts, mark it
@SExit:
; Print the message
mov ah, 9
int 21h
ret
Random:
; Purpose : pseudo random number in [0, DL)
; In : CX
; Out : DL: número aleatorio
; Destoys : DX
push ax
mov ax, [RandSeed]
mov dx, 8405h
mul dx
inc ax
mov [RandSeed],ax
xor dx, dx
div cx
pop ax
ret
GetResp:
; Out : al
push bx
push cx
push si
mov si, Input+1
xor bh, bh
cmp byte [Input+1], 1
mov bh, byte [Input+2]
jz @next
mov bx, word [Input+2]
@next:
xor ah, ah
mov al, bl
and al, 0Fh
mov cl, 10
mul cl
mov ah, bh
and ah, 0Fh
add al, ah
pop si
pop cx
pop bx
ret
Input db 3, " "
Text1 db 13, 10, "Guess the number [1,30], attempts: $"
Text2 db 13, 10, "Right!", 13, 10, "$"
Text3 db 13, 10, "Failed, it was ", 13, 10, "$"
Text4 db 13, 10, "Try again (Y/N)?$"
Text5 db 13, 10, 13, 10, "Take a look at http://www.abreojosensamblador.net/", 13, 10, "bye!$"
Text6 db 13, 10, "Too low", 13, 10, "$"
Text7 db 13, 10, "Too high", 13, 10, "$"
RandSeed rw 1
Secret rb 1