Skip to content

Instantly share code, notes, and snippets.

@pawlos pawlos/prog.asm
Last active May 18, 2017

Embed
What would you like to do?
Guess game in assembly
ideal
model small
stack 10244
dataseg
msg1 db 'Enter Word: $'
msg2 db 'Enter Hint: $'
msg3 db 'Enter Guess(Press . to end guess)($'
wrnmsg db 'Wrong!$'
a3msg db 'Number of letters: $'
a4msg db 'First two letters: $'
a5msg db 'Last letter: $'
a6msg db 'Second to the last letter: $'
failmsg db 'Enter guess again($'
secret db 21 dup(?)
hint db 100,?,101 dup(0)
guess db 21 dup(?)
endss db '): $'
win db 'That is correct! You win!$'
lose db 'Im sorry you lose$'
cnt dw 0
codeseg
PROC read_input
push cx
mov bx, 0
mov cx, 0
scan:
call get_char
cmp al, '.'
je com
inc cx
mov [di+bx], al
inc bx
jmp scan
com:
mov [di+bx], '$'
mov ax,cx
pop cx
ret
ENDP
PROC compare
push cx
lea si, [secret]
lea di, [guess]
rep cmpsb
pop cx
ret
ENDP
PROC display_string
mov ah,09
mov al,00
int 21h
ret
ENDP
PROC new_line
mov dx,13
call put_char
mov dx,10
call put_char
ret
ENDP
PROC get_char
mov ah,01
int 21h
ret
ENDP
PROC put_char
mov ah,02
int 21h
ret
ENDP
PROC print_number
push bx
push dx
mov bx,10
xor dx,dx
div bx
mov bx,dx
cmp ax,0
je second
mov dx,ax
add dx,48
call put_char
second:
mov dx,bx
add dx,48
call put_char
pop dx
pop bx
ret
ENDP
PROC main
mov ax, @data
mov ds, ax
mov es, ax
lea dx, [msg1]
call display_string
lea di,[secret]
call read_input
mov [cnt],ax
call new_line
lea dx, [msg2]
call display_string
lea dx, [hint]
mov ah, 10
int 21h
call new_line
lea dx, [msg3]
call display_string
xor bx,bx
mov bl, [hint+1]
mov [hint+bx+2], '$'
lea dx, [hint+2]
call display_string
lea dx, [endss]
call display_string
mov cx, 0
loop2:
inc cx
lea di,[guess]
call read_input
call new_line
call compare
je right
lea dx, [wrnmsg]
call display_string
cmp cx, 3
je three
cmp cx, 4
je four
cmp cx, 5
je five
cmp cx, 6
je six
cmp cx, 7
je fail
man:
call new_line
lea dx, [failmsg]
call display_string
lea dx, [hint+2]
call display_string
lea dx, [endss]
call display_string
jmp loop2
right:
lea dx, [win]
call display_string
jmp end1
fail:
lea dx, [lose]
call display_string
jmp end1
three:
lea dx, [a3msg]
call display_string
mov ax, [cnt]
call print_number
jmp man
four:
lea dx, [a4msg]
call display_string
mov dl, [secret]
call put_char
mov dl, [secret+1]
call put_char
jmp man
five:
lea dx, [a5msg]
call display_string
mov di, [cnt]
mov dl, [secret+di-1]
call put_char
jmp man
six:
lea dx, [a6msg]
call display_string
mov di, [cnt]
sub di,2
mov dl, [secret+di-1]
call put_char
jmp man
end1:
mov ah, 4ch
int 21h
ENDP
END main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.