汇编语言程序设计实验二 (顺序、多分支、循环)
实验二 汇编语言程序设计(顺序、多分支、循环)
一. 实验目的
1、掌握顺序和循环程序结构和设计方法;
2、熟悉在PC机上建立、汇编、连接、调试和运行8086/8088汇编语言程序的过程。
二. 实验内容
1、X、Y、Z、V均为字变量,在X、Y、Z、V字单元中存放是16位带符号数。试编写汇编语言程序完成以下功能:
①计算表达式值(V–(X*Y+Z-720))/X;
②将上述表达式运算结果整数放在SUM1单元,余数放在SUM2单元。
2、使用地址表实现如下功能:根据输入的数字1-7,分别显示相应的英文星期名,如果输入其他字符,则重新输入。
3、求一个班50名学生成绩的平均值、最大值和最小值,并将结果显示出来。
4、从键盘读入一个字符串,以Enter结束,字符串不超过50个字符,并打印该字符串;查找中间是否有‘masm’子串。如果有,输出‘Yes’;否则,输出‘No’)。
三. 实验过程和程序
1 ;the source code of the Exp 2.1
2 stack segment stack
3 db 512 dup(?)
4 stack ends
5
6 data segment
7 inputX db "input x:",0ah,0dh,'$'
8 inputY db "input y:",0ah,0dh,'$'
9 inputZ db "input z:",0ah,0dh,'$'
10 inputV db "input v:",0ah,0dh,'$'
11
12 x dw 0
13 y dw 0
14 z dw 0
15 v dw 0
16 sum1 dw 0
17 sum2 dw 0
18
19 input db 8
20 db 1 dup(?)
21 db 8 dup(?)
22 sign db 0
23 outputSum1 db "sum1:",0ah,0dh,'$'
24 outputSum2 db "sum2:",0ah,0dh,'$'
25 data ends
26
27 code segment 'code'
28 assume cs: code, ds: data, ss: stack
29 start:
30 mov ax, data
31 mov ds, ax
32 mov ax, stack
33 mov ss, ax
34
35 mov ah, 09h
36 lea dx, inputX
37 int 21h
38 call readInt
39 mov word ptr [x], ax
40
41 mov ah, 09h
42 lea dx, inputY
43 int 21h
44 call readInt
45 mov word ptr [y], ax
46
47 mov ah, 09h
48 lea dx, inputZ
49 int 21h
50 call readInt
51 mov word ptr [z], ax
52
53 mov ah, 09h
54 lea dx, inputV
55 int 21h
56 call readInt
57 mov word ptr [v], ax
58
59 mov ax, [x]
60 mov bx, [y]
61 imul bx ; x*y
62 add ax, [z] ; x*y+z
63 sub ax, 720 ; x*y+z-720
64 mov bx, [v]
65 sub bx, ax ; v-(x*y+z-720)
66 mov ax, bx
67 cwd
68 mov bx, [x]
69 idiv bx ; (v-(x*y+z-720))/x
70 mov [sum1], ax
71 mov [sum2], dx
72
73 lea dx, outputSum1
74 mov ah, 09h
75 int 21h
76
77 mov ax, [sum1]
78 call prinIint
79
80 mov dl, 0ah
81 mov ah, 02h
82 int 21h
83
84 mov dl, 0dh
85 mov ah, 02h
86 int 21h
87
88 lea dx, outputSum2
89 mov ah, 09h
90 int 21h
91
92 mov ax, [sum2]
93 call prinIint
94
95 mov ah, 4ch
96 int 21h
97 readInt:
98 pushf
99 push bx
100 push cx
101 push dx
102 push si
103
104 lea dx, input
105 mov ah, 0ah
106 int 21h
107
108 mov dl, 0ah
109 mov ah, 02h
110 int 21h
111
112 mov dl, 0dh
113 mov ah, 02h
114 int 21h
115
116 mov ax, 0
117 xor cx, cx
118 mov cl, [input+1]
119 lea si, [input+2]
120 mov byte ptr [sign], 0
121 cmp byte ptr[si], '-'
122 jnz next1
123 inc si
124 dec cx
125 mov byte ptr [sign], 1
126 next1:
127 mov ax, 0
128 loop1:
129 mov bx, 10
130 mul bx
131 xor bx, bx
132 mov bl, [si]
133 sub bl, '0'
134 add ax, bx
135 inc si
136 loop loop1
137
138 cmp byte ptr [sign], 1
139 jnz next2
140 xor bx, bx
141 sub bx, ax
142 mov ax, bx
143
144 next2:
145 pop si
146 pop dx
147 pop cx
148 pop bx
149 popf
150 ret
151 prinIint:
152 pushf
153 push bx
154 push cx
155 push dx
156 push si
157
158 mov si, ax
159 cmp ax, 0
160 jge skip2
161 mov dl, '-'
162 mov ah, 02h
163 int 21h
164 mov ax, 0
165 sub ax, si
166 skip2:
167 cmp ax, 0
168 jne skip1
169 push ax
170 mov cx, 1
171 jmp next3_end
172 skip1:
173 mov cx, 0
174 next3:
175 cmp ax, 0
176 je next3_end
177 mov dl, 10
178 div dl
179 xor bx, bx
180 mov bl,ah
181 push bx
182 mov ah, 0
183 inc cx
184 jmp next3
185
186 next3_end:
187 xor dx, dx
188 pop dx
189 add dl, 48
190 mov ah, 02h
191 int 21h
192 loop next3_end
193 pop si
194 pop dx
195 pop cx
196 pop bx
197 popf
198 ret
199
200 code ends
201 end start
1 ;the source code of the Exp 2.2
2 stack segment stack
3 db 512 dup(?)
4 stack ends
5
6 data segment
7 address dw l1, l2, l3, l4, l5, l6, l7
8 s1 db 'Monday $'
9 s2 db 'Tuesday $'
10 s3 db 'Wednesday $'
11 s4 db 'Thursday$'
12 s5 db 'Friday$'
13 s6 db 'Startday$'
14 s7 db 'Sunday$'
15 input db 'Please input a number between 1 to 7:',0ah,0dh,'$'
16 data ends
17
18 code segment 'code'
19 assume cs: code, ds: data, ss: stack
20 start:
21 mov ax, data
22 mov ds, ax
23 mov ax, stack
24 mov ss, ax
25 start1:
26 lea dx, input
27 mov ah, 09h
28 int 21h
29
30 mov ah, 01h
31 int 21h
32
33 mov bl, al
34
35 mov dl, 0ah
36 mov ah, 02h
37 int 21h
38
39 mov dl, 0dh
40 mov ah, 02h
41 int 21h
42
43 mov al, bl
44 sub al, '0'
45 cmp al, 1
46
47
48 jb start1
49 cmp al,7
50 ja start1
51
52 dec al
53 mov ah, 0
54 mov bx,ax
55 shl bx, 1
56 jmp address[bx]
57
58 l1:
59 lea dx,s1
60 jmp s
61 l2:
62 lea dx, s2
63 jmp s
64 l3:
65 lea dx, s3
66 jmp s
67 l4:
68 lea dx, s4
69 jmp s
70 l5:
71 lea dx, s5
72 jmp s
73 l6:
74 lea dx, s6
75 jmp s
76 l7:
77 lea dx, s7
78 s:
79 mov ah, 09h
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 mov ah, 4ch
91 int 21h
92
93 code ends
94 end start
1 ;the source code of the Exp 2.3
2 stack segment stack
3 db 512 dup(?)
4 stack ends
5
6 data segment
7 inputMSG db "input:", 0ah, 0dh, '$'
8 max dw 0
9 min dw 100
10 average dw 0
11 input db 8
12 db 1 dup(?)
13 db 8 dup(?)
14 sign db 0
15 output1 db "average:", 0ah, 0dh, '$'
16 output2 db "max:", 0ah, 0dh, '$'
17 output3 db "min:", 0ah, 0dh, '$'
18 data ends
19
20 code segment 'code'
21 assume cs: code, ds: data, ss: stack
22 start:
23 mov ax, data
24 mov ds, ax
25
26 mov cx, 50
27 L1:
28 lea dx, inputMSG
29 mov ah, 09h
30 int 21h
31 call readInt
32 cmp ax, [max]
33 jg max1
34 jmp skip11
35 max1:
36 mov [max], ax
37 skip11:
38 cmp [min], ax
39 jg min1
40 jmp skip22
41 min1:
42 mov [min], ax
43 skip22:
44 add [average], ax
45 loop L1
46
47 mov ax, [average]
48 cwd
49 mov bx, 50
50 idiv bx
51 mov [average], ax
52
53 lea dx, output1
54 mov ah, 09h
55 int 21h
56
57 mov ax, [average]
58 call printInt
59
60 mov dl, 0ah
61 mov ah, 02h
62 int 21h
63
64 mov dl, 0dh
65 mov ah, 02h
66 int 21h
67
68 lea dx, output2
69 mov ah, 09h
70 int 21h
71
72 mov ax, [max]
73 call printInt
74
75 mov dl, 0ah
76 mov ah, 02h
77 int 21h
78
79 mov dl, 0dh
80 mov ah, 02h
81 int 21h
82
83 lea dx, output3
84 mov ah, 09h
85 int 21h
86
87 mov ax, [min]
88 call printInt
89
90 mov ah, 4ch
91 int 21h
92 readInt:
93 pushf
94 push bx
95 push cx
96 push dx
97 push si
98
99 lea dx, input
100 mov ah, 0ah
101 int 21h
102
103 mov dl, 0ah
104 mov ah, 02h
105 int 21h
106
107 mov dl, 0dh
108 mov ah, 02h
109 int 21h
110
111 mov ax, 0
112 xor cx, cx
113 mov cl, [input+1]
114 lea si, [input+2]
115 mov byte ptr [sign], 0
116 cmp byte ptr[si], '-'
117 jnz next1
118 inc si
119 dec cx
120 mov byte ptr [sign], 1
121 next1:
122 mov ax, 0
123 loop1:
124 mov bx, 10
125 mul bx
126 xor bx, bx
127 mov bl, [si]
128 sub bl, '0'
129 add ax, bx
130 inc si
131 loop loop1
132
133 cmp byte ptr [sign], 1
134 jnz next2
135 xor bx, bx
136 sub bx, ax
137 mov ax, bx
138
139 next2:
140 pop si
141 pop dx
142 pop cx
143 pop bx
144 popf
145 ret
146
147 printInt:
148 pushf
149 push bx
150 push cx
151 push dx
152 push si
153
154 mov si, ax
155 cmp ax, 0
156 jge skip2
157 mov dl, '-'
158 mov ah, 02h
159 int 21h
160 mov ax, 0
161 sub ax, si
162 skip2:
163 cmp ax, 0
164 jne skip1
165 push ax
166 mov cx, 1
167 jmp next3_end
168 skip1:
169 mov cx, 0
170 next3:
171 cmp ax, 0
172 je next3_end
173 mov dl, 10
174 div dl
175 xor bx, bx
176 mov bl,ah
177 push bx
178 mov ah, 0
179 inc cx
180 jmp next3
181
182 next3_end:
183 xor dx, dx
184 pop dx
185 add dl, 48
186 mov ah, 02h
187 int 21h
188 loop next3_end
189 pop si
190 pop dx
191 pop cx
192 pop bx
193 popf
194 ret
195
196 code ends
197 end start
1 ;the source code of the Exp 2.4
2 stack segment stack
3 db 512 dup(?)
4 stack ends
5
6 data segment
7 data2 db 50, ?, 50 dup('$')
8 string db 'yes', 0ah, 0dh, '$'
9 string1 db 'no', 0ah, 0dh, '$'
10 data ends
11
12
13 code segment
14 assume cs: code, ds: data, ss: stack
15 start:
16 mov ax, data
17 mov ds, ax
18 mov ax, stack
19 mov ss, ax
20
21 mov ah, 0ah
22 mov dx, offset data2
23 int 21h
24
25 mov ah, 02h
26 mov dl, 0ah
27 int 21h
28
29 mov ah, 02h
30 mov dl, 0dh
31 int 21h
32
33
34 mov dx, offset data2+2
35 mov ah, 09h
36 int 21h
37
38 mov ah, 02h
39 mov dl, 0ah
40 int 21h
41
42 mov ah, 02h
43 mov dl, 0dh
44 int 21h
45
46 mov al, 0h
47 mov cx, 47
48 mov di, 0
49 again:
50 mov al, [ di + 2 ]
51 cmp al, 61h
52 jnz next
53 mov al, [ di + 3]
54 cmp al, 73h
55 jnz next
56 mov al, [ di + 4 ]
57 cmp al, 6dh
58 jnz next
59 jmp yes
60
61
62 next:
63 add di, 1
64 loop again
65 mov dx, offset string1
66 mov ah, 09h
67 int 21h
68 jmp endcode
69 yes:
70 mov dx, offset string
71 mov ah, 09h
72 int 21h
73 endcode:
74 mov ax, 4c00h
75 int 21h
76
77 code ends
78 end start
四. 实验结果(包括必要的截图)
图 1 题2.1运行截图①
图 2 题2.1运行截图②
图 3 题2.2运行截图①
图 4 题2.2运行截图②
图 5 题2.3运行截图①
图 6 题2.3运行截图②
图 7 题2.4运行截图①
图 8 题2.4运行截图②
四.实验体会
汇编太难了,这四个题目如果不查资料一个都弄不出来。上课听得很过瘾,下课啥也不会做。