asm实现类似电话薄功能

a.提示输入姓名回车完成;

b.提示输入电话号码回车完成;

c.自动保存到D:\phonebook.txt;

d.在程序运行的时候显示已有的电话名单;

e.输入exit退出程序。

 

代码
;file: phonebook.asm
;
date: 2010.12.12
LEN = 20
DATAS SEGMENT
filename db
'D:\phonebook.txt',0
k_exit db
'exit'
errMsg1 db
'File not found',13,10,'$'
errCreate db
'Create file error',13,10,'$'
errOpen db
'Open file error',13,10,'$'
errRead db
'Read error',13,10,'$'
errWrite db
'Write error',13,10,'$'
msgName db
'Please input user name: $'
msgTel db
'Please input tel number: $'
nameBuf db
20 dup(0)
telBuf db
20 dup(0)
tmpBuf db
20 dup(0)
inputBuf db LEN
db ?
db LEN dup(
0)
buf db ?
DATAS ENDS
STACKS SEGMENT
STACKS ENDS
CODES SEGMENT
ASSUME
CS:CODES,DS:DATAS,SS:STACKS
isexit PROC
xor dx,dx
mov dl,inputBuf+1
sub dl,4
mov si,offset inputBuf+2
add si,dx
lea di,k_exit
mov cx,4
xor ax,ax
cld
@@:
cmpsb
jne isexit2
inc ax
loop @b
isexit2:
ret
isexit ENDP
getInputBufLen proc
mov al,inputBuf+1
ret
getInputBufLen endp
inputMsg proc
lea dx,inputBuf
mov ah,0AH
int 21h
ret
inputMsg endp
writeChar proc
;Param in: al
push di
lea di,buf
mov [di],al
call writeFile
test al,al
je writeChar2
writeChar2:
pop di
ret
writeChar endp
writeEnter proc
push di
lea di,buf
mov al,13
mov [di],al
call writeFile
test al,al
je writeEnter2
;
mov al,10
mov [di],al
call writeFile
test al,al
je writeEnter2
writeEnter2:
pop di
ret
writeEnter endp
writeMsg proc
push si
push di
lea si,tmpBuf
lea di,buf
writeMsg1:
lodsb
cmp al,'$'
je writeMsg2
mov [di],al
call writeFile
test al,al
je writeMsg2
jmp writeMsg1
writeMsg2:
pop di
pop si
ret
writeMsg endp
getMsg proc
push si
push di
cld
xor cx,cx
mov di,offset tmpBuf
mov si,offset inputBuf + 2
mov cl,[si-1]
rep movsb
mov [di],'$'
pop di
pop si
ret
getMsg endp
enter proc
push dx
mov dl,13
mov ah,2
int 21h
mov dl,10
mov ah,2
int 21h
pop dx
ret
enter endp
showChar proc
push dx
mov dl,al
mov ah,2
int 21h
pop dx
ret
showChar endp
showMsg proc
;Param In:si
push dx
mov dx,si
mov ah,9
int 21h
pop dx
ret
showMsg endp
fileExists proc
;Param Out:al
lea dx,fileName
mov ax,3D00H
int 21h
jnc @f
jmp notexist
@@:
mov bx,ax
mov ax,3E00h
int 21h
mov al,1
ret
notexist:
xor al,al
ret
fileExists endp
createFile proc
lea dx,fileName
xor cx,cx
mov ax,3C00H
int 21h
mov bx,ax
ret
createFile endp
openFile proc
lea dx,fileName
mov ax,3D02H
int 21h
mov bx,ax
ret
openFile endp
closeFile proc
mov ax,3E00H
int 21h
closeFile endp
readFile proc
;Param out: al
lea dx,buf
mov cx,1
mov ah,3FH
int 21h
cmp ax,cx
jb read1
jmp read2
read1:
xor al,al
ret
read2:
mov al,buf
ret
readFile endp
writeFile proc
;Param out: al
lea dx,buf
mov cx,1
mov ah,40H
int 21h
cmp ax,cx
jb write1
jmp write2
write1:
xor al,al
ret
write2:
mov al,buf
ret
writeFile endp
START:
MOV AX,DATAS
MOV DS,AX
MOV ES,AX
call fileExists
test al,al
jz _create
jmp _open
_create:
call createFile
test al,al
jz _createErr
jmp _open
_open:
call openFile
jc _openErr
jmp _read
_read:
call readFile
test al,al
jz _readOK
call showChar
jmp _read
_write:
_writeName:
call enter
lea si,msgName
call showMsg
call inputMsg
call getMsg
call isexit
cmp ax,4
je _close
call getInputBufLen
test al,al
jz _writeName
call writeMsg
mov al,':'
call writeChar
;
_writeTel:
call enter
lea si,msgTel
call showMsg
call inputMsg
call getMsg
call isexit
cmp ax,4
je _close
call getInputBufLen
test al,al
jz _writeTel
call writeMsg
call writeEnter
jmp _write
_openErr:
lea si,errOpen
call enter
call showMsg
jmp _exit
_createErr:
lea si,errCreate
call enter
call showMsg
jmp _exit
_readErr:
lea si,errRead
call enter
call showMsg
jmp _close
_readOK:
jmp _write
_close:
call closeFile
jmp _exit
_exit:
MOV AH,4CH
INT 21H
CODES ENDS
END START

 

 

posted @ 2010-12-17 10:56  Jekhn  阅读(276)  评论(0编辑  收藏  举报