实验6 输入输出程序设计

  • 格式:doc
  • 大小:75.00 KB
  • 文档页数:6

实验6 I/O程序设计

实验目的:(1)掌握利用DOS、BIOS功能调用实现I/O的方法。

(2)掌握程序直接控制I/O方式。

实验内容:(1)利用DOS INT 21H功能调用,编程实现对输入的字符串进行统计;

(2)利用BIOS INT 10H功能调用,控制字符显示特性。

(3)通过I/O指令直接对61端口的控制,实现扬声器发声。

实验过程:

(1)从键盘输入一串字符(<80,回车结束),统计其中的数字、大写字母、小写字母和其它字符。

① 程序说明。

1)主程序利用INT 21H 的1h号功能调用输入字符串,并调用各子程序完成统计、显示功能;

2)子程序disp_msg: 利用INT 21H 的9h号功能调用显示字符串;

入口参数:DS:DX指向字符串

3)子程序disp_cnt: 利用INT 21H 的2h号功能调用显示统计数字;

入口参数: BX为待显示值

4)子程序 count: 分类统计;

入口参数:DS:DX指向字符串

出口参数: BX为数字字符;CX为大写字母;BP为小写字母;DI为其它字符

data segment

str db 80 dup (?)

msg0 db 13,10, 'Enter a string:', '$'

msg1 db 13,10, 'Numbers=', '$'

msg2 db 13,10, 'Upletters=', '$'

msg3 db 13,10, 'Lowletters=', '$'

msg4 db 13,10, 'Others=', '$'

data ends

code segment

assume cs:code,ds:data

go: mov ax, data

mov ds, ax

lea dx , msg0

call disp_msg

mov ah, 1

lea si , str

lp1:

int 21h

mov [si], al

cmp al, 13

je L0

inc si

jmp lp1

L0:

lea dx, str

call count

lea dx, msg1 call disp_msg

call disp_cnt

lea dx, msg2

call disp_msg

mov bx, cx

call disp_cnt

lea dx, msg3

call disp_msg

mov bx, bp

call disp_cnt

lea dx, msg4

call disp_msg

mov bx, di

call disp_cnt

mov ah, 4ch

int 21h

disp_msg proc

push ax

mov ah, 9

int 21h

pop ax

ret

disp_msg endp

disp_cnt proc

push ax

push dx

mov ax, bx

mov bl, 10

div bl

mov bh, ah

mov dl, al

add dl, 30h

mov ah, 2

int 21h

mov dl, bh

add dl, 30h

mov ah, 2

int 21h

pop dx

pop ax

ret

disp_cnt endp

count proc

push ax

push si

xor bx, bx; Numbers

xor cx, cx; Upletters

xor bp, bp; Lowletters

xor di, di; Others

mov si, dx

next:

mov al, [si]

cmp al, 13

je done

cmp al, '0'

jb other cmp al, '9'

jbe num

cmp al, 'A'

jb other

cmp al, 'Z'

jbe U_letter

cmp al, 'a'

jb other

cmp al, 'z'

jbe L_letter

other:

inc di

jmp cont

num:

inc bx

jmp cont

U_letter:

inc cx

jmp cont

L_letter:

inc bp

cont:

inc si

jmp next

done:

pop si

pop ax

ret

count endp

code ends

end go

② 补充完整程序,编辑、连接、运行、调试程序。

③ 记录编译过程中错误,并分析原因。

④ 画出子程序disp_cnt的流程图。

(2)从键盘输入一串密码(<40)后,以闪烁的方式显示密码。

① 程序说明。

1) 利用INT 21H 的7h号功能调用输入密码;

2) 利用BIOS 10H 的3h号功能调用获得当前显示位置;利用BIOS 10H 的13h号功能以红底白字闪烁显示密码信息;

3) 子程序disp1利用INT 21H 的2h号功能调用显示字符;子程序cls利用BIOS 10H 的6h号功能清屏。

data segment

msg1 db 13,10, 'Type a secret message :', '$'

msg2 db 13,10, 'Your secret message is:', '$'

secret db 40 dup (?)

data ends

code segment

main proc far

assume cs:code,ds:data

go:

mov ax,data

mov ds,ax

mov es,ax call cls

lea dx, msg1

mov ah,9

int 21h

xor bx,bx

lp1: mov ah,7

int 21h

cmp al,13

je disp

mov secret[bx], al

inc bx

call dsp1

jmp lp1

disp:

lea dx, msg2

mov ah,9

int 21h

mov si,bx

mov ah, 3

mov bh,0

int 10h

mov cx,si

mov al,0

lea bp,secret

mov bl,0cfh

mov ah,13h

int 10h

done:

mov ax,4c00h

int 21h

main endp

dsp1 proc near

push dx

push ax

mov dl, '*'

mov ah, 2

int 21h

pop ax

pop dx

ret

dsp1 endp

cls proc

push ax

push bx

push cx

push dx

mov ch, 0

mov cl, 0

mov dh, 24

mov dl, 79

mov bh,7

mov al, 0

mov ah,6

int 10h

pop dx pop cx

pop bx

pop ax

ret

cls endp

code ends

end go

② 在全屏方式下编辑、连接、运行(在全屏方式下)、调试程序。

③ 记录编译过程中错误,并分析原因。

(3)用I/O指令直接控制输出控制寄存器(I/O端口为61h)的第1位,使该位按所需要的频率进行1和0的交替变化,从而控制开关电路产生一串脉冲波形,这些脉冲经放大后驱动扬声器发声音)。

① 程序说明。

程序通过I/O指令使设备控制寄存器(1/O端口地址为61H)的第1位交替为0和l,而61H端口的第l位和扬声器的脉冲门相连,交替为0和l的脉冲电流被放大后送到扬声器使之发出了声音。

本例中CX=3000

它决定发声频率

值小声音尖锐,

值大声音低沉。

DX=6000决定

发声时间长短。

1) 主程序提示输入任一键后开始发声;

2) 子程序sound 实现发声。

SOUND

data segment

msg1 db 13,10, ‘Enter a key to ring.....’, ‘$’

data ends

code segment

main proc far

assume cs:code,ds:data

go:

mov ax,data

mov ds,ax

lp1: lea dx, msg1

mov ah,9

int 21h

mov ah,7