flat assembler
Message board for the users of flat assembler.

Index > DOS > Need guidance for first program (file i/o)

Author
Thread Post new topic Reply to topic
xHero



Joined: 11 Jun 2010
Posts: 7
xHero 14 Jun 2010, 06:32
I have spent the last couple hours reading the FASM progammers manual. I have decided to write a program that reads in a user specified file, stores it then outputs it in the user specified file. I would like to do this using standard push,pop and call instead of invoke crap. My problem is that I don't know when using push,pop and call is needed over invoke or vise versa. I would also like to due this using E** )32-bit resgisters. All I have done so far is read a few tutorials. I'm really not sure how to start the program... do i use

entry start

section 'text' code readable writeable executable

;code

section 'data' data readable writeable executable

or just start with use32 and move straight into my code. I understand most basic instructions now. I believe enough to perform this task I'm just not sure how to utilize them to do this. Any help will be great appreciated. Thank in advance!
Post 14 Jun 2010, 06:32
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 14 Jun 2010, 09:37
See the example folder in the download for some ways to write DOS programs.
Post 14 Jun 2010, 09:37
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Jun 2010, 10:13
xHero,

section directive is for PE/COFF/ELF formats. Are you using DOS extender?
Post 14 Jun 2010, 10:13
View user's profile Send private message Reply with quote
xHero



Joined: 11 Jun 2010
Posts: 7
xHero 14 Jun 2010, 10:35
i'm using DOSBox.

rev: I have gone over many examples. I'm just not sure how to read in a file and output it
Post 14 Jun 2010, 10:35
View user's profile Send private message Send e-mail Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 14 Jun 2010, 10:52
If your coding for DOS, get used to using rbil. Look at int 21, ah=3D(open) and int 21, ah=3F(read). I don't code for DOS, so idk if this is the "right"/standard way to do it.
Post 14 Jun 2010, 10:52
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1403
Location: Piraeus, Greece
Picnic 14 Jun 2010, 13:25
Hi xHero,

You might find useful this list with some basic interrupts under DOS.

INT 21h / AH= 3Ch - create or truncate file.
INT 21h / AH= 3Dh - open existing file.
INT 21h / AH= 3Eh - close file.
INT 21h / AH= 3Fh - read from file.
INT 21h / AH= 40h - write to file.
INT 21h / AH= 42h - SEEK - set current file position.
Post 14 Jun 2010, 13:25
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Jun 2010, 15:08
xHero,

Here's quick'n'dirty example of DOS I/O: this program reads standard input and copies it to standard output with rudimentary error handling.
Code:
       org     0x100
.again: xor        bx, bx                  ; standard input handle
     mov     dx, buffer              ; ds:dx -> buffer for data
   mov     cx, buffer.size         ; number of bytes to read
   mov     ah, 0x3F                ; DOS func: READ
        int     0x21                    ; call DOS dispatcher
       jc      .read_error             ; CF indicates error
        test    ax, ax                  ; ax==0 if EOF
      jz      .done
       mov     cx, ax                  ; number of bytes read otherwise
    inc     bx                      ; standard output handle (1)
        mov     ah, 0x40                ; DOS func: WRITE
       int     0x21                    ; call DOS dispatcher
       jc      .write_error            ; CF indicates error
        cmp     ax, cx                  ; number of bytes written
   je      .again
; warning: number of bytes written != number of bytes read
    mov     cx, warn_incomplete_write.size
      mov     dx, warn_incomplete_write
.error: mov    bx, 2                   ; standard error handle
     mov     ah, 0x40
    int     0x21
.done:      ret

.read_error:
 mov     bx, err_read.code       ; offset of error code placeholder
  mov     dx, err_read            ; in this message
   mov     cx, err_read.size       ; message size
.convert_error_code:
  aam     16                      ; split error code into ah:al
   cmp     al, 10                  ; convert low nibble
        sbb     al, 0x69
    das
 xchg    al, ah                  ; move converted char to its place
  cmp     al, 10                  ; convert high nibble
       sbb     al, 0x69
    das
 mov     [bx], ax                ; place converted error code in message
     jmp     .error

.write_error:
     mov     bx, err_write.code      ; see read_error
    mov     dx, err_write
       mov     cx, err_write.size
  jmp     .convert_error_code

warn_incomplete_write db "Warning: write not completed successfully.", 13, 10
.size = $-warn_incomplete_write

err_read db    "Error: read completed with code "
.code       db      "00"
      db      ".", 13, 10
.size = $-err_read

err_write db "Error: write completed with code "
.code      db      "00"
      db      ".", 13, 10
.size = $-err_write

buffer      rb      512
.size = $-buffer    
Post 14 Jun 2010, 15:08
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.