Help (urgent) : Write result to a created file

Started by boon, September 15, 2008, 08:07:01 AM

Previous topic - Next topic

boon

i wonder to create a file and then put all my result to the file, i can create a file but i have no idea how to write my result to the created file..i try to use int21h function 40h but i doesnt work, can plz any1 here teach me to do that?

.data
outfile db "result.txt"
stat   db "hello world",0

.code
;create the output file
mov ax,716ch
mov bx,1
mov cx,0
mov dx,12h
mov si,offset outfile
int 21h
jc quit
mov outhandle,ax

;write buffer to new file
mov ah,40h
mov bx,outhandle
mov cx,bytesread
mov dx,offset stat
int 21h
jc quit

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



Neil

Here's how to save to a file:-

SAVE: DB 'C:\FOLDER\FILE NAME',0

DATA DB ?

HANDLE DW ?

MOV DX,SAVE      ;File path & name
MOV AH,03C        ;Function get handle
XOR CX,CX           ;And open file
INT 021               ;For writing
JC ERROR            ;If error goto error handling
XCHG AX,BX         ;Ready to write
MOV HANDLE,BX   ;Store handle
MOV AH,040        ;Function write to file
LEA DX,DATA       ;Point at data entry point
MOV CX,?            ; bytes in file
INT 021              ;Write to file
CMP AX,?             ;Check total bytes written
JNE Error              ;Go to error handling if not correct
MOV BX,HANDLE   ;Get file handle
MOV AH,03E        ;Function close file
INT 021               ;Close file
RET

ERROR                ;Handle error here

boon

Neil,


i scare i do not understand about your codes, i do not know how is the data put..
for example,

output db "Hello world",0

if i want to save this "Hello world" to my created file, then where should i move the output to?

Thanks for your reply... :bg

Neil

Put the data inside a file & name it anything you like, then put it into the path :-

SAVE: DB 'C:\FOLDER\YOUR FILE HERE',0

boon

Neil,

can you please explain a little bit more ? actually what is the (data db ?) use for ? is it i put the thing i wan to display at data such as "hello world" ?
and also the (handle dw ?) ?
and then in your code written (mov cx,? ;bytes in file) and (cmp ax,? ;check total bytes written) what is the use of these two lines?

sinsi


fname  db 'result.txt',0    ;filename, zero-terminated
string db 'Hello World'     ;what to write to the file
count  equ $-string         ;the length of the string above


;create output file
    mov ah,3ch      ;DOS function "create file"
    mov cx,20h      ;attributes - bit 5 is the "archive" attribute
    lea dx,fname    ;DS:DX has the filename address
    int 21h         ;ask DOS nicely...
    jc error        ;and abort if there's an error

    mov bx,ax       ;keep the file handle in BX, since DOS usually
                    ;saves BX, and for many DOS calls BX=handle

;write the string to the file
    mov ah,40h      ;DOS function "write handle"
    ;here we would load the handle into BX, but it's already there.
    mov cx,count    ;how many bytes to write
    lea dx,string   ;DS:DX has the buffer address
    int 21h         ;ask DOS
    jc error        ;
    cmp ax,cx       ;on return AX=bytes actually written
    jb error        ;so if less were written then there's a problem

;close the file and we're done
    mov ah,3eh      ;DOS function "close handle"
    ;again, here we would load the handle into BX, but it's already there.
    int 21h

Light travels faster than sound, that's why some people seem bright until you hear them.

boon

thx sinsi, i can get what your code wrote there.. :bg


boon

#7
now i face another problem, at first i had create a file and managed to write a data "hello world" to the file, but then when i try to write more data to the same file, it comes out with problem, can anyone help me check where is my mistake ? and when i declare 2 data in a identifier just like my code below, the result just show the first line which is "hello world" and "welcome to my world" is just dissappear, why?


.model small
.stack

.data
cr = 0dh
lf = 0ah

    fn   db "result.txt",0
    buf  db "hello world",cr,lf
db "welcome to my world",0
    buf1 db "hello world 2",0

