News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

using masm32 to write string to text file

Started by irnix, February 08, 2014, 04:46:54 AM

Previous topic - Next topic

irnix

Like in c++ we use ofstream to write text to a text file and use maybe notepad, textpad to open and view data

I wanted an example with assembly, what must I do? Or positively, help with a simple source code that can get text from dos box command line and save in a text (.txt) file.

Happy if am helped.

MichaelW

I don't use DOS Box, but I think to create, write, and read text files you should be able to use the DOS file functions. Unless you have some other DOS function reference, you are probably going to need Ralf Brown's Interrupt List, an HTML version is here and a downloadable version here. Start with Interrupt 21h function 3Ch, 3Dh, 3Eh, 3Fh, and 40h (Create, Open, Close, Read, and Write).
Well Microsoft, here's another nice mess you've gotten us into.

mabdelouahab

Try this
1. WIN32
include \masm32\include\masm32rt.inc
.DATA
s_ db 255 dup(0)

.CODE
SaveStr proc lpName:DWORD,Str_:dword

    LOCAL hFile :DWORD
    LOCAL bytewr    :DWORD
LOCAL buff [255]:dword

INVOKE RtlZeroMemory, ADDR buff, 255
Invoke lstrcpy, Addr buff, Str_
;CREATE_ALWAYS
;OPEN_EXISTING
    invoke CreateFile,lpName,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
    mov hFile, eax
    invoke WriteFile,hFile,ADDR buff,255,ADDR bytewr,NULL
    invoke CloseHandle,hFile

    ret
SaveStr endp

start:
invoke StdIn,ADDR s_,255
invoke SaveStr,chr$("Abc.TXT"),addr s_
invoke ExitProcess,eax

end start

2. DOS

DOSSEG
.MODEL LARGE
.STACK 200h

.DATA
filename db 14 dup (0)
filehandle dw 0
bufferLen db 25
buffer  db 25 dup (0)

.CODE

ASSUME CS:@CODE, DS:@DATA

START:
mov AX,@DATA
mov ES,AX                  ; Point ES to the data segment for now
;------------------read filename ( Commandline )
mov ah,62h
int 21h                    ; Get the PSP
mov ds,bx
mov bx,81h                 ; Starting at the first printable character
add bl, byte ptr [ds:80h]  ; Get address of last character
mov cl, byte ptr [ds:80h]  ; Also put it in CL
inc cl
mov ds:[bx], word ptr 0    ; Null terminate the argument

mov si,81h
mov di,0                   ; Copy the first argument into the data segment
rep movsb                  ;  into the filename variable

mov AX,@DATA
mov DS,AX                  ; Point DS to the data segment, like normal

;------------------read string
MOV AH, 0AH
MOV DX, offset buffer
INT 21H

;------------------fileCreate
mov ah,3Ch                 ; Creat DOS service (yes, it is called 'creat')
mov cx,0                   ; File attributes
mov dx,offset filename     ; Put ADDRESS of filename in DX
int 21h

mov [filehandle],ax        ; File handle is returned in AX, put in a variable



;------------------fileWrite
mov ah, 40h
mov bx, [filehandle]
mov dx, offset buffer       ; ADDRESS of string to be written
xor cx, cx                 ; If I don't do this, things blow up in my face
mov cl, [bufferLen]         ; VALUE of length of string to be written
int 21h

;------------------fileClose
mov ah,3Eh
mov bx,[filehandle]
int 21h

;------------------ exit
mov AX,4C00h
int 21h                     
END START