好久没更blog了,微机原理要一个汇编大作业。经过两天的预习。写了一个记事本。其中有很多漏洞,不过勉强能用。
环境:Masm for Windows 2012.5【64位】
实现功能:
1、创建文件
2、编辑文件
3、读文件
4、删除文件
主要思想:
太简单了,没有什么思想orz。
Talk is cheap , show me your code.【代码不是一般的丑,改改应该可以改成2、300行的代码,但是还有好几门要预习的课,就不修改了【扑哧】】
DATAS SEGMENT
STR1 DB '成功创建文件$'
SYMBOL DB 20H, 08H,0AH,24H
BUFFER DB 300 DUP(?)
FILENAME DB 300 DUP(?)
CREATE DB 'create$'
DELETE DB 'delete$'
READ DB 'read$'
EDIT DB 'edit$'
SUCCESS DB 'success$'
FAIL DB 'fail$'
STEP DB 'STEP1$'
NOSTEP DB 'NOSTEP$'
FILESUCCESSMESSAGE DB 'create file successfully!$'
FILEFAILMESSAGE DB 'create file failly!$'
ERRORFAILMESSAGE DB 'cant find this command!$'
READSUCCESSMESSAGE DB 'read file successfully$'
READFAILMESSAGE DB 'read file failly$'
DELETESUCCESSMESSAGE DB 'delete file successfully$'
FLAG DB ?
COMMAND DB ?
HANDLE DW ?
COUNT DW ?
TOKENFLAG DW ?
DATAS ENDS
STACKS SEGMENT
DB 100 DUP(?)
STACKS ENDS
ESGE SEGMENT
ESGE ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS, ES:ESGE
START:
MOV AX,DATAS
MOV DS,AX
MOV SI, 0;SI置零
MOV COUNT, 0;COUNT置零
LOP:
MOV AH, 01H
INT 21H;回显输入单个字符
;----------------------------------------------------------------
CMP AL, [SYMBOL]
JZ TOKEN;判断是否是空格,如果是空格则跳转到TOKEN
;----------------------------------------------------------------
CMP AL, [SYMBOL+1];判断是否是DEl键,如果是则将SI减1,并重新输出
MOV FLAG, 00H;将FLAG置零,DELETECALL可以返回到LOP
JZ DELETECALL
;----------------------------------------------------------------
MOV [BUFFER+SI], AL;如果不是空格和退格将字符存入缓冲区
ADD SI, 1
ADD COUNT, 1
LOOP LOP;循环
LOP2:
MOV AH, 01H
INT 21H;回显输入单个字符
;----------------------------------------------------------------
CMP AL, [SYMBOL+1];判断是否是DEl键,如果是则将SI减1,并重新输出
MOV FLAG, 03H;将FLAG置零,DELETECALL可以返回到LOP
JZ DELETECALL
;----------------------------------------------------------------
CMP AL, [SYMBOL+3];判断是否是退出编辑键
JZ KEEPFILE
;----------------------------------------------------------------
MOV [BUFFER+SI], AL;如果不是空格和退格将字符存入缓冲区
ADD SI, 1
ADD COUNT, 1
LOOP LOP2;循环
TOKEN:
SUB COUNT, 1
SUB SI, 1;0-SI是字符串内容
;识别单个词法单元【命令】,然后继续识别
;MOV [BUFFER+SI], 24H
;LEA DX, BUFFER
;MOV AH, 09H
;INT 21H
;显示指令
;----------------------------------------------------------------
;识别是否是'create'指令
TOKENCREATE:
CMP SI, 05H
;判断长度,如果不是6则跳转到TOKENEDIT
JNZ TOKENREAD
MOV CX, SI
MOV AX, DATAS
MOV DS, AX
MOV ES, AX
LEA SI, BUFFER
LEA DI, CREATE
REPZ CMPSB
MOV COMMAND,00H
JZ INPUTFILENAME;如果是'create'则跳转
JNZ TOKENDELETE;如果不是则跳转到'TOKENDELETE'
;---------------------------------------------------------------
TOKENREAD:
CMP SI, 03H
JNZ ERRORCALL
MOV CX, SI
MOV AX, DATAS
MOV DS, AX
MOV ES, AX
LEA SI, BUFFER
LEA DI, READ
REPZ CMPSB
MOV COMMAND,01H
JZ INPUTFILENAME;是读命令则跳转到输入“文件名字”
JNZ TOKENEDIT;不是则跳转到"TOKENEDIT"
TOKENEDIT:
MOV AX, 03H
MOV SI, AX
CMP SI, 03H
JNZ ERRORCALL
MOV CX, SI
MOV AX, DATAS
MOV DS, AX
MOV ES, AX
LEA SI, BUFFER
LEA DI, EDIT
REPZ CMPSB
MOV COMMAND, 02H
JZ INPUTFILENAME;是编辑命令则跳转到输入“文件名字”
JNZ ERRORCALL;如果不是则跳转到'ERRORCALL'
TOKENDELETE:
MOV SI, 05H
MOV CX, SI
MOV AX, DATAS
MOV DS, AX
MOV ES, AX
LEA SI, BUFFER
LEA DI, DELETE
REPZ CMPSB
MOV COMMAND,03H
JZ INPUTFILENAME;如果是'create'则跳转
JNZ ERRORCALL;如果不是则跳转到'ERROECALL'
DELETECALL:
CMP COUNT, 00H
JZ S0
CMP COUNT, 01H
SUB SI, 1
SUB COUNT, 1
JZ S1
JNZ S2
;----------------------------------------------------------------
S0: MOV BH, 00H
MOV AH, 03H
INT 10H
;得到光标的位置
;----------------------------------------------------------------
JZ NEXT1
S1:
MOV [BUFFER+SI], 24H;将光标前一个位置设为终结符
;----------------------------------------------------------------
MOV DL, 20H
MOV AH, 02H
INT 21H
MOV BH, 00H
MOV AH, 03H
INT 10H
;得到光标的位置
;----------------------------------------------------------------
SUB DL, 1
MOV AH, 02H
INT 10H
JZ NEXT1
S2:
MOV [BUFFER+SI], 24H;将光标前一个位置设为终结符
;----------------------------------------------------------------
MOV DL, 20H
MOV AH, 02H
INT 21H
;输出空格覆盖删掉的字符
;----------------------------------------------------------------
MOV BH, 00H
MOV AH, 03H
INT 10H
;得到光标的位置
;----------------------------------------------------------------
MOV AX, DX
MOV AH, 00H
SUB AX, 1
MOV DL, 4FH
DIV DL
;判断光标位置是否是4F的倍数
;获得当前光标位置,调用参数BH=页号
;返回参数CH = 光标开始行,C L= 光标结束行
;返回参数DH = 行, DL = 列
;----------------------------------------------------------------
CMP AH, 00H
JZ STEP1
JNZ STEP2
STEP1:
MOV BH, 00H
MOV AH, 03H
INT 10H
;得到光标的位置
;----------------------------------------------------------------
SUB DH, 01H
ADD DL, 4EH
MOV AH, 02H
INT 10H
MOV DL, 20H
MOV AH, 02H
INT 21H
;回到上一行的末尾
MOV BH, 00H
MOV AH, 03H
INT 10H
;得到光标的位置
;----------------------------------------------------------------
SUB DH, 01H
ADD DL, 4FH
MOV AH, 02H
INT 10H
JMP NEXT1
;光标位置已经为0,退行,列为终点
;----------------------------------------------------------------
STEP2:
MOV BH, 00H
MOV AH, 03H
INT 10H
;得到光标的位置
;----------------------------------------------------------------
SUB DL, 01H
MOV AH, 02H
INT 10H
;列减一
;----------------------------------------------------------------
NEXT1:
CMP FLAG, 00H
JZ LOP
CMP FLAG, 01H
JZ LOP1
CMP FLAG, 03H
JZ LOP2
MOV AH, 4CH
INT 21H
;回到循环位置
;----------------------------------------------------------------
INPUTFILENAME:
MOV CX, SI
MOV SI, 00H
ZERO:
MOV [BUFFER+SI], 00H
ADD SI, 1
SUB CX, 1
CMP CX, 00H
JNZ ZERO
MOV SI, 0
MOV COUNT, 0
;缓冲区清零
LOP1:
MOV AH, 01H
INT 21H
;回显输入单个字符
CMP AL, 0DH
JNZ SWITCH2
SWITCH1:
CMP COMMAND, 00H
JZ CREATEFILE
CMP COMMAND,01H
JZ READFILE
CMP COMMAND, 02H
JZ EDITFILE
CMP COMMAND, 03H
JZ DELETEFILE
MOV AH, 4CH
INT 21H
SWITCH2:
CMP AL, [SYMBOL+1];判断是否是DEl键,如果是则将SI减1,并重新输出
MOV FLAG,01H;将FLAG置01H,使DELETECALL可以返回到LOP1
JZ DELETECALL
;----------------------------------------------------------------
MOV [BUFFER+SI], AL;将字符存入缓冲区
ADD SI, 1
ADD COUNT, 1
LOOP LOP1;循环
MOV AH, 4CH
INT 21H
CREATEFILE:
MOV [BUFFER+SI], 24H
LEA DX, BUFFER
MOV CX, 00H
MOV AH, 3CH
INT 21H
;根据字符串内容创建文件
MOV HANDLE, AX
JC FAILCREATEFILE
JNC SUCCESSCREATEFILE
;如果成功创建则输出成功信息
;-----------------------------------------------------------------
READFILE:
MOV [BUFFER+SI], 24H
LEA DX, BUFFER
MOV AL, 02H
MOV AH, 3DH
INT 21H
;获得文件句柄,打开文件
;-----------------------------------------------------------------
MOV HANDLE, AX
MOV BX, AX
LEA DX, BUFFER
MOV CX, 100
MOV AH, 3FH
INT 21H
;读文件
MOV BX, AX
MOV BUFFER[BX], 24H
LEA DX, BUFFER
MOV AH, 09H
INT 21H
MOV DL, 0AH
MOV AH, 02H
INT 21H
;显示文件
;-----------------------------------------------------------------
MOV BX, HANDLE
MOV AH, 3EH
INT 21H
;关闭文件
;-----------------------------------------------------------------
JMP SUCCESSREADFILE
MOV AH, 4CH
INT 21H
EDITFILE:
MOV [BUFFER+SI], 24H
MOV CX, SI
MOV AX, DATAS
MOV DS, AX
MOV ES, AX
LEA SI, BUFFER
LEA DI, FILENAME
REP MOVSB
;保留文件名字
LEA DX, BUFFER
MOV AL, 02H
MOV AH, 3DH
INT 21H
;获得文件句柄,打开文件
;-----------------------------------------------------------------
MOV CX, SI
MOV SI, 0
ZERO0:
MOV [BUFFER+SI], 00H
SUB CX, 1
ADD SI, 1
CMP CX, 00H
JNZ ZERO0
;缓冲区清零
MOV SI, 0
MOV COUNT, 0
MOV HANDLE, AX
MOV BX, AX
LEA DX, BUFFER
MOV CX, 100
MOV AH, 3FH
INT 21H
;读文件
MAKESUREBYTES:
MOV SI, 0
LOP4:
CMP [BUFFER+SI], 24H
JZ NEXT3
CMP [BUFFER+SI], 00H
JZ NEXT3
ADD SI, 1
LOOP LOP4
NEXT3:
MOV [BUFFER+SI], 24H
LEA DX, BUFFER
MOV AH, 09H
INT 21H
MOV [BUFFER+SI], 00H
MOV COUNT, SI
JMP LOP2
MOV AH, 4CH
INT 21H
KEEPFILE:
MOV [BUFFER+SI], 24H
LEA DX, FILENAME
MOV CX, 02H
MOV AH, 3CH
INT 21H
MOV HANDLE, AX
MOV BX, AX
LEA DX, BUFFER
MOV AH, 40H
MOV CX, 100
INT 21H
MOV DL, 0AH
MOV AH, 02H
INT 21H
MOV CX, SI
MOV SI, 00H
ZERO6:
MOV [BUFFER+SI], 00H
SUB CX, 1
ADD SI, 1
CMP CX, 00H
JNZ ZERO6
;缓冲区清零
MOV COUNT, 0
MOV SI, 0
MOV BX, HANDLE
MOV AH, 3EH
INT 21H
;关闭文件
;-----------------------------------------------------------------
JMP LOP
MOV AH, 4CH
INT 21H
DELETEFILE:
MOV [BUFFER+SI], 24H
LEA DX, BUFFER
MOV AH, 41H
INT 21H
LEA DX, DELETESUCCESSMESSAGE
MOV AH, 09H
INT 21H
MOV DL, 0AH
MOV AH, 02H
INT 21H
MOV CX, SI
MOV SI, 00H
ZERO5:
MOV [BUFFER+SI], 00H
SUB CX, 1
ADD SI, 1
CMP CX, 00H
JNZ ZERO5
;缓冲区清零
MOV SI, 0
MOV COUNT,0
JMP LOP
SUCCESSCREATEFILE:
MOV BX, HANDLE
MOV AH, 3EH
INT 21H
LEA DX, FILESUCCESSMESSAGE
MOV AH, 09H
INT 21H
MOV DL, 0AH
MOV AH, 02H
INT 21H
MOV CX, COUNT
MOV SI, 0
ZERO3:
MOV [BUFFER+SI], 00H
SUB CX, 1
ADD SI, 1
CMP CX, 00H
JNZ ZERO3
;缓冲区清零
MOV SI, 0
MOV COUNT, 0
JMP LOP
MOV AH, 4CH
INT 21H
;输出成功创建文件提示
;-----------------------------------------------------------------
FAILCREATEFILE:
LEA DX, FILEFAILMESSAGE
MOV AH, 09H
INT 21H
MOV AH, 4CH
INT 21H
;输出失败创建文件提示
;-----------------------------------------------------------------
SUCCESSREADFILE:
MOV BX, HANDLE
MOV AH, 3EH
INT 21H
MOV CX, COUNT
MOV SI, 0
ZERO4:
MOV [BUFFER+SI], 00H
SUB CX, 1
ADD SI, 1
CMP CX, 00H
JNZ ZERO4
;缓冲区清零
MOV COUNT, 0
MOV SI, 0
JMP LOP
MOV AH, 4CH
INT 21H
;输出成功读取文件提示
;-----------------------------------------------------------------
FAILREADFILE:
LEA DX, READFAILMESSAGE
MOV AH, 09H
INT 21H
MOV AH, 4CH
INT 21H
;输出失败读取文件提示
;-----------------------------------------------------------------
ERRORCALL:
LEA DX, ERRORFAILMESSAGE
MOV AH, 09H
INT 21H
MOV AH, 4CH
INT 21H
CODES ENDS
END START