flat assembler
Message board for the users of flat assembler.

Index > DOS > unix2dos

Author
Thread Post new topic Reply to topic
osetnik



Joined: 11 Jun 2021
Posts: 2
osetnik 11 Jun 2021, 19:48
So I tried co make unix to dos (dos to unix) end line converter and tried do use stdin/stdout. This posed a problem to recognize when was end of file reached. So starting from int 21h, ah=08h to read stdin and ah=02h to write stdout, I ended up reading with func. ah=3Fh, and writing with ah=02h.

So question is this (u2d-2):
1. is there a way to get EOF with ah=02h, or similar?

If I stick with ah=3Fh, and wanted to read more than 1 byte at a time, then:
2. how do I distinguish between reading less than requested, especially 0, and EOF?

If I use ah=40h to write (u2d-3) then:
3. does it make sense to check correctness of writing to stdout?
4. if I check then how do I know separate recoverable writing "error" from irrecoverable?

u2d-2:

Code:
;
; read from stdin and write to stdout replacing '\n' with '\r\n'
;
format MZ

main:
        push    cs
        pop     ds
L1:
        mov     dx, buf
        mov     ah, 3Fh
        mov     bx, STDIN
        mov     cx, 1
        int     21h     ; read char from file into buffer
        cmp     al, 0   ; number of bytes read
        je      EXIT

        mov     al, [buf]
        cmp     al, LF
        je      WR_CR
WR_BUF:
        mov     dl, al
        mov     ah, 02h
        int     21h     ; write char form DL to stdout
        jmp     L1
WR_CR:
        mov     dl, CR
        mov     ah, 02h
        int     21h     ; write CR
        mov     al, 10
        jmp     WR_BUF  ; write NL

EXIT:
        mov     ax, 4C00h
        int     21h; exit program

desc db 'unix2dos',0
buf db ?
CR = 13
LF = 10
STDIN = 0
STDOUT = 1
    


u2d-3:

Code:
;
; read from stdin and write to stdout replacing '\n' with '\r\n'
;
format MZ

main:
        push    cs
        pop     ds
L1:
        mov     dx, buf
        mov     ah, 3Fh
        mov     bx, STDIN
        mov     cx, 1
        int     21h     ; read char from file into buffer
        cmp     al, 0   ; number of bytes actually read
        je      EXIT    ; exit at EOF
                        ; check for LF
        mov     al, [buf]
        cmp     al, LF
        je      WR_CR
WR_BUF:
        mov     dx, buf
        mov     ah, 40h
        mov     bx, STDOUT
        mov     cx, 1
        int     21h     ; write char from buffer to file
;       cmp     al, 0   ; number of bytes actually written
;       je      EXIT    ; do ... on unsuccessful write/-
        jmp     L1
WR_CR:
        mov     dx, _cr ; write CR
        mov     ah, 40h
        mov     bx, STDOUT
        mov     cx, 1
        int     21h
;       cmp     al, 0
;       je      EXIT
        jmp     WR_BUF  ; back to writing buf

EXIT:
        mov     ax, 4C00h
        int     21h; exit program

desc db 'unix2dos',0
buf db 0,0
CR = 13
LF = 10
_cr db 13       ; CR ('\r')
_lf db 10       ; LF ('\n')

STDIN = 0
STDOUT = 1
    


.
Post 11 Jun 2021, 19:48
View user's profile Send private message Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 70
Location: Belgium
SeproMan 06 Jul 2021, 18:53
If neither input nor output are redirected, then the process of converting line endings would not make sense.
If input and/or output are redirected, you should always favour using the 3Fh and 40h functions because they offer error reporting. Some additional info about the DOS.ReadFileOrDevice function 3Fh is https://stackoverflow.com/questions/47379024/how-buffered-input-works


Quote:
1. is there a way to get EOF with ah=02h, or similar?


Without STDOUT being redirected, no. If STDOUT is redirected to an actual diskfile, an EOF could happen at the 2GB file limit. A diskerror is much more probable though, but since this function has no error reporting built in, you wouln't know about it until the fatal error termination of your program.


Quote:
2. how do I distinguish between reading less than requested, especially 0, and EOF?


First, the EOF is not an error condition. You can only detect the EOF when DOS returned with the carry flag clear. If the AX register is 0, or less than CX, then the end of the file has been reached.


Quote:
3. does it make sense to check correctness of writing to stdout?


Without STDOUT being redirected, no. If STDOUT is redirected to an actual diskfile, then you must check the carry flag for any errors, but also (when carry is clear) whether AX is equal to CX. If AX is less than CX, the disk is probably 'full'.


Quote:
4. if I check then how do I know separate recoverable writing "error" from irrecoverable?


You can consult
the DOS.GetExtendedErrorInformation function 59h. You must invoke this function immediately after the function 40h returned with an error.

_________________
Real Address Mode.
Post 06 Jul 2021, 18:53
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.