News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Writing to a file

Started by amrac, December 16, 2009, 07:14:51 PM

Previous topic - Next topic

amrac

 I'm trying to do a small program in which I write to a file which is already  created. Can anyone explain to me why this is printing the buffer to the screen instead of printing it to the file numbers.txt? Don't read the comments. They're in portuguese. Here goes the code:

stack segment para stack   ; define segmento de pilha
   db   64   dup   ('mystack ')
stack ends

mydata segment para 'data'

buffer db 10 dup ('t'),'$'
filename   db   '.\numbers.txt',0   ;nome do ficheiro, terminado com 0
handle dw ?
mydata ends

mycode segment para 'code'   ;def segemnto de cod para o masm

myproc proc far
   assume cs:mycode,ds:mydata,ss:stack
   push ds            ;guardar na pilha o seg ds
   sub ax,ax         ;garante zero no ax
   push ax            ;guarda zero na pilha
   mov ax,mydata   ;coloca em ax a posiçao dos dados
   mov ds,ax         ;coloca essa posicao no reg. ds
   mov es,ax         ;coloca essa posicao no reg. es
   
   call ToFile
   RET
myproc endp
ToFile proc near
   mov ah,3dh               ;prepara para abrir o ficheiro
   mov al,2               ;abre o ficheiro para escrita
   mov dx,offset filename      ;endereço do ficheiro que vai ser aberto
   int 21h
   ;lea dx,buffer            ;coloca linha no ecran
   ;mov ah,09h            ;inicialização de parâmetro de interrupção
   ;int 21h               ;interrupção que envia frase para o ecran
   mov ah,40H      ;prepara a escrita no ficheiro
   mov handle,bx
   mov cx,10
   mov dx, offset buffer
   int 21h
   mov ah,3eh               ;prepara para fechar o ficheiro
   int 21h
   ret
ToFile endp
mycode ends
end

dedndave

the handle is screwy - lol
the open file and create file functions return the handle in AX (mov handle,AX) if successful
then - the read/write/set pointer/close functions all need the handle in BX

amrac

Can you tell me where can I find, in the internet, more information about handles? I didn't quite understood what you said. But thanks anyway for your answer

dedndave


   mov ah,3dh               ;prepara para abrir o ficheiro
   mov al,2               ;abre o ficheiro para escrita
   mov dx,offset filename      ;endereço do ficheiro que vai ser aberto
   int 21h
   ;lea dx,buffer            ;coloca linha no ecran
   ;mov ah,09h            ;inicialização de parâmetro de interrupção
   ;int 21h               ;interrupção que envia frase para o ecran
   mov ah,40H      ;prepara a escrita no ficheiro
   mov handle,bx ;<----------------------------------------------------------------------- oops !!!!!
   mov cx,10
   mov dx, offset buffer
   int 21h

after opening the file.....

        mov     dx,offset filename
        mov     ax,3D02h
        int     21h
        jc      Error_Code           ;if the carry flag is set, there was an error

        mov     handle,ax            ;otherwise, AX contains the handle

then, to access that file.....

        mov     bx,handle
.
.
        mov     ah,Function_Number
        int     21h

If you intend to continue writing 16-bit programs, I suggest you download Ralf Brown's Interrupt List:

http://www.cs.cmu.edu/~ralf/files.html

The list is organized in 6 ZIP files (inter61a.zip-inter61f.zip). This is the most complete list of interrupts available.
Most of the information you will need is in the first 2 ZIP files (inter61a.zip and inter61b.zip).

The files are fairly large text files. If you look down the list a little further, there is a program for viewing them.
It is called Ralf Brown Interrupt List Viewer, or simply RBIL Viewer (rbilv21.zip).

The individual text files have names like INTERRUP.A, INTERRUP.B, and so on.
You may want to rename them to something like INT_A.TXT, INT_B.TXT, etc. to make them easier to use.

Ralf's List has a lot of information you may never use, because they try to make it complete.
On the other hand, it is full of useful tables and structure definitions that are hard to find elsewhere.

also - for a quick reference, Randy Hyde's AOA

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html

Chapter 13  BIOS, DOS, and File I/O     (includes information on the interrupt services)

dedndave

oops i corrected a mistake in that code - CF indicates errors - not AX = 0
lol - forgot about CF - in 32-bit code, the status is always in EAX - they don't seem to use the CF anymore