汇编实现的简单的通讯录系统,有插入,修改,删除,打印,查找功能

2016-12-30 13:47

是这学期的作业,个人感觉还不错,分享给大家,还有很多可以改进的地方,希望大家不吝指教。




代码如下:



DATAS SEGMENT
NOTICE1 DB "1:insert contacts",10,13,'$'
NOTICE2 DB "2:cancel contacts",10,13,'$'
NOTICE3 DB "3:modify contacts",10,13,'$'
NOTICE4 DB "4:search  contacts",10,13,'$'
NOTICE5 DB "q:exit"      ,10,13,'$' 
NOTICE6 DB "Please choose function options and input 1,2,3,4,5 or q",10,13,'$'
NOTICE7 DB "5:print the contacts list",10,13,'$'
NOTICE8 DB "You can save 20 contacts",10,13,'$'
NOTICE9 DB "Sorry, you have saved 20 cantacts,there is no place to save more",10,13,'$'
NOTICE10 DB "Please input the name of the contact(less than 8 characters)",10,13,'$'
NOTICE11 DB "Please input the phone number of the contact",10,13,'$'
NOTICE12 DB "Operation succeeded",10,13,'$'
NOTICE13 DB "No contacts have been saved",10,13,'$'
NOTICE14 DB " : ",'$'
NOTICE15 DB "**NAME*****NUMBER***",10,13,'$'
NOTICE16 DB "1:search by name",10,13,"2:search by telnumber",10,13,'$'
NOTICE17 DB "Not found",10,13,'$'
NOTICE18 DB "Delete this contact?  Y/N",10,13,'$'
NAMEINPUT    DB 9,0, 9 DUP('$')
NUMBERINPUT  DB 12,0,12 DUP('$')
CRNL    DB 10,13,'$'
TXL     DB 0, 20 DUP(8 DUP(?),'$',11 DUP(?),'$')
DATAS ENDS




STACKS SEGMENT
    DB 128 DUP(?)
STACKS ENDS




CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
    MOV AX,DATAS
    MOV DS,AX
    
    MOV AX,STACKS
    MOV SS,AX
    MOV SP,0
INIT:
MOV DX,OFFSET NOTICE1
MOV AH,09H
INT 21H 

    MOV DX,OFFSET NOTICE2
    MOV AH,09H
    INT 21H
    
    MOV DX,OFFSET NOTICE3
    MOV AH,09H
    INT 21H
    
    MOV DX,OFFSET NOTICE4
    MOV AH,09H
    INT 21H
    
    MOV DX,OFFSET NOTICE7
    MOV AH,09H
    INT 21H
    
    MOV DX,OFFSET NOTICE5
    MOV AH,09H
    INT 21H
    
    MOV DX,OFFSET CRNL
    MOV AH,09H
    INT 21H
INIT1:  
  MOV DX,OFFSET NOTICE6
  MOV AH,09H
  INT 21H
 
  MOV AH,01H
  INT 21H
  MOV DX,OFFSET CRNL
  MOV AH,09H
  INT 21H
  CMP AL,'1'
  JE INSERT
  CMP AL,'2'
  JE CANCEL
  CMP AL,'3'
  JE MODIFY
  CMP AL,'4'
  JE SEARCH
  CMP AL,'5'
  JE NEAR PTR PRINT
  CMP AL,'Q'
  JE EXIT
  CMP AL,'q'
  JE EXIT
  JMP INIT1
 
    
INSERT:
MOV AL,14H
MOV BX,OFFSET TXL
CMP DS:[BX],AL
JAE A
JB  B 
A:
MOV DX,OFFSET NOTICE9
MOV AH,09H
INT 21H
JMP NEAR PTR INIT
B:
MOV DX,OFFSET NOTICE10
MOV AH,09H
INT 21H
CALL CLEARNAMEINPUT
MOV DX,OFFSET NAMEINPUT
MOV AH,0AH
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H


MOV DX,OFFSET NOTICE11
MOV AH,09H
INT 21H
MOV DX,OFFSET NUMBERINPUT
MOV AH,0AH
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
MOV BX,OFFSET TXL
MOV AH,DS:[BX] ;AH为参数,存放写入的位置
CALL COPYNAME

