Day 20
Writing Files
See Day 19 for the note that I want to make, the same thing applies here.
Opening and Writing a File
Before we can read anything, we need to open it first:
jmp start
filename db 'elmo.txt',0
handle dw 0 ; will be the file handle. the number that DOS assigns to the open file.
len db 255 ; we need an input buffer this time.
act db 0
buffer db 255 DUP 0
start:
mov ah,3Dh ; 3Dh of DOS Services opens a file.
mov al,1 ; 0 - for reading. 1 - for writing. 2 - both
mov dx,offset filename ; make a pointer to the filename
int 21h ; call DOS
mov handle,ax ; Function 3Dh returns the file handle in AX, here we save it for later use.
Get input:
mov ah,0Ah
mov dx,offset len
int 21h
DOS Service Function number 40h writes to a file.
mov ah,40h
mov bx,offset act ; pointer to number of bytes read from user.
mov cl,byte [bx] ; get the contents of the byte at the pointer.
; note that even though CX takes the length, CL physically IS the low byte of CX.
mov dx,offset buffer ; pointer to the actual data in DX.
mov bx,handle ; BX needs the file handle.
int 21h ; call DOS
Now we can just exit, because we've accomplished our mission:
mov ah,4Ch
int 21h ; Function 4Ch (Exit Program)
It looks like a lot like that, but don't forget that you can make your own subroutines and CALL them.
This Day In Review
You can probably tell I used the previous Day's file as a template for this, but oh well!
Happy files!,
- GbaGuy
Intro - Day 21
Patater GBAGuy Mirror
Contact