您的位置:首页 > 其它

利用汇编语言实现串口的通讯(一)(BISO调用)

2013-04-29 15:31 603 查看
本程序是使用汇编语言通过BISO中断来实现多机间串口通讯功能,程序的简要功能有,支持串口号、波特率、奇偶检验、数据位等通讯参数的设置,可以实现两台PC机间的一发一收的功能;

本人刚学的汇编,写的不好(有点像C的风格),还望各位批评指正。

DATAS SEGMENT
;此处输入数据段代码
str5	db	0dh,0ah,'Enter your choice:','$'
strerro db 0dh,0ah,'Your choice is erro!	please enter a correct choice!','$'
Str0 db	0dh,0ah,'Please input the number of the com:','$'
Str1 db	0dh,0ah,'Please input the baud rate:','$'
Str2 db	0dh,0ah,'Please input the data bits:','$'
Str3 db	0dh,0ah,'Please input the stop bit:','$'
Str4 db	0dh,0ah,'Please input the parity bit:','$'
Str6 db	0dh,0ah,'Press anykey to send to the other computer:','$'

choice0	 db  '1.com1 	2.com2 		3.com3','$'
choice1	 db  '1.2400bps 2.4800bps 3.9600bps' ,'$'
choice2	 db  '1.7bits	 2.8bits 	','$'
choice3	 db  '1.1bit	 2.2bits	','$'
choice4	 db  '1.none 	 2.odd	 	3.evn','$'

sign db 0
ComNum dw 0
;choice	db 2 dup(?)
StrEnter	db 0dh,0ah,'$'

DATAS ENDS

STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS

CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX

call SetCom
call SetBand
call SetDaBits
call SetSpBit
call SetParBit
;初始化串口
mov ah,0
mov al,sign
mov dx,ComNum
int 14h

call SendData

jmp exit

SetCom	proc
;com的菜单,
push bx
push cx
push dx

lpcom:
lea dx,Str0
mov	ah,9
int	21h

lea dx,choice0
mov	ah,9
int	21h

lea dx,str5
mov	ah,9
int	21h

mov ah,1
int 21h
cmp al,'1'
je lpcom1
cmp al,'2'
je lpcom2
cmp al,'3'
je lpcom3

;cmp al,27
;;按下ESC则退出
;je com_exit
;显示错误,提示重新输入
lea dx,Strerro
mov	ah,9
int	21h
;输入错误,重新输入
jmp lpcom

;将串口设为1
lpcom1:
;mov ah,0;初始化串口1
;mov dx,0
;int 14h;调用BISO中断
mov ComNum,0
jmp com_exit
;将串口设为2
lpcom2:
;mov ah,0;初始化串口2
;mov dx,1
;int 14h;调用BISO中断
mov ComNum,1
jmp com_exit
lpcom3:
;mov ah,0;初始化串口3
;mov dx,2
;int 14h;调用BISO中断
mov	ComNum,2
com_exit:
pop dx
pop cx
pop bx
ret
SetCom	endp

SetBand	proc
;band的菜单
push bx
push cx
push dx
lpband:
;mov	ah,7
;int	21h
lea dx,Str1
mov	ah,9
int	21h

lea dx,choice1
mov	ah,9
int	21h

lea dx,str5
mov	ah,9
int	21h

mov ah,1
int 21h

cmp al,'1'
je lpband1
cmp al,'2'
je lpband2
cmp al,'3'
je lpband3

cmp al,27
;按下ESC则退出
je band_exit

lea dx,Strerro
mov	ah,9
int	21h
;输入错误,重新输入
jmp lpband

lea dx,StrEnter
mov	ah,9
int	21h

;将串口设为1
lpband1:
;mov ah,0;初始化串口1
;and al,0a0h;10100000;将波特率设为2400bps
;int 14h;调用BISO中断
and sign,0a0h;
jmp band_exit
;将串口设为2
lpband2:
;mov ah,0;初始化串口2
;and al,0c0h;11000000;将波特率设为4800bps
;int 14h;调用BISO中断
and sign,0c0h
jmp band_exit
lpband3:
;mov ah,0;初始化串口3
;mov al,0e0h;11000000;将波特率设为9600bps
;int 14h;调用BISO中断
add sign,0c0h
band_exit:
pop dx
pop cx
pop bx
ret
SetBand	endp

