flat assembler
Message board for the users of flat assembler.
Index
> DOS > File operations in FASM for dos |
Author |
|
igob 30 Jan 2024, 12:42
How I can open file and write to in FASM for dos
|
|||
30 Jan 2024, 12:42 |
|
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
|
|||
30 Jan 2024, 14:46 |
|
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. |
|||
30 Jan 2024, 15:45 |
|
macomics 30 Jan 2024, 17:01
This is how the program works in DOSBox
|
|||
30 Jan 2024, 17:01 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.