int 0x80



gorlist@int0x80:~$ cd ~/code; ls -la
drwxr-xr-x 2 gorlist gorlist  120 2007-10-08 20:45 .
drwxr-xr-x 6 gorlist gorlist 1216 2007-10-08 20:45 ..
-rw-r--r-- 1 gorlist gorlist 5078 2007-10-30 00:33 strguess.asm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 					;;
;; String Permutation Guessing Game	;;
;; 					;;
;;		in x86 ASM		;;
;;		by gorlist		;;
;;					;;
;; 	nasm -f elf game.asm		;;
;; 	gcc game.o -o game.out		;;
;; 		./game.out		;;
;;					;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

extern open
extern printf
extern strfry
extern time
extern srand

extern rand
extern execl

global main

section .data

fmttries: db "You have %d tries left.",10,0
fmtgotit: db "You got it after %d tries.",10,0

fmtfirst: db "Hint: The first char is %s.",10,0
fmtsecond: db "Hint: The second char is %s.",10,0

fmtword: db "Too bad. The word was %s.",10,0

inputmsg: db "Enter guess: "

leninputmsg: equ $-inputmsg

guessmsg: db "The word you have to guess is: ",10,0

lenguessmsg: equ $-guessmsg

correctmsg: db "Correct!",10,0

lencorrectmsg: equ $-correctmsg

falsemsg: db "That's not it.",10,0

lenfalsemsg: equ $-falsemsg

playagainmsg: db "Do you want to play again? (y/n) ",0
lenplayagainmsg: equ $-playagainmsg

thankyoumsg: db "Thank you for playing!",10
lenthankyoumsg: equ $-thankyoumsg

yes: db "y",10
name: db "words.txt",0
fd: dd 0

flags: dd 0
chars: dd 0
max: dd 10

num: dd 0
tries: dd 5
guesses: dd 0

execvalue: dd 0

a: dd 1
b: dd 100

wordlen: dd 0

cor: db "Correct",10,0

lencor: equ $-cor
false: db "Wrong",10,0

lenfal: equ $-false

fmtstr: db "%s",10

section .bss

buf: resb 512
buf_scr: resb 512

line: resb 512
lenbuf: resd 1
first_char: resb 2

second_char: resb 2
yesorno: resb 2
filename: resb 64

inputstr: resb 256
readlen: resb 4

section .text

main:
mov eax,[esp+8]
mov ebx,[eax]

mov [filename],ebx
mov ebx,[filename]
xor eax,eax
xor ebx,ebx

begin:
push ebp
mov ebp,esp
push ebx

openit:
push dword [flags]
push dword name
call open

add esp, 8
mov [fd], eax
cmp eax, 2

jg next

next:
xor esi,esi

count_chars:
mov edx, 1

mov ecx, line
mov ebx, [fd]
mov eax, 3
int 0x80

inc esi
mov [lenbuf], eax
cmp eax, 0
jg count_chars

jmp eof

eof:
dec esi
mov [chars], esi


randomize:

xor edx, edx

call time
add esp, 4

push eax
call srand

call rand
xor edx, edx

mov ecx, [chars]
sub ecx, [a]
div ecx

add edx, [a]
mov [num], edx
mov al, [max]

cmp [num], al

jl no_sub
sub [num], al

no_sub:
mov eax, [num]

lseek:
mov eax, 19
mov ebx, [fd]
mov ecx, [num]

mov edx, 0
int 0x80

mov edi,buf

doread:
mov edx, 1
mov ecx, edi
mov ebx, [fd]

mov eax, 3
int 0x80

inc edi
mov esi, [ecx]

cmp esi, 10
je new
jmp doread

new:

mov edi, buf

xor eax, eax
mov [wordlen], eax

continue:
mov edx, 1
mov ecx, edi
mov ebx, [fd]

mov eax, 3
int 0x80

inc edi

mov eax, [wordlen]
inc eax
mov [wordlen], eax

mov esi, [ecx]

cmp esi, 10
je printing
jmp continue


printing:

mov eax, [wordlen]
dec eax
mov [wordlen], eax

mov eax,buf-1

chop:
inc eax
cmp byte [eax], 0
je done

cmp byte [eax], 10
jne chop
mov byte [eax], 0

done:

; UNCOMMENT TO PRINT THE CHOSEN WORD
; push buf
; push fmtstr
; call printf
; add esp,8

cld
mov ecx, [wordlen]

mov esi, buf
mov edi, buf_scr
repz movsb

mov esi, buf

mov edi, first_char
movsb

mov ecx, 2
mov esi, buf

mov edi, second_char
repz movsb

mov esi, buf
mov edi, first_char

movsb

push buf_scr
call strfry
push eax
push fmtstr

call printf
add esp, 12

input:

mov eax, 4

mov ebx, 1
mov ecx, inputmsg
mov edx, leninputmsg
int 0x80

mov eax, 3
mov ebx, 0
mov ecx, inputstr

mov edx, 256
int 0x80

mov [readlen], eax
cmp eax, 0

jg readok
jmp exit

readok:

xor ebx, ebx

mov eax, inputstr-1

chop2:
inc eax
inc ebx

cmp byte [eax], 0
je done2
cmp byte [eax], 10

jne chop2
mov byte [eax], 0
done2:

dec ebx

mov eax, [wordlen]

xor eax, eax

xor ecx, ecx
xor esi, esi
xor edi, edi

mov eax, [tries]

stillguessing:

mov esi, buf
mov edi, inputstr

mov ecx, [wordlen]

cmp ecx, ebx
jne notcorrect

cld
repe cmpsb
or ecx, ecx
jnz notcorrect
mov edx, lencor

mov ecx, cor
mov ebx, 1
mov eax, 4

int 0x80
xor eax,eax

jmp gotit

notcorrect:

mov eax, [tries]
dec eax
push eax
push fmttries

call printf
add esp, 8

mov edx, lenfal
mov ecx, false

mov ebx, 1
mov eax, 4
int 0x80
xor eax, eax

triesleft:
mov eax, [tries]
dec eax
mov [tries], eax

mov ebx, [guesses]
inc ebx
mov [guesses], ebx

cmp eax, 2

je first
cmp eax, 1
je second
cmp eax, 0

jg near input
jmp didntgetit

first:
push first_char

push fmtfirst
call printf
add esp, 8
jmp input

second:
push second_char
push fmtsecond
call printf

add esp, 8
jmp input

didntgetit:
push buf

push fmtword
call printf
add esp, 8
jmp close

gotit:
mov eax, [guesses]
inc eax
push eax
push fmtgotit

call printf
add esp, 8

close:
mov eax, 6

mov ebx, [fd]
int 0x80

ask:
mov eax, 4

mov ebx, 1
mov ecx, playagainmsg
mov edx, lenplayagainmsg
int 0x80

mov eax, 3
mov ebx, 0
mov ecx, yesorno

mov edx, 3
int 0x80

xor eax, eax

xor ebx, ebx
xor ecx, ecx
xor edx, edx

mov esi, yes
mov edi, yesorno

cmpsb
je exec_yes

mov eax, 4
mov ebx, 1
mov ecx, thankyoumsg

mov edx, lenthankyoumsg
int 0x80

xor eax, eax
xor ebx, ebx

xor ecx, ecx
xor edx, edx

exit:
mov eax, 1

mov ebx, 0
int 0x80

exec_yes:
mov eax, 1

mov [execvalue], eax

exec:
mov eax, 0
mov ebx, [filename]

push eax
push ebx
push ebx
call execl

add esp, 12

mov eax, 1
mov ebx, 0

int 0x80

Copyright © gorlist 2007-2024