.code
.startup

    mov ah, 3Ch         ; Create File with Handle
    mov cx, 0           ; attributes = normal
    mov dx, OFFSET fn   ; DS:DX -> filename
    int 21h
    mov bx, ax          ; store handle in BX

    mov ah, 40h         ; Write File or Device
                        ; handle in BX
    mov cx, SIZEOF buf  ; bytes to write
    sub cx, 1           ; eliminate trailing null
    mov dx, OFFSET buf  ; DS:DX -> buffer
    int 21h

    mov ah, 3Eh         ; Close File with Handle
                        ; handle in BX
    int 21h

    mov ah,3dh
    mov al,2
    mov dx,offset buf1
    ;mov si,OFFSET buf1
    int 21h
    mov bx,ax


    mov cx, SIZEOF buf1  ; bytes to write
    sub cx, 1            ; eliminate trailing null
    mov dx, OFFSET buf1  ; DS:DX -> buffer
    int 21h

    mov ah, 3Eh         ; Close File with Handle
                        ; handle in BX
    int 21h
.exit
end


if i use int 21h 716ch instead of int 21h 3dh ? what is the difference?

Added code tags to make the code easier to read and copy.

FORTRANS

Hello,

You create a file with the name in 'fn'.

You write to it the contents of 'buf'.

You close the file.

You try to open a file with the name in 'buf1'.  That probably
does not work.

You do a DOS INT with your file handle or error number in AX.

You probably don't get any further.  As you probably called
function zero.

HTH,

Steve N.

boon

Thanks Steve,
i did a stupid mistake..=.=
but after i correct it, it can assemble but when i run it, an error stating { the NTVDM CPU has encountered an illegal instruction. CS:d1e3 IP:d1e3 OP:ff ff ff ff ff }
what is it about?

FORTRANS

Quotewhat is it about?

   Well, what happens if you use DEBUG.EXE to look at your
program?  Use DEBUG's P command to step through your
program and see what happens.  Use the U command to
look at your program.

Good luck,

Steve N.

boon

emm, Steve sorry to tell that, i am totally new to masm, so i have no idea how to use debug.exe, p command all that, where can i find it and how to use it?

Mark Jones

For those looking to learn 16-bit DOS coding, this might be the best series of tutorials out there:

http://www.btinternet.com/~btketman/tutpage.html

Includes an interactive assembly interpreter.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

MichaelW

boon,

For each open file, DOS maintains a file pointer that stores the current position in the file. When a file is created or opened DOS sets the file pointer to 0, which is the position of the first byte in the file. The Write File or Device function starts writing to the file at the location specified by the file pointer, and then updates the file pointer so it points to the first byte after the last byte written. So to add more data to the end of the file you would simply call the Write File or Device function again, with DS:DX pointing to the new data, and the length of the new data in CX.

Also, you can control the position of the file pointer with Function 42h, Move File Pointer. This could be used to overwrite data at any position in the file, or truncate the file at any length.


.model small
.stack
.data
cr = 0dh
lf = 0ah

    fn   db "result.txt",0   ; filename needs null terminator
    buf  db "hello world",cr,lf,"welcome to my world",cr,lf
    buf1 db "hello world 2",cr,lf

.code
.startup

    mov ah, 3Ch         ; Create File with Handle
    mov cx, 0           ; attributes = normal
    mov dx, OFFSET fn   ; DS:DX -> filename
    int 21h
    mov bx, ax          ; store handle in BX

    mov ah, 40h         ; Write File or Device
                        ; handle in BX
    mov cx, SIZEOF buf  ; bytes to write
    ;sub cx, 1           ; eliminate trailing null
    mov dx, OFFSET buf  ; DS:DX -> buffer
    int 21h

    ;mov ah, 3Eh         ; Close File with Handle
                        ; handle in BX
    ;int 21h
    ;mov ah,3dh
    ;mov al,2
    ;mov dx,offset buf1
    ;mov si,OFFSET buf1
    ;int 21h
    ;mov bx,ax

    mov ah, 40h         ; Write File or Device
                        ; handle in BX
    mov cx, SIZEOF buf1  ; bytes to write
    ;sub cx, 1            ; eliminate trailing null
    mov dx, OFFSET buf1  ; DS:DX -> buffer
    int 21h

    mov ah, 3Eh         ; Close File with Handle
                        ; handle in BX
    int 21h
.exit
end

eschew obfuscation

boon

MichaelW, really thanks alot, you lead me have clearer image about the input and output file...and thanks to Mark Jones for introducing the websites to me..Great to meet you guys here... :bg