📄 file.asm
字号:
assume cs:code
data segment
smaxlen db 10
sactlen db ?
_sname db 10 dup(' ')
nmaxlen db 10
nactlen db ?
_name db 10 dup(' ')
pmaxlen db 6
pactlen db ?
_phone db 6 dup(' '),0ah,0dh
ioarea db 18 dup(' '),'$'
ioarea1 db 18 dup(' ')
ioarea2 db 18 dup(' ')
handle dw ?
curfp dw ?
backnum dw ?
readnum dw ?
recnum dw 11
recnum_asc db 3 dup ('A'),0ah,0dh,'$'
openErroFlag db 0
readErroFlag db 0
createErroFlag db 0
endfileflag db 0
c_d db 0ah,0dh,'$'
_eof db 1ah
prompt db 'Input the names:',0dh,0ah,'$'
filename db 'phones.txt',0
createErr db 'Create file exception!',0dh,0ah,'$'
openErr db 'Open file error',0dh,0ah,'$'
readErr db 'Read file error',0dh,0ah,'$'
phoneErr db 'Phone number can only be 0-9!',0dh,0ah,'$'
fpbackErr db 'file point back error',0dh,0ah,'$'
cmd db '*****0 for view All, 1 for insert, 2 for select, 3 for EXIT*****',0ah,0dh,'$'
ins1 db '*****Input the Name: ','$'
ins2 db '*****Input the Phone: ','$'
show1 db 'Names_____Phones',0ah,0dh,'$'
show2 db '___________Total:$'
sel db '*****Input who U wanna select: ','$'
data ends
stack segment
dw 128 dup(?)
stack ends
code segment
start:
mov ax,stack
mov ss,ax
mov sp,256
mov ax,data
mov ds,ax
mov es,ax
prog_beg:
call openfile
cmp openErroFlag,1
jz create_it
jmp prog_beg1
create_it:
call createfile
prog_beg1:
lea dx,cmd
mov ah,9
int 21h
mov ah,0
int 16h
cmp al,'0'
jz _viewall
cmp al,'1'
jz _insert
cmp al,'2'
jz _select
cmp al,'3'
jz main_ext
main_ext:
call closefile
mov ax,4c00h
int 21h
_viewall:
call fpoint_s
call viewall
jmp prog_beg1
_select:
call fpoint_s
call write_selname
call cmpnames
jmp prog_beg1
_insert:
call fpoint_e
call insert
call fsort
call closefile
jmp prog_beg
;--------------------
c_down proc
push ax
push dx
lea dx,c_d
mov ah,9
int 21h
pop dx
pop ax
ret
c_down endp
;--------------------
write_selname proc
push ax
push dx
call clr_sname
s1: lea dx,sel
mov ah,9
int 21h
mov ah,0ah
lea dx,smaxlen
int 21h
cmp sactlen,0
jz s1
mov al,20h
lea bx,_sname
add bl,sactlen
mov [bx],al
call c_down
pop dx
pop ax
ret
write_selname endp
;---------------------
cmpnames proc
push cx
push di
push si
mov endfileflag,0
cmps_start:
mov readnum,18
call readfile
cmp readErroFlag,0
jz cmps_EOF
lea dx,readErr
mov ah,9
int 21h
jmp ext_cmps
cmps_EOF:
cmp endfileflag,0
jz goon2
jmp ext_cmps
goon2: lea si,_sname
lea di,ioarea
mov cx,5
repz cmpsw
cmp cx,0
jnz cmp_next
call displayio
cmp_next:
jmp cmps_start
ext_cmps:
pop si
pop di
pop cx
ret
cmpnames endp
;----------------------
insert proc
push ax
push bx
push cx
push dx
call clr_name
call clr_phone
i1: lea dx,ins1
mov ah,9
int 21h
mov ah,0ah
lea dx,nmaxlen
int 21h
cmp nactlen,0
jz i1
i2: mov al,20h
mov bx,offset _name
add bl,nactlen
mov ds:[bx],al
call c_down
i3: lea dx,ins2
mov ah,9
int 21h
mov ah,0ah
lea dx,pmaxlen
int 21h
cmp pactlen,0
jz i3
lea si,_phone
mov cx,5
_check: mov al,ds:[si]
cmp al,30h
jb redo
cmp al,39h
ja redo
inc si
loop _check
jmp i4
redo: call clr_phone
lea dx,phoneErr
mov ah,9
int 21h
jmp i3
i4: call c_down
mov al,20h
mov bx,offset _phone
add bl,pactlen
mov [bx],al
mov ah,40h
mov bx,handle
mov cx,10
lea dx,_name
int 21h
mov ah,40h
mov bx,handle
mov cx,8
lea dx,_phone
int 21h
pop dx
pop cx
pop bx
pop ax
ret
insert endp
;----------------------
viewall proc
mov endfileflag,0
mov readnum,18
lea dx,show1
mov ah,9
int 21h
contin: call readfile
cmp readErroFlag,0
jz is_EOF
jmp ext_view
is_EOF: cmp endfileflag,0
jz goon
jmp ext_view
goon: call displayio
jmp contin
ext_view:
lea dx,show2
mov ah,9
int 21h
mov bx,0 ;clear recnum_asc
mov cx,3 ;
clr_asc:
mov ds:recnum_asc[bx],20h ;
inc bx ;
loop clr_asc ;
call get_curfp ;set recnum !
mov ax,curfp ;
mov bl,18 ;
div bl ;
mov recnum,ax ;
call d2c ;trans recnum into ascII
lea dx,recnum_asc ;show total
mov ah,9 ;
int 21h ;
ret
viewall endp
;------------------------
d2c proc
push ax
push bx
push cx
push dx
push si
mov si,0
mov cx,0
mov ax,recnum
mov bl,10
d2c_loop:
div bl
push ax
inc cx
cmp al,0
jz d2c_over
mov ah,0
jmp short d2c_loop
d2c_over:
pop ax
add ah,30h
mov ds:recnum_asc[si],ah
inc si
loop d2c_over
d2c_ext: pop si
pop dx
pop cx
pop bx
pop ax
ret
d2c endp
;-------------------
get_curfp proc
push ax
push bx
push cx
push dx
mov ax,4201h
mov bx,handle
mov cx,0
mov dx,0
int 21h
mov curfp,ax
pop dx
pop cx
pop bx
pop ax
ret
get_curfp endp
;--------------------
fsort proc
push ax
push bx
push cx
push dx
push si
mov readnum,36
upper_group:
call get_curfp
cmp curfp,36
jb ext_fsort
mov backnum,-36
call fpoint_back ;back only if it can back
mov si,0
call readfile
mov cx,9
cmpbyte:
mov al,ds:ioarea1[si]
cmp al,ds:ioarea2[si]
ja xchg_yes
jb xchg_no
inc si ;camp next byte
loop cmpbyte
xchg_no:
mov backnum,-18 ;these 2 names need not xchg
call fpoint_back
jmp upper_group ;camp upper 2 names
xchg_yes:
mov si,0
mov cx,9
xchg_byte:
mov ax,word ptr ds:ioarea1[si]
xchg ax,word ptr ds:ioarea2[si]
xchg ax,word ptr ds:ioarea1[si]
add si,2
loop xchg_byte
mov backnum,-36
call fpoint_back
mov bx,handle
lea dx,ioarea1
mov cx,36
mov ah,40h
int 21h
jmp xchg_no
ext_fsort:
pop si
pop dx
pop cx
pop bx
pop ax
ret
fsort endp
;--------------------
fpoint_back proc
push ax
push bx
push cx
push dx
call get_curfp
mov ax,backnum
add ax,curfp
js ext_bk
mov bx,handle
mov cx,0
mov dx,backnum
cmp dx,0
jge point
not cx
point: mov al,1
mov ah,42h
int 21h
jc back_err
ext_bk: pop dx
pop cx
pop bx
pop ax
ret
back_err:
call c_down
lea dx,fpbackErr
mov ah,9
int 21h
call c_down
jmp ext_bk
fpoint_back endp
;---------------------
fpoint_e proc
push ax
push bx
push cx
push dx
mov ah,42h
mov al,2
mov bx,handle
mov cx,0
mov dx,0
int 21h
pop dx
pop cx
pop bx
pop ax
ret
fpoint_e endp
;---------------------
fpoint_s proc
push ax
push bx
push cx
push dx
mov ah,42h
mov al,0
mov bx,handle
mov cx,0
mov dx,0
int 21h
pop dx
pop cx
pop bx
pop ax
ret
fpoint_s endp
;-----------------------
clr_ioarea proc
push ax
push di
push cx
lea di,ioarea
mov cx,18
mov al,20h
rep stosb
pop cx
pop di
pop ax
ret
clr_ioarea endp
;----------------------
clr_name proc
push ax
push di
push cx
mov al,20h
lea di,_name
mov cx,10
rep stosb
pop cx
pop di
pop ax
ret
clr_name endp
;----------------------
clr_sname proc
push ax
push di
push cx
mov al,20h
lea di,_sname
mov cx,10
rep stosb
pop cx
pop di
pop ax
ret
clr_sname endp
;----------------------
clr_phone proc
push ax
push di
push cx
mov al,20h
lea di,_phone
mov cx,6
rep stosb
pop cx
pop di
pop ax
ret
clr_phone endp
;----------------------
displayio proc
push ax
push dx
mov ah,9
lea dx,ioarea
int 21h
pop dx
pop ax
ret
displayio endp
;----------------------
readfile proc
push ax
push bx
push cx
push dx
call clr_ioarea
mov ah,3fh
mov bx,handle
mov cx,readnum
cmp readnum,18
je r1
lea dx,ioarea1
jmp r0
r1: lea dx,ioarea
r0: int 21h
jc err_read ;error on read!
cmp ax,0
jz ext_read2 ;end of file!
cmp readnum,18
jnz r2
cmp ioarea,1ah
jmp r3
r2: cmp ioarea1,1ah
r3: jnz ext_read ;not Eof marker!
ext_read2:
mov endfileflag,1 ;enf of file or reach eof marker
ext_read:
pop dx
pop cx
pop bx
pop ax
ret
err_read:
mov readErroFlag,1
lea dx,readErr
mov ah,9
int 21h
jmp ext_read
readfile endp
;----------------------
openfile proc
push ax
push dx
mov openErroFlag,0
mov ah,3dh
mov al,02h
lea dx,filename
int 21h
jc err_open
mov handle,ax
ext_open:
pop dx
pop ax
ret
err_open:
mov openErroFlag,1 ;open error!
lea dx,openErr
mov ah,9
int 21h
jmp ext_open
openfile endp
;----------------------
createfile proc
push ax
push bx
push cx
push dx
mov ah,3ch
mov cx,0
lea dx,filename
int 21h
jc err_create
mov handle,ax
create_ext:
pop dx
pop cx
pop bx
pop ax
ret
err_create:
mov createErroFlag,1 ;create error
lea dx,createErr
mov ah,9h
int 21h
jmp create_ext
createfile endp
;---------------------
closefile proc
push ax
push bx
push cx
push dx
mov bx,handle
mov ah,3eh
int 21h
pop dx
pop cx
pop bx
pop ax
ret
closefile endp
;--------------------------
code ends
end start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -