3. Разработка программы.
Разработаем программу
3.1. Написание кода
Для создания файла воспользуемся командой echo:

CMD>> echo pract4 > pract4.asm
Теперь в созданном файле (F4) впишем код
CODE SEGMENT
ASSUME CS:CODE, DS:CODE, SS:CODE
ORG 100h
START:
LEA DX, WELCOME
CALL PRINT
LEA DX, FILENAME
CALL INPUT
CALL NEXTLINE
MOV BL, [FILENAME+1] ; len(FILENAME)
MOV [FILENAME+2+BX], BYTE PTR 0 ; because the filename should fit the format
'filepath,0'
CALL OPENFILE |
; oppening the file with filename = FILENAME |
JNC PUSHSTRING |
; if the file exists |
CALL CREATEFILE |
; creating the file with filename = FILENAME |
PUSH AX |
|
LEA DX, NOFILE |
; pushing the notificatoion about new file |
CALL PRINT |
|
|
|
4
CALL NEXTLINE
PUSHSTRING: |
|
|
PUSH AX |
|
|
LEA DX, STR |
|
|
CALL PRINT |
|
|
LEA DX, MES |
; getting the message to write |
|
CALL INPUT |
|
|
CALL NEXTLINE |
|
|
POP AX |
|
; pop the descriptor |
CALL COURSOR |
; set the coursor position to the end of the |
|
file |
|
|
CALL WRITE |
; writing message to the file |
|
LEA DX, BYE |
; pushing notification about the success |
|
CALL PRINT |
|
|
CALL NEXTLINE |
|
|
CALL CLOSEFILE |
; closing file |
|
EXIT: |
|
|
MOV AH, |
4CH |
|
INT 21H |
|
|
OPENFILE: ; Opennig file with filename = FILENAME and access = write |
||
MOV AH, |
3DH |
|
MOV AL, |
1 |
|
LEA DX, [FILENAME+2] |
|
|
INT 21H |
|
|
RET |
|
|
CREATEFILE: ; Creating file with filename = FILENAME and access = write |
||
MOV AH, |
5BH |
|
MOV CX, |
0 |
|
LEA DX, |
[FILENAME+2] |
|
5
INT 21H
RET
COURSOR: ; Set the coursor position to the end of the file MOV BX, AX
MOV CX, 0
MOV DX, 0
MOV AL, 2
MOV AH, 42H
INT 21H
RET
WRITE: ; Write into the file
MOV CL, [MES+1]
LEA DX, [MES+2]
MOV AH, 40H
INT 21H
RET
CLOSEFILE: ; Close file
MOV AH, 3EH
INT 21H
RET
INPUT: ; input string
MOV AH, 0AH
INT 21h
RET
PRINT: ; output notification
MOV AH, 09H
INT 21H
RET
6
NEXTLINE: ; Warp to the next line
MOV AH, 02H
MOV DL, 10
INT 21h
RET
FILENAME DB 128, 0, 128 DUP(0)
MES DB 128, 128 DUP('$')
WELCOME DB 'Enter filename: $'
NOFILE DB 'There is no file with that name. It was created.$' STR DB 'Enter the message to write: $'
BYE DB 'Done.$'
CODE ENDS
END START
3.2. Создание исполняемого .com файла
Воспользуемся методикой из Пр1:
CMD>> masm pract4.asm
CMD>> link pract4.obj
CMD>> exetobin pract4.exe pract4.com
7
3.3. Демонстрация результата
Рис. 3.1
Рис. 3.2
8
