flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
igob 30 Jan 2024, 12:42
How I can open file and write to in FASM for dos
|
|||
![]() |
|
igob 30 Jan 2024, 14:46
Where I can see the created files. I work with dosbox-x and fasm 1.73.32 for dos
|
|||
![]() |
|
macomics 30 Jan 2024, 15:45
Code: format MZ segment text entry text:$ PRINT_STR = $09 EXIT_SUCCESS = $4C00 EXIT_FAILED = $4C01 FILE_CREATE_OR_TRUNCATE = $3C FILE_OPEN_READ = $3D00 FILE_OPEN_WRITE = $3D01 FILE_OPEN_READ_WRITE = $3D02 FILE_CLOSE = $3E FILE_READ = $3F FILE_WRITE = $40 FILE_SEEK_FROM_BEGINING = $4200 FILE_SEEK_FROM_CURRENT = $4201 FILE_SEEK_FROM_END = $4202 FILE_ONLY_CREATE_NEW = $5B ; Prepare mov ax, rwdata mov ds, ax mov es, ax ; open file and read mov dx, fname1 ; dx = offset fname mov ax, FILE_OPEN_READ ; al = 0 - read only, al = 1 - write only, al = 2 - read write int 33 ; ah = $3D - open file (ds:dx - file name; al = mode): out ax = fd or cf = 1 jc error_end ; cf = 1 - error mov [fd], ax ; out ax = fd mov dx, fbuff ; dx = buffer mov cx, szbuf ; cx = bytes mov bx, [fd] ; bx = fd mov ah, FILE_READ int 33 ; ah = $3F - read fd (bx - fd, cx - bytes to read, ds:dx - memory buffer): out ax - readed bytes or cf = 1 jc error_end1 ; cf = 1 - error mov [readed], ax ; out ax = readed bytes mov bx, [fd] ; bx = fd mov ah, FILE_CLOSE int 33 ; ah = $3E - close fd (bx - fd) jc error_end ; cf = 1 - error ; create file and write mov dx, fname2 ; dx = offset fname mov cx, $20 ; cx = FILE_ATTRIBUTE_NORMAL mov ah, FILE_CREATE_OR_TRUNCATE int 33 ; ah = $3C - create new file or truncate (dx:dx - file name; cx - attributes): out ax = fd or cf = 1 jc error_end ; cf = 1 - error mov [fd], ax ; out ax = fd mov dx, wdata ; dx = offset wdata mov cx, wsize ; cx = bytes to write mov bx, [fd] ; bx = fd mov ah, FILE_WRITE int 33 ; ah = $40 - write fd (bx - fd, cx - bytes to write, ds:dx - memory block): out ax = writen bytes or cf = 1 jc error_end1 ; cf = 1 - error cmp ax, wsize jc error_end1 ; out ax < wsize - error mov dx, fbuff ; dx = offset wdata mov cx, [readed]; cx = bytes to write mov bx, [fd] ; bx = fd mov ah, FILE_WRITE int 33 ; ah = $40 - write fd (bx - fd, cx - bytes to write, ds:dx - memory block): out ax = writen bytes or cf = 1 jc error_end1 ; cf = 1 - error cmp ax, [readed] jc error_end1 ; ax < [readed] - error mov bx, [fd] ; bx = fd mov ah, FILE_CLOSE int 33 ; ah = $3E - close fd (bx - fd) jc error_end ; cf = 1 - error ; create new and write, seek and read mov dx, fname3 ; dx = offset fname3 mov cx, $20 ; cx = FILE_ATTRIBUTE_NORMAL mov ah, FILE_ONLY_CREATE_NEW int 33 ; ah = $5B - create new file or fail (dx:dx - file name; cx - attributes): out ax = fd or cf = 1 (DOS 3.x +) jc error_end ; cf = 1 - error mov [fd], ax ; out ax = fd mov dx, fbuff ; dx = offset wdata mov cx, [readed]; cx = bytes to write mov bx, [fd] ; bx = fd mov ah, FILE_WRITE int 33 ; ah = $40 - write fd (bx - fd, cx - bytes to write, ds:dx - memory block): out ax = writen bytes or cf = 1 jc error_end1 ; cf = 1 - error cmp ax, [readed] jc error_end1 ; ax < [readed] - error mov dx, wdata ; dx = offset wdata mov cx, wsize ; cx = bytes to write mov bx, [fd] ; bx = fd mov ah, FILE_WRITE int 33 ; ah = $40 - write fd (bx - fd, cx - bytes to write, ds:dx - memory block): out ax = writen bytes or cf = 1 jc error_end1 ; cf = 1 - error cmp ax, wsize jc error_end1 ; out ax < wsize - error mov dx, -5 mov cx, -1 ; cx:dx = offset (-5) mov bx, [fd] mov ax, FILE_SEEK_FROM_CURRENT int 33 ; ah = $42 - seek fd (bx - fd, cx:dx - offset. al - seek mode: 0 - from begining, 1 - from current, 2 - from end): out dx:ax = offset or cf = 1 jc error_end1 ; cf = 1 - error mov dx, fbuff ; dx = buffer mov cx, szbuf ; cx = bytes mov bx, [fd] ; bx = fd mov ah, FILE_READ int 33 ; ah = $3F - read fd (bx - fd, cx - bytes to read, ds:dx - memory buffer): out ax - readed bytes or cf = 1 jc error_end1 ; cf = 1 - error mov [readed], ax ; out ax = readed bytes mov dx, fbuff ; dx = offset wdata mov cx, [readed] ; cx = bytes to write mov bx, 1 ; bx = fd (0 - stdin, 1 - stdout, 2 - stderr) mov ah, FILE_WRITE int 33 ; ah = $40 - write fd (bx - fd, cx - bytes to write, ds:dx - memory block): out ax = writen bytes or cf = 1 jc error_end1 ; cf = 1 - error cmp ax, [readed] jc error_end1 ; out ax < wsize - error mov dx, 0 mov cx, 0 ; cx:dx = offset (0) mov bx, [fd] mov ax, FILE_SEEK_FROM_END int 33 ; ah = $42 - seek fd (bx - fd, cx:dx - offset. al - seek mode: 0 - from begining, 1 - from current, 2 - from end): out dx:ax = offset or cf = 1 jc error_end1 ; cf = 1 - error mov [fsize.l],ax ; out ax - offset low mov [fsize.h],dx ; out dx - offset high mov dx, 0 mov cx, 0 ; cx:dx = offset (0) mov bx, [fd] mov ax, FILE_SEEK_FROM_BEGINING int 33 ; ah = $42 - seek fd (bx - fd, cx:dx - offset. al - seek mode: 0 - from begining, 1 - from current, 2 - from end): out dx:ax = offset or cf = 1 jc error_end1 ; cf = 1 - error or ax, dx jnz error_end1 ; dx:ax != 0 - error cmp [fsize.h], 0 jnz @f cmp [fsize.l], szbuf ja @f mov dx, fbuff ; dx = offset fbuff mov cx, szbuf; cx = bytes mov bx, [fd] ; bx = fd (0 - stdin, 1 - stdout, 2 - stderr) mov ah, FILE_READ int 33 ; ah = $3F - read fd (bx - fd, cx - bytes to read, ds:dx - memory buffer): out ax - readed bytes or cf = 1 jc error_end1 ; cf = 1 - error mov [readed],ax ; out ax = readed bytes @@: mov bx, [fd] ; bx = fd mov ah, FILE_CLOSE int 33 ; ah = $3E - close fd (bx - fd) jc error_end ; cf = 1 - error ; End of program mov dx, exitok mov ah, PRINT_STR int 33 mov ax, EXIT_SUCCESS int 33 ; close fd when error error_end1: mov bx, [fd] mov ah, FILE_CLOSE int 33 error_end: mov dx, errort mov ah, PRINT_STR int 33 mov ax, EXIT_FAILED int 33 segment rwdata fname1 db 'READ.TXT', 0 fname2 db 'WRITE.TXT', 0 fname3 db 'NEW.TXT', 0 exitok db 13, 10, 'Done: EXIT_SUCCESS$' errort db 13, 10, 13, 10, 'Bad end: EXIT_FAILED$' wdata db 'Hello world!' wsize = $ - wdata align 4 label fsize dword .l dw ? .h dw ? fd dw ? readed dw ? szbuf = 512 fbuff rb szbuf To run it, you need to have file "READ.TXT" next to the program and there was some data in this file. The program consistently uses some functions:
Function calls in program text are separated from each other by an empty line. Error handling is added at the end of each call (after int 33). If an error occurs in any of functions, program immediately terminates with an error (ExitCode = 1). I added the output of two messages before exiting the program accordingly and now it will not leave an empty console when exiting with an error. |
|||
![]() |
|
macomics 30 Jan 2024, 17:01
This is how the program works in DOSBox
|
|||
![]() |
|
bitdog2u 04 Jun 2025, 22:22
Code: FNAME DB "OUTfile.txt",0 ; nul ending ASCIZ file name FCREATAOm FNAME ; Create+Overwrite Archive JC ERROR MOV [FHAND],AX ; save File handle FWRITE3m AX,[BUF2size],[BUF2adr] JC ERROR FCLOSEm [FHAND] FOPENRm FNAME JC ERROR ; File doesn't exist MOV [FHAND],AX ; Save file handle FREAD3m AX,BUF1size,BUF1adr JC ERROR ; this never happens. If AX=0 File is read to EOF already, move file ptr? MOV [BUF1data],AX ; bytes read in CMP AX,BUF1size ; if data read = data request, there is more data in the file to read JZ @F ; Not read to EOF yet? FCLOSEm [FHAND] ; got the data at BUF1adr: sizeOF [BUF1data] MOV word [FHAND],0 ; zero out file handle marks it as closed @@: Using macro's you can have clean looking code. The downside of .ASM programming is all the clutter ones mind has to sift through to have a working overview of the code, and these macro's kinda clean that up, and makes the writing simplified since they are their own comment. Well, I hope this helps.
Last edited by bitdog2u on 12 Jun 2025, 02:49; edited 2 times in total |
|||||||||||
![]() |
|
bitdog2u 12 Jun 2025, 02:02
WOW, I didn't know that little aFILE.ZIP would be so popular so quick.
So here is what I could find for file manipulation macros in my c:\FASM\inc16\ directory. Sorry I didn't clean it up and check it all out first, so post your complaints here and I'll try to get you all fixed up. bitdog2u PS, if I remember correctly, I checked out and used them all at one point in time. But, to use them you have to have a clue as to what you are doing. But #2, just viewing them can give you an idea of what is possible, and what info to start looking for.
|
|||||||||||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.