SetDaBits	proc
;com的菜单,
push bx
push cx
push dx

lpdata:
lea dx,Str2
mov	ah,9
int	21h

lea dx,choice2
mov	ah,9
int	21h

lea dx,str5
mov	ah,9
int	21h

mov ah,1
int 21h
cmp al,'1'
je lpdata1
cmp al,'2'
je lpdata2

cmp al,27
;按下ESC则退出
je data_exit
;显示错误,提示重新输入
lea dx,Strerro
mov	ah,9
int	21h
;输入错误,重新输入
jmp lpdata

lpdata1:
;mov ah,0;初始化串口1
;and al,0feh;00000010-f1100,7位数据位
;or al,02h
;int 14h;调用BISO中断
and sign,0feh
or sign,02h
jmp data_exit

lpdata2:
;mov ah,0;初始化串口2
;or al,03h;00000011,8位数据位
;int 14h;调用BISO中断
or sign,03h

jmp data_exit

data_exit:
pop dx
pop cx
pop bx
ret
SetDaBits	endp

SetSpBit	proc
;的菜单,
push bx
push cx
push dx

lpstop:
lea dx,Str3
mov	ah,9
int	21h

lea dx,choice3
mov	ah,9
int	21h

lea dx,str5
mov	ah,9
int	21h

mov ah,1
int 21h
cmp al,'1'
je lpstop1
cmp al,'2'
je lpstop2

cmp al,27
;按下ESC则退出
je stop_exit
;显示错误,提示重新输入
lea dx,Strerro
mov	ah,9
int	21h
;输入错误,重新输入
jmp lpstop

lpstop1:
;mov ah,0;初始化串口1
;and al,0fdh;00000010-f1011,第3位设为0
;int 14h;调用BISO中断
and sign,0fbh;

jmp stop_exit

lpstop2:
;mov ah,0;初始化串口2
;or al,02h;00000010-ff,第3位设为1
;int 14h;调用BISO中断
or sign,04h
jmp stop_exit

stop_exit:
pop dx
pop cx
pop bx
ret
SetSpBit	endp

SetParBit	proc
;的菜单,
push bx
push cx
push dx

lppar:
lea dx,Str4
mov	ah,9
int	21h

lea dx,choice4
mov	ah,9
int	21h

lea dx,str5
mov	ah,9
int	21h

mov ah,1
int 21h
cmp al,'1'
je lppar1
cmp al,'2'
je lppar2
cmp al,'3'
je lppar3

cmp al,27
;按下ESC则退出
je par_exit
;显示错误,提示重新输入
lea dx,Strerro
mov	ah,9
int	21h
;输入错误,重新输入
jmp lppar

lppar1:
;mov ah,0;初始化串口1
;and al,0f7h;第三位设为0
;int 14h;调用BISO中断
and sign,0f7h
jmp par_exit

lppar2:
;mov ah,0;初始化串口2
;or al,08h;第四位设为1
;and al,0efh;5设为0
;int 14h;调用BISO中断
or sign,08h
and sign,0efh
jmp par_exit
lppar3:
;mov ah,0;初始化串口2
;or al,02h;第4位设为1
;int 14h;调用BISO中断
or sign,08h
or sign,010h

par_exit:
pop dx
pop cx
pop bx
ret
SetParBit	endp

SendData proc
push ax
push bx
push cx
push dx

lea dx,Str6
mov ah,9
int 21h
lpsend:
mov ah,1;从键盘的缓冲区读数据(BISO中断)
int 16h
jz lpnext
mov ah,0;读键盘输入数据(BISO中断)
int 16h
cmp al,1bh;esc
jz send_exit
mov ah,1;send data
mov dx,ComNum
int 14h
lpnext:
mov ah,3;read com state
mov dx,ComNum
int 14h

loop lpsend
mov ah,2;read from com
mov dx,ComNum
int 14h

and ah,01h
cmp ah,1
jne lpsend

;disp
mov dl,al
mov ah,2
int 21h
jmp lpsend

send_exit:
pop dx
pop cx
pop bx
pop ax
ret
SendData endp

exit:
MOV AH,4CH
INT 21H

CODES ENDS
END START
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