;这是我学汇编三月来的地一个程序,一个简单的编辑器,可以打开txt文件,还有许多需要改进的地方,
win macro fun,upline,attir,top,left,bottom,right
mov ah,fun
mov al,upline
mov bh,attir
mov ch,top
mov cl,left
mov dh,bottom
mov dl,right
int 10h
endm
cur macro page,row,col
mov bh,page
mov ch,05h
mov cl,07h
mov ah,01
int 10h
mov dh,row
mov dl,col
mov ah,02h
int 10h
endm
showstr macro string
mov dx,offset string
mov ah,09h
int 21h
endm
redc macro
mov ah,0
int 16h
endm
attir macro att,line,x,y
local next
pushall
push es
mov ax,0b800h
mov es,ax
mov ax,line
inc ax
mov bl,160
mul bl
mov dx,x
add dx,dx
add ax,dx
mov bx,ax
inc bx
mov cx,y
next:
mov byte ptr es:[bx],att
add bx,2
loop next
pop es
popall
endm
pushall macro
push ax
push bx
push cx
push dx
push si
push di
push es
push ds
push bp
endm
popall macro
pop bp
pop ds
pop es
pop di
pop si
pop dx
pop cx
pop bx
pop ax
endm
shln macro reg,n
mov cl,n
shl reg,cl
endm
hidcur macro
mov cx,2507h
mov ah,01h
int 10h
endm
negb macro reg,var
mov reg,var
neg reg
endm
newrow macro nrow ;当前行
negb ax,offsettop
mov ah,nrow
add al,ah
dec al
xor ah,ah
endm
newcol macro ncol ;当前列
negb ax,offsetleft
mov ah,ncol
add al,ah
xor ah,ah
endm
newbottom macro nbottom ;当前行
negb ax,offsettop
mov ah,nbottom
add al,ah
xor ah,ah
endm
******************************************************************************************
include mykey.mac
assume cs:cseg,ds:dseg,es:eseg,ss:stack
cseg segment 'code'
main proc far
mov ax,dseg
mov ds,ax
mov ax,eseg
mov es,ax
mov ax,stack
mov ss,ax
mov ax,1000h
mov sp,ax
call wind
call newfile
mov ax,4c00h
int 21h
main endp
cseg ends
assume cs:sseg
sseg segment 'code'
wind proc far
win 6,0,1fh,0,0,0,79
win 6,0,7dh,1,0,23,79
win 6,0,1fh,24,0,24,79
cur 0,0,2
showstr menu
cur 0,24,2
showstr msg1
ret
wind endp
edit proc far
key:
cur 0,24,50
mov dx,offset statusmsg
mov ah,9
int 21h
newrow row
mov bx,12
call winStatus
newcol col
mov bx,28
call winStatus
mov bx,46
xor ah,ah
mov al,bottom
call winStatus
cur 0,row,col
key_1:
redc
cmp ah,3bh
je show_menu
cmp ah,1ch
je edit_enter
cmp ah,48h
je edit_up
cmp ah,50h
je edit_down
cmp ah,4bh
je edit_left
cmp ah,4dh
je edit_right
cmp ah,0eh
je edit_back
cmp ah,53h
je edit_del
cmp al,20h
jb key_1
call EnterChar
call win1
jmp key
edit_enter:
cmp bflag,0
je enter_1
call DownMovBuf
enter_1:
inc bottom
call GetRowHead
negb ax,offsetleft ;ax=左偏移
cmp bx,ax
jnb enter_4
mov col,0
mov offsetleft,0
sub offsetleft,bx
jmp enter_5
enter_4:
;mov offsetleft,0
sub bx,ax
mov col,bl
enter_5:
inc row
cmp row,24
jne enter_3
mov row,23
dec offsettop
enter_3:
call win1
jmp key
edit_up:
mov bflag,1
newrow row
cmp al,0
je up_1
dec row
cmp row,0
jne up_1
mov row,1
inc offsettop
call win1
up_1:
;call SetBufTail
jmp key
edit_down:
newrow row
cmp al,bottom
jz down_1
inc row
jmp down_2
down_1:
cld
call GetRowAddr ;bx
mov di,bx
mov al,0
repz scasb
jcxz down_3
inc bottom
inc row
down_2:
cmp row,24
jne down_4
mov row,23
dec offsettop
call win1
jmp down_4
down_3:
mov bflag,0
down_4:
jmp key
edit_left:
mov bflag,1
newcol col
cmp al,0
je left_1
dec col
cmp col,0ffh
jne left_1
mov col,0
inc offsetleft
call win1
left_1:
jmp key
edit_right:
newcol col
cmp al,0feh
je right_1
inc col
cmp col,80
jne right_1
mov col,79
dec offsetleft
call win1
right_1:
jmp key
edit_back:
newcol col
xor ah,ah
cmp al,0 ;是头吗
je back_1 ;是转
newrow row
call GetRowAddr
mov di,bx
newcol col
add di,ax
mov dx,di
cld
mov cx,0fdh;
sub cx,ax
mov al,0
repz scasb
jcxz back_2 ;后面无字符转
mov di,dx
mov si,di
dec di
rep movsb buffer[di],es:buffer[si]
mov es:buffer[si],0
jmp back_14
back_2:
mov si,dx
dec si
mov es:buffer[si],0
back_14:
dec col
cmp col,0ffh
jne back_11 ;col!=-1,转
mov col,0
inc offsetleft
jmp back_11 ;col!=-1,转
;********************************************
back_1:
newrow row
cmp al,0
je back_12 ;是最左上角,转
dec al
xor ah,ah
call GetRowInfo ;AL=0,非空;AH=偏移;BX=行首址;DH=头偏移;DL=尾偏移
xor dh,dh
cmp al,0
jne back_3 ;空,转
mov di,bx
add di,0ffh
add di,0fch;0fdh
mov cx,0feh;0fdh
std
mov al,0
repz scasb
cld
jz back_6
mov di,bx
mov si,di
add si,0ffh
add di,dx
;inc cx
mov ax,cx
add cx,dx
cmp cx,0fdh
jbe back_4 ;小于或等于,转
mov cx,0fdh
sub cx,dx
rep movsb buffer[di],es:buffer[si]
mov cx,dx
inc cx
add di,2
rep movsb buffer[di],es:buffer[si]
mov cx,0fdh
sub cx,dx
mov al,0
rep stosb
jmp back_15
back_4:
mov cx,ax
;mov cx,0fdh
;sub cx,dx
rep movsb buffer[di],es:buffer[si]
jmp back_6
back_3:
xor cx,cx
mov cl,bottom
newrow row
sub cx,ax
inc cx
mov di,bx
mov si,di
add si,0ffh
jmp back_13
back_6:
xor cx,cx
mov cl,bottom
newrow row
sub cx,ax
inc cx
mov di,bx
add di,0ffh
mov si,di
add si,0ffh
;jcxz back_18
back_13:
mov bx,0
push cx
mov cx,0fdh
back_16:
mov al,es:buffer[si][bx]
mov es:buffer[di][bx],al
mov byte ptr es:buffer[si][bx],0
inc bx
loop back_16
add si,0ffh
add di,0ffh
pop cx
loop back_13
dec bottom
back_15:
dec row
cmp row,0
jne back_7
mov row,1
inc offsettop
back_7:
cmp dl,79
jbe back_19
mov offsetleft,79
xor dh,dh
sub offsetleft,dx
mov col,79
jmp back_11
back_19:
mov col,dl
back_11:
call win1
back_12:
jmp key
edit_del:
newcol col ;al
mov bl,al
newrow row ;al
mov ah,bl
call GetRowInfo
xor dh,dh
cmp al,0 ;ah=0,非空
jne del_1
cld
mov al,ah
xor ah,ah
mov cx,0fdh
sub cx,dx
mov di,bx
add di,ax
mov si,di
inc si
rep movsb buffer[di],es:buffer[si]
mov es:buffer[si],0
jmp del_4
del_1:
mov dl,ah
newrow row
cmp al,bottom
je del_2
mov di,bx
add di,0ffh
add di,0fdh
mov cl,ah
xor ch,ch
std
mov al,0
repz scasb
cld
jcxz del_3
del_3:
mov cx,0fdh
sub cx,dx
mov di,bx
mov si,di
add di,dx
add si,0ffh
rep movsb buffer[di],es:buffer[si]
newrow row
mov cl,bottom
sub cl,al
xor ch,ch
mov di,bx
add di,0ffh
mov si,di
add si,0ffh
del_6:
push cx
mov cx,0fdh
mov bx,0
del_5:
mov al,es:buffer[si][bx]
mov es:[di][bx],al
inc bx
loop del_5
add si,0ffh
add di,0ffh
pop cx
loop del_6
dec bottom
del_4:
call win1
del_2:
jmp key
show_menu:
call showmenu
call win1
jmp key
ret
edit endp
showmenu proc far
mov menuid,0
hidcur
menu_selt:
win 6,0,0dh,2,1,6,10
win 6,0,0adh,1,0,5,9
cur 0,1,0
showstr menu1
showstr menu2
showstr menu3
showstr menu4
showstr menu5
attir 3dh,menuid,0,10
redc
cmp ah,1ch
je inmenu
cmp ah,48h
je menu_up
cmp ah,50h
je menu_down
cmp ah,01h
jne menu_next
call hidemenu
ret
menu_next:
jmp menu_selt
menu_up:
dec menuid
cmp menuid,0
jnl menu_selt
mov menuid,4
jmp menu_selt
menu_down:
inc menuid
cmp menuid,5
jb menu_selt
mov menuid,0
jmp menu_selt
inmenu:
win 6,0,7dh,1,0,23,79
call win1
mov bx,menuid
shln bx,2
call dword ptr menufun[bx]
ret
showmenu endp
hidemenu proc far
win 6,0,7dh,1,0,6,10
ret
hidemenu endp
CharStack proc far
push ax
push bx
push cx
push si
mov pathlen,0
;mov si,offset filepath
push si
xor cx,cx
mov cl,filelist
mov al,0
stack_cls:
mov [si],al
inc si
loop stack_cls
pop si
stack_again:
mov ah,0
int 16h
cmp ah,01h
je stack_esc
cmp ah,0eh
je stack_left
cmp ah,4bh
je stack_left
cmp ah,1ch
je stack_enter
cmp al,20h
jbe stack_again
mov ds:[si],al
cmp pathlen,30
je stack_again
inc pathlen
inc si
mov bl,0
call ShowChar
jmp stack_again
stack_left:
cmp pathlen,0
je stack_again
dec pathlen
dec si
mov byte ptr ds:[si],0
mov bl,1
call ShowChar
jmp stack_again
stack_enter:
clc
jmp stack_end
stack_esc:
stc
stack_end:
pop si
pop cx
pop bx
pop ax
ret
CharStack endp
ShowChar proc far
push dx
push cx
push bx
push ax
mov ah,3
mov bh,0
int 10h ;dh=行
cmp bl,0
je showstr_1
dec dl
mov bh,0
mov ah,2
int 10h
mov al,0
mov bl,3bh
mov cx,1
mov ah,9
int 10h
jmp showstr_3
showstr_1:
mov bl,3bh
mov cx,1
mov ah,9
int 10h
inc dl
mov ah,2
int 10h
showstr_3:
pop ax
pop bx
pop cx
pop dx
ret
ShowChar endp
newfile proc far
mov offsettop,0
mov offsetleft,0
mov bottom,1
mov row,1
mov col,0
mov bflag,0
mov filelen,0
win 6,0,7dh,1,0,23,79
call edit
ret
newfile endp
openfile proc far
pushall
openfile_again:
win 6,0,0,10,19,14,62
win 6,0,3bh,9,18,13,61
cur 0,11,19
mov ah,9
mov dx,offset filemsg1
int 21h
mov si,offset filepath
call CharStack
jc open_end
mov dx,offset filepath
mov ah,3dh
mov al,2
int 21h
jc open_err
mov handfile,ax
mov si,offset buffile
mov cx,0fe01h
mov al,0
open_16:
mov byte ptr ds:[si],al
inc si
loop open_16
mov ah,3fh
mov dx,offset buffile
mov bx,handfile
mov cx,0f000h
int 21h
jc open_err
mov filelen,ax
call ReadFile
mov ah,3eh
mov bx,handfile
int 21h
jmp open_end
open_err:
cur 0,11,19
mov dx,offset filemsg2
mov ah,9
int 21h
openfile_1:
mov ah,0
int 16h
cmp al,'y'
je openfile_again
cmp al,'n'
jne openfile_1
open_end:
popall
ret
openfile endp
savefile proc far
pushall
save_3:
mov ah,3dh
mov dx,offset filepath
mov si,dx
mov al,1
int 21h
jc savefile_again
;mov ah,41h
;int 21h
jmp save_13
savefile_again:
win 6,0,0,10,19,14,62
win 6,0,3bh,9,18,13,61
cur 0,11,19
mov ah,9
mov dx,offset filemsg1
int 21h
mov si,offset filepath
call CharStack
jc save_end
save_13:
mov dx,offset filepath
mov ah,3ch
mov cx,0 ;文件属性 01,只读,02,隐文件,03,系统文件,10,子目录
int 21h
jnc save_1
win 6,0,0,10,19,14,62
win 6,0,3bh,9,18,13,61
cur 0,11,19
mov dx,offset filemsg3
mov ah,9
int 21h
savefile_1:
mov ah,0
int 16h
cmp al,'y'
je savefile_again
cmp al,'n'
jne savefile_1
jmp save_end
save_1:
mov handfile,ax
call FormatFile
mov bx,handfile
mov dx,offset buffile
mov cx,filelen
mov ah,40h
int 21h
mov ah,3eh
mov bx,handfile
int 21h
save_end:
popall
ret
savefile endp
saveasfile proc far
pushall
mov si,offset fileaspath
mov cx,30
mov al,0
as_1:
mov [si],al
inc si
loop as_1
as_again:
win 6,0,0,10,19,14,62
win 6,0,3bh,9,18,13,61
cur 0,11,19
mov ah,9
mov dx,offset filemsg1
int 21h
mov si,offset fileaspath
call CharStack
jc as_end
mov dx,offset fileaspath
mov ah,3dh
mov al,2
int 21h
jc as_2
win 6,0,0,10,19,14,62
win 6,0,3bh,9,18,13,61
cur 0,11,19
mov ah,9
mov dx,offset filemsg4
int 21h
mov dx,offset fileaspath
as_3:
mov ah,0
int 16h
cmp al,'y'
je as_4
cmp al,'n'
jne as_3
jmp as_end
as_4:
;mov ah,41h
;int 21h
as_2:
mov ah,3ch
mov cx,0
int 21h
jc as_again
mov handasfile,ax
call FormatFile
mov bx,handasfile
mov dx,offset buffile
mov cx,filelen
mov ah,40h
int 21h
mov ah,3eh
mov bx,handfile
int 21h
as_end:
popall
ret
saveasfile endp
exitp proc far
mov ax,4c00h
int 21h
ret
exitp endp
win1 proc far
pushall
win 6,0,7dh,1,0,23,79
;newrow row
;call GetRowAddr
mov ax,[offsettop]
neg ax
mov dl,0ffh
mul dl
mov dx,[offsetleft]
neg dx
add ax,dx
mov si,ax
push es
pop ds
mov ax,0b800h
mov es,ax
mov dh,7dh
mov di,0
mov cx,23
win1_2:
add di,0a0h
push cx
mov cx,80
mov bp,0
mov bx,0
win1_1:
mov dl,ds:buffer[si][bp]
cmp dl,20h
;jb win1_3
mov es:[di][bx],dx
;win1_3:
inc bp
inc bx
inc bx
loop win1_1
add si,0ffh
pop cx
loop win1_2
popall
ret
win1 endp
EnterChar proc far
push ax
push bx
push si
push di
push cx
push ds
mov dl,al
newrow row
call GetRowAddr ;bx
mov di,bx
mov ax,offset buffer
add di,ax
newcol col
cmp al,0fdh
je char_end
add bx,ax
push ds
push es
pop ds
mov cx,0fdh
sub cx,ax
add di,0feh
mov si,di
dec si
std
rep movsb
cld
mov es:buffer[bx],dl
pop ds
inc col
cmp col,80
jnz char_end
mov col,79
dec offsetleft
char_end:
pop ds
pop cx
pop di
pop si
pop bx
pop ax
ret
EnterChar endp
GetRowTail proc far
push dx
push cx
push ax
push di
newrow row
call GetRowAddr
mov di,bx
add di,0feh
std
mov al,0
mov cx,0fdh
repz scasb
cld
mov bx,cx
pop di
pop ax
pop cx
pop dx
ret
GetRowTail endp
GetRowHead proc far
push ax
push dx
push si
newrow row
call GetRowAddr ;bx
newcol col
mov si,0
xor ah,ah
head_1:
cmp si,ax
jz head_2
mov dl,es:[bx][si]
inc si
cmp dl,20h
jbe head_1
dec si
head_2:
xor bh,bh
mov bx,si
pop si
pop dx
pop ax
ret
GetRowHead endp
DownMovBuf proc far
push ax
push bx
push cx
push ds
;newbottom bottom
mov cl,bottom
xor ch,ch
mov al,cl
call GetRowAddr ;bx
newrow row
sub cx,ax
mov dl,ch
mov si,bx
mov di,bx
add di,0ffh
jcxz mov_5
mov_1:
push cx
mov cx,0fdh
mov bx,0
mov_2:
mov al,es:buffer[si][bx]
mov es:buffer[di][bx],al;
inc bx
loop mov_2
sub si,0ffh
sub di,0ffh
pop cx
loop mov_1
mov cx,0fdh
mov bx,0
mov_3:
mov byte ptr es:buffer[di][bx],dl
inc bx
loop mov_3
mov_5:
newcol col ;al
mov cx,0fdh
sub cl,al
xor ah,ah
add si,ax
call GetRowHead
add di,bx
mov_4:
mov al,es:buffer[si]
mov byte ptr es:buffer[si],0
mov es:buffer[di],al
inc di
inc si
loop mov_4
pop ds
pop cx
pop bx
pop ax
ret
DownMovBuf endp
GetRowAddr proc far
;功能:获得文件缓存行首地址
;bx=行首地址
push ax
push dx
xor ah,ah
mov dl,0ffh
mul dl
mov bx,ax
pop dx
pop ax
ret
GetRowAddr endp
GetRowInfo proc far
;功能:获得行信息
;入口:AL=行,AH=偏移
;出口:AL=1,空;AH=偏移;BX=行首址;
;AL=0,非空;AH=偏移;BX=行首址;DH=头偏移;DL=尾偏移
push cx
push di
push bp
push ds
push cs
pop ds
jmp short info_next
info db 2 dup (?)
info_next:
mov word ptr info[0],0
mov bp,ax
xor ah,ah
mov dl,0ffh
mul dl
mov bx,ax ;bx=行首址
std
mov cx,0feh;0fdh
mov di,bx
add di,0fch
mov al,0
repz scasb
jcxz info_1
;inc cl
mov info[0],cl
cld
mov di,bx
mov al,0
mov cx,0fdh
repz scasb
mov ch,0fdh
sub ch,cl
dec ch
mov info[1],ch
mov ax,bp
mov cl,8
shr ax,cl
mov di,bx
add di,ax
mov cx,0fdh
sub cx,ax
mov al,0
repz scasb
jcxz info_1
mov al,0
jmp info_2
info_1:
mov al,1
info_2:
mov dx,bp
mov ah,dh
mov dx,word ptr info[0]
pop ds
pop bp
pop di
pop cx
ret
GetRowInfo endp
WinStatus proc far
push es
push ax
push bx
push cx
mov cl,al
mov ax,0b8f6h
mov es,ax
mov al,cl
inc al
mov dl,10
status_3:
xor ah,ah
div dl
add ah,30h
mov byte ptr es:[bx],ah
mov ah,1bh
mov byte ptr es:[bx+1],ah
mov byte ptr es:[bx+3],ah
mov byte ptr es:[bx+5],ah
cmp al,0
jz status_1
mov cx,2
mov si,bx
add si,2
status_2:
mov ah,es:[si]
mov es:[si+2],ah
sub si,2
loop status_2
jmp status_3
status_1:
pop cx
pop bx
pop ax
pop es
ret
WinStatus endp
FormatFile proc far
push ax
push bx
push cx
push dx
push si
push di
mov filelen,0
mov si,offset buffile
mov cx,0fe01h
mov al,0
write_6:
mov byte ptr ds:[si],al
inc si
loop write_6
std
xor cx,cx
mov cl,bottom
newrow row
cmp al,cl
jne write_12
inc cl
write_12:
mov bx,offset buffer
mov si,offset buffile
write_4:
push cx
mov cx,0ffh
mov di,bx
add di,0fdh
mov al,0
repz scasb
jcxz write_5
mov di,0
write_1:
mov al,es:[bx][di]
cmp al,20h
jb write_2
mov ds:[si],al
jmp write_3
write_2:
mov byte ptr ds:[si],20h
write_3:
inc di
inc si
inc filelen
loop write_1
write_5:
mov byte ptr ds:[si],0dh
inc filelen
inc si
mov byte ptr ds:[si],0ah
inc filelen
inc si
add bx,0ffh
pop cx
loop write_4
cld
;mov filelen,si
;sub filelen,offset buffile
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
FormatFile endp
ReadFile proc far
push ax
push bx
push cx
push si
push di
mov si,offset buffer
mov cx,0fe01h
mov al,0
read_6:
mov byte ptr es:[si],al
inc si
loop read_6
mov bottom,1
mov si,offset buffile
mov di,offset buffer
mov dx,0
read_2:
mov bx,0
read_3:
cmp dx,filelen
je read_end
mov al,ds:[si]
cmp al,0dh
je read_1
mov es:[di][bx],al
inc bx
inc si
inc dx
jmp read_3
read_1:
add di,0ffh
inc bottom
add si,2
jmp read_2
read_end:
pop di
pop si
pop cx
pop bx
pop ax
ret
ReadFile endp
sseg ends
dseg segment 'data'
msg1 db 'F1 Active menu F10 Active help','$'
menu db 'File',09h,'Edit',09h,'Help','$'
menuadd dw,menu1,menu2,menu3,menu4,menu5
menu1 db ' New',0dh,0ah,'$'
menu2 db ' Open',0dh,0ah,'$'
menu3 db ' Save',0dh,0ah,'$'
menu4 db ' Save as',0dh,0ah,'$'
menu5 db ' Exit','$'
menuid dw ? ;
menufun dw offset newfile,seg newfile,offset openfile,seg openfile,offset Savefile,seg Savefile,offset Saveasfile,seg Saveasfile,offset exitp,seg exitp
row db ? ;当前行
col db ? ;当前列
statusmsg db 'Row:',0,0,0,0,'Col:',0,0,0,0,'Line:',0,0,0,0,'$'
offsettop dw ? ;缓存上偏移
offsetleft dw ? ;缓存左偏移
bottom db ? ;缓存底行
handfile dw ? ;文件指针
handasfile dw ?
; ptail dw ? ;文件尾指针
bflag db 0 ;文件尾标志
filelen dw ? ;文件长度
buffile db 0ffh dup (0ffh dup(0)) ;文件缓存
filelist db 30
pathlen db 0 ;路径长度
filepath db 31 dup (0)
fileaspath db 31 dup (0)
filemsg1 db 'File Name:','$'
filemsg2 db "File cann't open'd!Again open,Y/N?",'$'
filemsg3 db "Cann't bulid file,Again bulid,y/n?",'$'
filemsg4 db 'File exist,Cover it?y/n','$'
dseg ends
eseg segment 'data'
buffer db 0ffh dup (0ffh dup(0)) ;屏幕缓存
eseg ends
stack segment 'stack'
db 1000h dup(0)
stack ends
end main