MOV DX,OFFSET NOTICE12
MOV AH,09H
INT 21H
MOV BX,OFFSET TXL
INC BYTE PTR DS:[BX]
JMP NEAR PTR INIT

COPYNAME:
MOV BX,OFFSET NAMEINPUT
MOV BP,OFFSET TXL
MOV AL,15H
MUL AH
MOV SI,AX

MOV CL,15H
MOV CH,0H
PUSH SI ;保存SI
CLEAR:
MOV AL,'$'
MOV DS:[BP+SI+1],AL
INC SI
LOOP CLEAR

MOV DI,0H
MOV CL,DS:[BX+1]
MOV CH,0H
POP SI
PUSH SI
S1:
MOV AL,DS:[BX+DI+2]
MOV DS:[BP+SI+1],AL
INC SI
INC DI
LOOP S1
COPYNUMBER:
MOV CX,0BH
MOV BX,OFFSET NUMBERINPUT
MOV BP,OFFSET TXL
POP SI
MOV DI,0H
S2:
MOV AL,DS:[BX+DI+2]
MOV DS:[BP+SI+10],AL
INC SI
INC DI
LOOP S2
RET

CANCEL:
CALL FIND
CMP AX,0H
JE NEAR PTR NOTFOUND
DEC AX
PUSH AX
MOV AH,AL
MOV AL,15H
MUL AH
MOV SI,AX
PUSH SI  ;保存 SI
MOV DX,OFFSET NOTICE15
MOV AH,09H
INT 21H

MOV BX,OFFSET TXL
ADD BX,SI
ADD BX,01H
MOV DX,BX
MOV AH,09H
INT 21H
MOV DX,OFFSET NOTICE14
MOV AH,09H
INT 21H
ADD BX,09H
MOV DX,BX
MOV AH,09H
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
INITC:
MOV DX,OFFSET NOTICE18
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
CMP AL,'Y'
JE DELETE
CMP AL,'y'
JE DELETE
CMP AL,'N'
JMP NEAR PTR INIT
CMP AL,'n'
JMP NEAR PTR INIT
JMP INITC
DELETE:
POP SI
POP CX ; 第(CX)条记录


COPY1:
PUSH CX
MOV CX,15H
MOV BX,OFFSET TXL
COPY2:
MOV AL,DS:[BX+SI+22]
MOV DS:[BX+SI+1],AL
INC SI
LOOP COPY2

POP CX
INC CX
CMP CX,DS:[BX]
JBE COPY1
DEC BYTE PTR DS:[BX]
MOV DX,OFFSET NOTICE12
MOV AH,09H
INT 21H
JMP NEAR PTR INIT







MODIFY:
CALL FIND
CMP AX,0H
JE NEAR PTR NOTFOUND
PUSH AX    ;
DEC AX
MOV AH,AL
MOV AL,15H
MUL AH
MOV SI,AX

MOV DX,OFFSET NOTICE15
MOV AH,09H
INT 21H

MOV BX,OFFSET TXL
ADD BX,SI
ADD BX,01H
MOV DX,BX
MOV AH,09H
INT 21H

MOV DX,OFFSET NOTICE14
MOV AH,09H
INT 21H

MOV DX,BX
ADD DX,09H
MOV AH,09H
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H

MOV DX,OFFSET NOTICE10
MOV AH,09H
INT 21H
CALL CLEARNAMEINPUT
MOV DX,OFFSET NAMEINPUT
MOV AH,0AH
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H

MOV DX,OFFSET NOTICE11
MOV AH,09H
INT 21H
MOV DX,OFFSET NUMBERINPUT
MOV AH,0AH
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H

POP AX
MOV AH,AL
DEC AH
CALL COPYNAME

MOV DX,OFFSET NOTICE12
MOV AH,09H
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
JMP NEAR PTR INIT



CLEARNAMEINPUT:
PUSH BX
PUSH AX
PUSH SI
PUSH CX
MOV BX,OFFSET NAMEINPUT
MOV AL,'$'
MOV DS:[BX],AL
ADD BX,01H
MOV DS:[BX],AL
MOV SI,01H
MOV CX,09H
CN:
MOV DS:[BX+SI],AL
INC SI
LOOP CN
POP CX
POP SI
POP AX
POP BX
RET











SEARCH:
CALL FIND ;FIND 查找联系人,返回值AX为该联系人位置,
CMP AX,0H
JE NOTFOUND
DEC AL
MOV AH,15H
MUL AH
MOV SI,AX

MOV DX,OFFSET NOTICE15
MOV AH,09H
INT 21H

MOV DX,OFFSET TXL;NAME 
ADD DX,01H
ADD DX,SI
MOV AH,09H
INT 21H

PUSH DX
MOV DX,OFFSET NOTICE14   ;冒号
MOV AH,09H
INT 21H
POP DX

ADD DX,09H ;number
MOV AH,09H
INT 21H

MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
JMP NEAR PTR INIT


NOTFOUND:
MOV DX,OFFSET NOTICE17
MOV AH,09H
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
JMP NEAR PTR INIT


FIND:
MOV DX,OFFSET NOTICE16
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
CMP AL,'1'
JE SEARCHBYNAME
CMP AL,'2'
JE SEARCHBYNUMBER
JMP FIND
SEARCHBYNAME:
MOV DX,OFFSET NOTICE10
MOV AH,09H
INT 21H

MOV DX,OFFSET NAMEINPUT
MOV AH,0AH
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H

MOV BX,OFFSET TXL
MOV BP,OFFSET NAMEINPUT
MOV CL,DS:[BX]
MOV CH,0H
LOOP1:
PUSH CX
MOV AL,15H
DEC CL
MUL CL
MOV SI,AX
MOV DI,0H
MOV CL,DS:[BP+1]
MOV CH,0H
LOOP2:
MOV AL,DS:[BX+SI+1]
CMP AL,DS:[BP+DI+2]
JNE TOLOOP1
INC SI
INC DI
DEC CX
CMP CX,0H
JNE LOOP2
JE JUDGE
JUDGE:
MOV AL,DS:[BX+SI+1]
CMP AL,'$'
JE FOUND
JNE TOLOOP1
TOLOOP1:
POP CX
LOOP LOOP1
MOV AX,0H
RET
FOUND:
POP AX
RET

SEARCHBYNUMBER:
MOV DX,OFFSET NOTICE11
MOV AH,09H
INT 21H
MOV DX,OFFSET NUMBERINPUT
MOV AH,0AH
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H

MOV BX,OFFSET TXL
MOV BP,OFFSET NUMBERINPUT
MOV CL,DS:[BX]
MOV CH,0H
LOOP3:
PUSH CX
DEC CL
MOV AL,15H
MUL CL
MOV SI,AX
MOV CX,0BH
MOV DI,0H
LOOP4:
MOV AL,DS:[BX+SI+10]
CMP AL,DS:[BP+DI+2]
JNE TOLOOP3
INC SI
INC DI
LOOP LOOP4
POP AX
RET
TOLOOP3:
POP CX
LOOP LOOP3
MOV AX,0H
RET



PRINT:
MOV BX,OFFSET TXL
MOV CL,DS:[BX]
MOV CH,0H
CMP CX,0H
JE S4
JA S3
S4:
MOV DX,OFFSET NOTICE13
MOV AH,09H
INT 21H
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
JMP NEAR PTR INIT
S3:
MOV DX,OFFSET NOTICE15
MOV AH,09H
INT 21H

S5:
MOV AL,15H
PUSH CX
DEC CL
MUL CL
POP CX
MOV SI,AX
MOV DX,BX
ADD DX,01H
ADD DX,SI
MOV AH,09H
INT 21H  ;打印名字

PUSH DX
MOV DX,OFFSET NOTICE14 ;冒号
MOV AH,09H
INT 21H
POP DX

ADD DX,09H ;打印号码
MOV AH,09H
INT 21H

MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
LOOP S5
MOV DX,OFFSET CRNL
MOV AH,09H
INT 21H
JMP NEAR PTR INIT


EXIT:
MOV AH,4CH
    INT 21H

CODES ENDS
    END START