DOS_8086_Assembly write a text input program with a window

 

Assembler:

;***************************************************************************************
;  功能:开个窗口,做个窗框,把光标定位在窗口内,显示信息,简单的上卷下卷,末行控制,回车处理
;***************************************************************************************

CODES SEGMENT
    ASSUME CS:CODES
    
;----------------------------------------------------------------------------------
;主程序
TextWindow  proc   			far
	
	call  Initializing
	call  get_char
	ret
TextWindow				endp

;----------------------------------------------------------------------------------
;屏幕初始化
Initializing	proc		        near
	push  ax
	push  bx
	push  cx
	push  dx
    ;设置外窗口属性
    MOV   AH,7				;功能:屏幕初始化
    MOV   AL,0				;page
    MOV   BH,70H			;白底黑字
    MOV   CH,1				;row左上
    MOV   CL,2				;column左上
    MOV   DH,23				;row右下
    MOV   DL,77				;column右下
    INT   10H				;BIOS显示操作中断
    
    ;设置内窗口属性
    MOV   AH,7				;功能:屏幕初始化
    MOV   AL,0				;page
    MOV   BH,3FH			;浅青色底白字
    MOV   CH,2				;row左上
    MOV   CL,4				;column左上
    MOV   DH,22				;row右下
    MOV   DL,75				;column右下
    INT   10H				;BIOS显示操作中断
    
    ;设置光标类型(大小)
    MOV   CH,12				;光标起始行
    MOV   CL,13				;光标结束行
    MOV   AH,1				;设置光标类型
    INT   10H				;DOS中断:显示操作
    ;初始化光标位置
    MOV   DH,2				;屏幕显示行
    MOV   DL,4				;屏幕显示列
    MOV   BH,0				;显示页号
    MOV   AH,2				;置光标位置
    INT   10H				;BIOS显示操作中断
    
    pop   dx
    pop   cx
    pop   bx
    pop   ax
    ret
Initializing                endp

;----------------------------------------------------------------------------------
;键盘输入,直到Esc退出
get_char  proc				near
	push  ax
	push  dx
	
	mov   ah,1			; 功能:从键盘输入一个字符并回显
	int   21h
	
	cmp   al,1bh			; 判断按键是否ESC√
	jz    exit
	
	cmp   al,0DH			; 判断是否为回车键(要注意回车后不返回,而是等待继续输入)
	jz    EnterDeal			; 调转 回车键处理
	
	mov   ah,3			; 读取当前光标位置
	;mov  bh,0			; 页号
	int   10h			; BIOS显示操作中断
	
	cmp   dl,76			; 判断是否最后一列(75+1)√(可用于设置字符显示宽度)
	jz    LastColumn		; 跳转 末列处理
	
	loop  get_char			; 继续输入
	
	pop   dx
	pop   ax
	
;退出程序
exit:
	mov   ah,4ch
	int   21h			;DOS中断
get_char				endp
	
;----------------------------------------------------------------------------------
;回车键处理-----------------;(把回车控制到内窗口-不处理会跳到屏幕左边界)
EnterDeal proc				near
	push  ax
	push  dx

	mov   ah,3			; 读取 当前光标位置
	;mov  bh,0			; 页号
	int   10h			; BIOS显示操作中断
	
	mov   ah,2			; 功能:置光标位置
	;mov  dh,?			; 保持当前行
	mov   dl,4			; 光标置内窗口左边界
	;mov  bh,0			; 页号--------------测试省略:可省略√
	int   10h			; BIOS显示操作中断
	
	jmp   LastLine			; 继续 末行判断
	
	pop   dx
	pop   ax
	ret				; 返回主程序
EnterDeal				endp

;----------------------------------------------------------------------------------
;末列处理
LastColumn     proc			near
	push  ax
	push  dx

	mov   ah,3			; 读取当前光标位置
	;mov  bh,0			; 页号
	int   10h			; BIOS显示操作中断
	
	mov   ah,2			; 功能:置光标位置
	add   dh,1			; 光标置下一行
	mov   dl,4			; 光标置内窗口左边界
	;mov  bh,0			; 页号
	int   10h			; BIOS显示操作中断
	
	jmp   LastLine			; 继续 末行判断
	
	pop   dx
	pop   ax
	ret				; 返回主程序
LastColumn				endp

;----------------------------------------------------------------------------------
;末行判断-------------------; 末行(22+1)需要卷屏并上移光标
LastLine  proc				near
	push  ax
	push  dx

	mov   ah,3			; 读取当前光标位置
	;mov  bh,0			; 页号
	int   10h			; BIOS显示操作中断
	
	cmp   dh,23			; 判断是否最后一行(22+1)
	jne   get_char		        ; 否:继续输入
	
	;-----------------------        ; 是:卷屏1行并上移光标
	call  scroll_screen 	        ; 调用 屏幕滚动
	
	mov   ah,3			; 读取当前光标位置
	;mov  bh,0			; 页号
	int   10h			; BIOS显示操作中断
	
	mov   ah,2			; 功能:置光标位置
	mov   dh,22			; 光标置内窗口左边界
	mov   dl,4			; 光标置内窗口左边界
	int   10h			; BIOS显示操作中断
	
	pop   dx
	pop   ax
	jmp   get_char		        ; 继续输入
Lastline			        endp
	
;----------------------------------------------------------------------------------
;屏幕每次上卷1行----------------          ; 屏幕滚动必须要设置窗口滚动的范围,即cx,dx
scroll_screen	proc		near
	push  ax
	push  bx
	push  cx
	push  dx

	mov	  al,1			; 上卷的行数
	mov	  bh,2FH		; 
	mov   ch,2
	mov   cl,4
	mov   dh,22			; 整屏滚动
	mov   dl,75
	mov	  ah,6			; 功能:屏幕上卷
	int   10h			; BIOS显示操作中断
	
	pop   dx
	pop   cx
	pop   bx
	pop   ax
	ret				; 返回调用程序
scroll_screen			        endp

;----------------------------------------------------------------------------------
CODES ENDS
;**********************************************************************************
    END   TextWindow
    

Operation status:

Figure [1] Operation

Function description:

  1. Enter character strings continuously, and jump to the next line after reaching the inner window boundary;
  2. Press Enter anywhere to jump to the next line;
  3. Press the ESC key anywhere to exit the program;
  4. When the last line is entered, pressing the Enter key and entering characters beyond the inner window will trigger the scrolling of the screen and jump to the next line;
  5. Different background colors are used for scrolling on the screen, in order to facilitate the observation of the scrolling effect;
  6. Press the Backspace key to move the cursor forward. At this time, the typed character will be rewritten, which can be used for character correction;
  7. The arrow keys, page turning keys and other keys have no function, and pressing these keys will only display the identification code of the key;
  8. Keep pressing the backspace key will move the cursor out of the inner window, this problem has not been resolved;
  9. The program only demonstrates window input, cursor and content control, does not save character strings, but only writes to video memory.

Guess you like

Origin blog.csdn.net/qq_27677599/article/details/102992013