DATA SEGMENT
SPEED DB 0DH,0AH,'PLEASE CHOOSE THE SPEED:','1.110 2.150 3.300 4.600 5.1200 6.2400 7.4800 8.9600',0DH,0AH,'$'
CBITS DW 110,150,300,600,1200,2400,4800,9600
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX, DATA
MOV DS, AX
OSPEED: MOV DX, OFFSET SPEED
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
PUSH AX;
MOV AH, 7; CLEAR SCREEN
MOV AL,0
MOV BH,70H
MOV CH,0
MOV CL,0
MOV DH,24
MOV DL,79
INT 10H
POP AX
SUB AL, 30H
MOV BL, AL
MOV BL, AL
MOV SI, BX
MOV BX, CBITS[SI]
MOV AX, 0C200H
MOV DX, 0001H
DIV BX
PUSH AX
;初始化波特率除数寄存器
MOV DX, 03FBH ;指向线路控制寄存器
MOV AL, 80H ;位7置1
OUT DX, AL ;发送此字节
POP AX ;出栈
MOV DX, 03F8H
OUT DX, AL
MOV DX, 03F9H
MOV AL, AH
OUT DX, AL
;初始化线路控制寄存器
MOV DX, 3FBH; 指向线路控制寄存器
MOV AL, 0AH
OUT DX, AL
;初始化中断寄存器
MOV DX,3F9H; 指向中断允许寄存器
MOV AL,0H ;禁止所有中断
OUT DX,AL
MOV DX,3F9H; 禁止中断
MOV AL,0H
SEND: MOV AH,0BH ;有键按下?
INT 21H
AND AL,AL
JZ SEND ;无键按下,转send,否则,调用1S延时子程序
MOV AH,07H ;从键盘输入一个字符,但不回显
INT 21H
MOV DX, 3F8H ;指向发送保持寄存器
OUT DX, AL
CMP AL,1BH; 1BH=ESC
JE END1
;线路状态检测循环
forever: mov dx, 3fdh; 指向线路状态寄存器LSR
in al, dx ;取状态字节
test al, 01h ;测试是否已收到数据
jnz receive ;转接收例程
test al, 20h ;测试是否可以发送字符
jz forever ;若否,继续检测
;发送部分
MOV AH,0BH ;有键按下?
INT 21H
AND AL,AL
jz forever
MOV AH,07H
INT 21H
cmp al, 1bh
jz END1
mov dx, 3f8h
out dx, al
jmp forever
;接受部分
receive: mov dx, 3f8h ;指向接受数据寄存器
in al, dx ;取新收到的字符
and al, 7fh
push ax
mov bx,0; BL=1,前景色
mov ah,14
int 10h 键盘I/O中断调用,检测有无键入
pop ax
cmp al, 0dh ;判断刚才收到的字符是否为回车
jnz forever ;不是,则转向线路检测循环
mov al, 0ah
mov bx,0
mov ah,14
int 10h
jmp forever
END1: MOV AH,4CH;
INT 21H
CODE ENDS
END START