实验三 中断和子程序设计
1. 实验目的
(1)掌握子程序、宏和中断程序的设计方法;
(2)熟悉在PC机上建立、汇编、连接、调试和运行8086/8088汇编语言程序的过程。
2. 实验内容
(1)编写一个子程序计算z=f(x,y)=x/y+x-y(x,y,z有符号数内存字数)。要求通过堆栈传送输入参数,输出参数通过AX传递。(要求输入输出实现,且有提示)。
(2) 编写一个宏,求三个数的最大数,原型为:MAX3 x,y,z,min,最大值要求输出。
(3) 挂接1CH中断,正计时90秒后退出。要求屏幕显示0-89的秒数。
3. 实验过程和程序
1 ;the source code of the Exp 3.1
2 stack segment stack
3 db 512 dup(?)
4 stack ends
5
6 data segment
7 input1 db "input x:",0ah,0dh,'$'
8 input2 db "input y:",0ah,0dh,'$'
9 input db 8
10 db 1 dup(?)
11 db 8 dup(?)
12 sign db 0
13 x dw 0
14 y dw 0
15 z dw 0
16 data ends
17
18 code segment
19 assume ds: data, cs: code
20 start:
21 mov ax, data
22 mov ds, ax
23 mov ax, stack
24 mov ss, ax
25
26 lea dx, input1
27 mov ah, 09h
28 int 21h
29
30 lea dx, input
31 mov ah, 0ah
32 int 21h
33
34 mov dl, 0ah
35 mov ah, 02h
36 int 21h
37
38 mov dl, 0dh
39 mov ah, 02h
40 int 21h
41
42 mov ax, 0
43 xor cx, cx
44 mov cl, [input+1]
45 lea si, [input+2]
46 mov byte ptr [sign], 0
47 cmp byte ptr[si], '-'
48 jnz next1
49 inc si
50 dec cx
51 mov byte ptr [sign], 1
52 next1:
53 mov ax, 0
54 loop1:
55 mov bx, 10
56 mul bx
57 xor bx, bx
58 mov bl, [si]
59 sub bl, '0'
60 add ax, bx
61 inc si
62 loop loop1
63
64 cmp byte ptr [sign], 1
65 jnz next2
66 xor bx, bx
67 sub bx, ax
68 mov ax, bx
69
70 next2:
71 mov [x], ax
72
73 lea dx, input2
74 mov ah, 09h
75 int 21h
76
77 mov byte ptr [input], 8
78 lea dx, input
79 mov ah, 0ah
80 int 21h
81
82 mov dl, 0ah
83 mov ah, 02h
84 int 21h
85
86 mov dl, 0dh
87 mov ah, 02h
88 int 21h
89
90
91
92 mov ax, 0
93 xor cx, cx
94 mov cl, [input+1]
95 lea si, [input+2]
96 mov byte ptr [sign], 0
97 cmp byte ptr [si], '-'
98 jnz next5
99 inc si
100 dec cx
101 mov byte ptr [sign], 1
102 next5:
103 mov ax, 0
104 loop2:
105 mov bx, 10
106 mul bx
107 xor bx, bx
108 mov bl, [si]
109 sub bl, '0'
110 add ax, bx
111 inc si
112 loop loop2
113
114 cmp byte ptr[sign], 1
115 jnz next4
116 xor bx, bx
117 sub bx, ax
118 mov ax, bx
119
120 next4:
121 mov word ptr[y], ax
122
123
124 mov ax, word ptr[x]
125 push ax
126 mov ax, word ptr[y]
127 push ax
128 call f
129 add sp, 4
130 mov word ptr [z], ax ; 保存结果
131
132 mov ax, word ptr[z]
133 cmp word ptr[z], 0
134 jge skip2
135 mov dl, '-'
136 mov ah, 02h
137 int 21h
138 mov ax, 0
139 sub ax, word ptr [z]
140 skip2:
141 cmp ax, 0
142 jne skip1
143 push ax
144 mov cx, 1
145 jmp next3_end
146 skip1:
147 mov cx, 0
148 next3:
149 cmp ax, 0
150 je next3_end
151 mov dl, 10
152 div dl
153 xor bx, bx
154 mov bl,ah
155 push bx
156 mov ah, 0
157 inc cx
158 jmp next3
159
160 next3_end:
161 xor dx, dx
162 pop dx
163 add dl, 48
164 mov ah, 02h
165 int 21h
166 loop next3_end
167
168 mov ah, 4ch
169 int 21h
170 f:
171 push bp
172 mov bp, sp
173 mov bx, [bp+6] ; x
174 mov cx, [bp+4] ; y
175 mov dx, bx
176 sub dx, cx ; x-y
177 mov ax, bx
178 mov si, dx
179 cwd
180 idiv cx
181 add ax, si
182 mov sp, bp
183 pop bp
184 ret
185 code ends
186 end start
1 ;the source code of the Exp 3.2
2 stack segment stack
3 db 512 dup(?)
4 stack ends
5
6 data segment
7 input1 db "input x(<2559):", 0ah, 0dh, '$'
8 input2 db "input y(<2559):", 0ah, 0dh, '$'
9 input3 db "input z(<2559):", 0ah, 0dh, '$'
10 input db 8
11 db 1 dup(?)
12 db 8 dup(?)
13 x dw 0
14 y dw 0
15 z dw 0
16 max dw 0
17 sign db 0
18 output db "max:", 0ah, 0dh, '$'
19 data ends
20
21 code segment
22 assume ds: data, cs: code, ss: stack
23
24 start:
25 mov ax, data
26 mov ds, ax
27 mov ax, stack
28 mov ss, ax
29 MAX3 macro x, y, z, max
30
31 push ax
32 push bx
33 push cx
34 mov ax, word ptr[x]
35 mov bx, word ptr[y]
36 mov cx, word ptr[z]
37 cmp bx, ax
38 jg n1
39 jmp n2
40 n1:
41 mov ax, bx
42 n2:
43 cmp cx, ax
44 jg n3
45 jmp end_p
46 n3:
47 mov ax, cx
48 end_p:
49 mov word ptr[max], ax
50 pop cx
51 pop bx
52 pop ax
53 endm
54
55 lea dx, input1
56 mov ah, 09h
57 int 21h
58
59 lea dx, input
60 mov ah, 0ah
61 int 21h
62
63 mov dl, 0ah
64 mov ah, 02h
65 int 21h
66
67 mov dl, 0dh
68 mov ah, 02h
69 int 21h
70
71 mov ax, 0
72 xor cx, cx
73 mov cl, [input+1]
74 lea si, [input+2]
75 mov byte ptr [sign], 0
76 cmp byte ptr[si], '-'
77 jnz next1
78 inc si
79 dec cx
80 mov byte ptr [sign], 1
81 next1:
82 mov ax, 0
83 loop1:
84 mov bx, 10
85 mul bx
86 xor bx, bx
87 mov bl, [si]
88 sub bl, '0'
89 add ax, bx
90 inc si
91 loop loop1
92
93 cmp byte ptr [sign], 1
94 jnz next2
95 xor bx, bx
96 sub bx, ax
97 mov ax, bx
98
99 next2:
100 mov word ptr[x], ax
101
102 lea dx, input2
103 mov ah, 09h
104 int 21h
105
106 lea dx, input
107 mov ah, 0ah
108 int 21h
109
110 mov dl, 0ah
111 mov ah, 02h
112 int 21h
113
114 mov dl, 0dh
115 mov ah, 02h
116 int 21h
117
118 mov ax, 0
119 xor cx, cx
120 mov cl, [input+1]
121 lea si, [input+2]
122 mov byte ptr [sign], 0
123 cmp byte ptr[si], '-'
124 jnz next11
125 inc si
126 dec cx
127 mov byte ptr [sign], 1
128 next11:
129 mov ax, 0
130 loop11:
131 mov bx, 10
132 mul bx
133 xor bx, bx
134 mov bl, [si]
135 sub bl, '0'
136 add ax, bx
137 inc si
138 loop loop11
139
140 cmp byte ptr [sign], 1
141 jnz next22
142 xor bx, bx
143 sub bx, ax
144 mov ax, bx
145
146 next22:
147 mov word ptr[y], ax
148
149 lea dx, input3
150 mov ah, 09h
151 int 21h
152
153 lea dx, input
154 mov ah, 0ah
155 int 21h
156
157 mov dl, 0ah
158 mov ah, 02h
159 int 21h
160
161 mov dl, 0dh
162 mov ah, 02h
163 int 21h
164
165 mov ax, 0
166 xor cx, cx
167 mov cl, [input+1]
168 lea si, [input+2]
169 mov byte ptr [sign], 0
170 cmp byte ptr[si], '-'
171 jnz next111
172 inc si
173 dec cx
174 mov byte ptr [sign], 1
175 next111:
176 mov ax, 0
177 loop111:
178 mov bx, 10
179 mul bx
180 xor bx, bx
181 mov bl, [si]
182 sub bl, '0'
183 add ax, bx
184 inc si
185 loop loop111
186
187 cmp byte ptr [sign], 1
188 jnz next222
189 xor bx, bx
190 sub bx, ax
191 mov ax, bx
192
193 next222:
194 mov word ptr[z], ax
195
196 MAX3 x,y,z,max
197
198 lea dx, output
199 mov ah, 09h
200 int 21h
201
202 mov ax, word ptr[max]
203 cmp ax, 0
204 jge skip2
205 mov dl, '-'
206 mov ah, 02h
207 int 21h
208 mov ax, 0
209 sub ax, [max]
210 skip2:
211 cmp ax, 0
212 jne skip1
213 push ax
214 mov cx, 1
215 jmp next3_end
216 skip1:
217 mov cx, 0
218 next3:
219 cmp ax, 0
220 je next3_end
221 mov dl, 10
222 div dl
223 xor bx, bx
224 mov bl,ah
225 push bx
226 mov ah, 0
227 inc cx
228 jmp next3
229
230 next3_end:
231 xor dx, dx
232 pop dx
233 add dl, 48
234 mov ah, 02h
235 int 21h
236 loop next3_end
237
238 mov ah, 4ch
239 int 21h
240
241 code ends
242 end start
1 ;the source code of the Exp 3.3
2 intno equ 1ch
3
4 stack segment 'stack'
5 db 512 dup(?)
6 stack ends
7
8 data segment 'data'
9 oldisr dw ?,?
10 timer db 100
11 counter dw 89
12 isdone db 0
13 data ends
14
15 code segment 'code'
16 assume cs: code, ds:data, ss: stack
17 start:
18 mov ax,data
19 mov ds,ax
20
21 mov ax,0
22 mov es,ax
23
24 ;protect
25
26 cli
27 mov ax, es:[ intno * 4 ]
28 mov oldisr[0],ax
29 mov ax, es:[ intno * 4 + 2 ]
30 mov oldisr[2], ax
31 sti
32
33 ;set new isr
34
35 cli
36 mov word ptr es:[ intno * 4 ], offset isr
37 mov word ptr es:[ intno * 4 + 2 ], seg isr
38 sti
39
40
41 ;wait here
42 waithere:
43 cmp isdone, 1
44 jnz waithere
45
46 exit:
47 ;restore
48
49 cli
50 mov ax, oldisr[0]
51 mov es: [ intno * 4 ], ax
52 mov ax, oldisr[2]
53 mov es: [ intno * 4 + 2 ], ax
54 sti
55
56 mov ax, 4c00h
57 int 21h
58
59 isr proc far
60 push dx
61 push ax
62
63 mov ax, data
64 mov ds, ax
65
66 sti ;便于中断嵌套
67
68 ;count here
69 inc timer
70 again:
71 cmp timer, 1000/55 ;18
72 jb done
73 mov timer,0
74
75
76 ;carriage
77 mov ah,2
78 mov dl,13
79 int 21h
80
81 ;print time
82 mov ax,counter
83
84 mov dl, 10
85 div dl
86 mov dh, ah
87 mov dl, al
88 mov ah, 2
89 add dl, 30h
90 int 21h
91 mov dl, dh
92 add dl, 30h
93 int 21h
94
95 dec counter
96 jge done
97 mov isdone, 1
98
99 done:
100 pushf
101 call dword ptr oldisr
102
103 cli
104 pop ax
105 pop dx
106 iret ;中断返回
107 isr endp
108
109
110 code ends
111 end start
四. 实验结果(包括必要的截图)
图 1 题1程序运行截图①
图 2 题1程序运行截图②
图 3 题2程序运行截图①
图 4 题2程序运行截图②
图 5 题2程序运行截图③
图 6 题3程序运行截图①
图 7 题3程序运行截图②
图 8 题3程序运行截图③
图 9 题3程序运行截图④
4. 实验体会
题3直接用的老师的代码,对着看勉强看懂。