Hangman in Assembly

I am currently doing an assembly program on hangman.

I have an external data file (word.txt).
I am already able to access the words in my data file although I dont know how to access the words line per line or should I say word per word.
For example,
red
yellow
blue

how am i suppose to access yellow.. or blue?? Since i only need one word at a time (in this game).

Here's a snippet of my program::

.MODEL small
.STACK 128
.DATA

Handle dw 0 ;variable to store file handle
FileName db "worddb.txt",0 ;file to be opened
Buffer db 101 dup (?)
bytesread dw 0
randomWords db ?
newline db 13,10,"$"

.CODE
main proc
mov ax,@data
mov ds,ax

mov dx,offset FileName ;put address of fileneame in dx
mov al,000 ;access mode - read
mov ah,3Dh ;function 3Dh -open a file
int 21h
mov Handle,ax ;save file handle for later
jc ErrorOpening

mov dx,offset Buffer ;address of buffer in dx
mov bx,Handle ;handle in bx
mov cx,100 ;amount of bytes to be read
mov ah,3Fh ;function 3Fh - read from file
int 21h
jc ErrorReading

mov [bytesread], ax ;ax returns number of actual byte read
mov bx, bytesread ;add terminator to end of byte read
mov buffer[bx], "$"

mov ah, 9h
mov dx, offset buffer
int 21h

mov dx, offset newline
mov ah, 9h
int 21h

mov bx,Handle ;put file handle in bx
mov ah,3Eh ;function 3Eh - close a file
int 21h

I'm stuck! pls help me..
ps. im using tasm
[1844 byte] By [izuma] at [2007-11-20 10:25:21]
# 1 Re: Hangman in Assembly
Hello Izuma,

I don't know exactly what trouble you're facing, so I'm focusing on your code.

The problem comes right after reading word.txt (or woddb.txt):

Your code is:

mov [bytesread], ax ;ax returns number of actual byte read

Change this to

mov bytesread,ax

or

mov [offset bytesread],ax

You need brackets only when you mean the contents of an address. In your example "bytesread" is not an address, but a variable.

Also, make sure that

buffer[bx]

means the same as in C language. Maybe you'll need to change your code to

add bx, offset buffer

before assigning $ to the end of read word:

mov [bx],24h -- ASCII for $

Try it (sorry, I didn't test). Best regards,

Iaki Viggers
iviggers at 2007-11-10 3:55:08 >
# 2 Re: Hangman in Assembly
INT 21h / AH= 42h - SEEK - set current file position.

Entry:

AL = origin of move: 0 - start of file. 1 - current file position. 2 - end of
file.
BX = file handle.
CX:DX = offset from origin of new file position.

Return:

CF clear if successful, DX:AX = new file position in bytes from start of file.
CF set on error, AX = error code.

Notes:

for origins 1 and 2, the pointer may be positioned before the start of the file;
no error is returned in that case, but subsequent attempts to read or write the
file will produce errors. If the new position is beyond the current end of file,
the file will be extended by the next write (see AH=40h).

example:

org 100h
mov ah, 3ch
mov cx, 0
mov dx, offset filename
mov ah, 3ch
int 21h ; create file...
mov handle, ax

mov bx, handle
mov dx, offset data
mov cx, data_size
mov ah, 40h
int 21h ; write to file...

mov al, 0
mov bx, handle
mov cx, 0
mov dx, 7
mov ah, 42h
int 21h ; seek...

mov bx, handle
mov dx, offset buffer
mov cx, 4
mov ah, 3fh
int 21h ; read from file...

mov bx, handle
mov ah, 3eh
int 21h ; close file...
ret

filename db "myfile.txt", 0
handle dw ?
data db " hello files! "
data_size=$-offset data
buffer db 4 dup(' ')

snippet from http://www.emu8086.com

or try editing (edited code of Gavin Estey,thnks!)

.model small
.stack 64

.data
handle dw ?
data db " hello files! "
buffer db 4 dup(' ')
filename db "C:\myfile.txt", 0
.code
mov ax,@data
mov ds,ax

mov ah, 3ch
mov cx, 0
mov dx, offset filename
mov ah, 3ch
int 21h ; create file...
mov handle, ax

mov bx, handle
mov dx, offset data
mov cx, 14
mov ah, 40h
int 21h ; write to file...

mov al, 0
mov bx, handle
mov cx, 0
mov dx, 7
mov ah, 42h
int 21h ; seek...

mov bx, handle
mov dx, offset buffer
mov cx, 4
mov ah, 3fh
int 21h ; read from file...

mov bx, handle
mov ah, 3eh
int 21h ; close file...

mov bx,4
mov buffer[4],"$"

mov dx, offset buffer
mov ah,09
int 21h

mov ah,4ch
int 21h
end
try to compile and run this code...
prints "file" from the myFile.txt

PS. hahah cs 21

;p
TryMeBrB at 2007-11-10 3:56:12 >
# 3 Re: Hangman in Assembly
PS. rey?

haha

PS. quote (edited code of Gavin Estey)

not of Gavin Estey.. wrong name... of emu8086

sorry for the mix up...

thnks...

hala sir pio nandyan na... /gg
TryMeBrB at 2007-11-10 3:57:12 >