实验二 汇编语言程序设计(顺序、多分支、循环)

实验二 汇编语言程序设计(顺序、多分支、循环)

一. 实验目的

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、

设置参变量:;设置 x=2 y = 10 z= 700 v = 10 计算(10-(2x10 + 700 -720))/2 = 5 利用-g命令分段运行最终验证商ax是否等于5 余数dx是否为0
源代码如下:

stack	segment stack
dw 512 dup(?)
stack	ends
data	segment
x     dw  	0002h 
y     dw  000ch
z     dw  02bch 	
v     dw  000ch
sum1  dw   ?
sum2  dw   ?
data	ends
code	segment 
assume cs:code,ds:data,ss:stack
start:	mov ax,data
	mov ds, ax
	mov ax,x
	imul y
	mov bx,ax
	mov cx,dx
	mov ax,z
	cwd
	add bx,ax
	adc cx,dx
	sub bx ,2d2h
	sbb cx,0
	mov ax,v
	cwd
	sub ax,bx
	sbb dx,cx
	idiv x
	mov sum1,ax
	mov sum2,dx
	mov ah, 4ch
	int 21h	
code	ends
	end start
2、
stack	segment stack
dw 512 dup(?)
stack	ends
data	segment
msg db 'Input 1~7:',0dh,0ah,'$'
msg1 db 0dh,0ah,'$'
string1   db  'Monday' ,0dh,0ah,'$'
string2   db  'Tuesday' ,0dh,0ah,'$'
string3   db  'Wednesday' , 0dh,0ah,'$'
string4   db  'Furseday' ,0dh,0ah,'$'
string5   db  'Friday',0dh,0ah,'$'
string6   db  'Saturday',0dh,0ah,'$'
string7   db  'Sunday',0dh,0ah,'$'
data	ends
code	segment 
	assume cs:code,ds:data,ss:stack
start:	mov ax,data
	mov ds,ax
one:	
	mov dx,offset msg
        	mov ah, 9
	int 21h
	mov ah,1
	int 21h
	mov dx,offset msg1
        	mov ah, 9
	int 21h
	cmp  al ,'1'
	jz    Monday
	cmp  al,'2'
	jz   Tuesday
	cmp  al,'3'
	jz  Wednesday
	cmp al,'4'
	jz  Th ursday
	cmp al,'5'
	jz Friday
	cmp al,'6'
	jz  Saturday
	cmp al,'7'
	jz Sunday
	jmp one
	
Monday:   mov ah,09h
	mov dx,offset string1
	int 21h
	mov ah,4ch
	int 21h
Tuesday: mov ah,09h
	mov dx,offset string2
	int 21h
	mov ah,4ch
	int 21h
Wednesday: mov ah,09h
	mov dx,offset string3
	int 21h
	mov ah,4ch
	int 21h
Thursday: mov ah,09h
	mov dx,offset string4
	int 21h
	mov ah,4ch
	int 21h
Friday:     mov ah,09h
	mov dx,offset string5
	int 21h
	mov ah,4ch
	int 21h
Saturday:  mov ah ,09h
	mov dx,offset string6
	int 21h
	mov ah,4ch
	int 21h
Sunday:   mov ah, 09h
	mov dx,offset string7
	int 21h
	mov ah,4ch
	int 21h
code	ends
	end start

3、

stack segment stack
dw 512 dup(?)
stack ends
data	segment
msg db  'Insert the score of 50 students :',0dh,0ah,'$'
msgave db 0dh,0ah,'The average of tatal score is :','$'
msglowest db  0dh,0ah,'The lowest score in the class is:','$'
msghighest db  0dh,0ah,'The highest score in the class is:','$'
msg1 db 0dh,0ah,'ok','$'
blank    db   '  ','$'
xixi         db  50 dup(?)
ave db ?  
sum dw ?
min db ?
max db ?
data	ends
code	segment 
	assume cs:code,ds:data,ss:stack
start:	
	mov ax,data	
	mov ds,ax
	mov dx,offset msg
	mov ah,09h
	int 21h
	mov  cx  , 49
	xor bx,bx
	mov si,offset xixi
	mov ah,01h
	int 21h
	sub al,30h ;方便平均值的输出
	mov bl,al
	mov [si],al ;储存所输入的数据
	mov ave,al  ;将第一个输入的成绩设置为最大值和最小值
	mov min,al
	mov max,al
	mov dx,offset blank
	mov ah,09h
	int 21h
again :
	mov ah,01h;再循环输入49个数据
	int 21h
	sub al,30h
	inc si
	mov [si],al
	mov dx,offset blank;每个输入数据之间用空格隔开
	mov ah,09h
	int 21h
	add bl,al
	adc bh,0
	cmp al, min  ;比较大小
	jae compare1
	mov min ,al
compare1:
	cmp al, max
	jbe go
	mov max,al
go:
	loop again
	mov ax,bx
	mov sum,bx
	mov bl,32h
	div bl
	mov dx,offset msgave;打印平均成绩
	mov ah,09h
	int 21h
	mov dl ,al
	add dl,30h
	mov ah,02h
	int 21h
	mov dx,offset msghighest;打印最高分
	mov ah,09h
	int 21h
	mov dl ,max
	add dl,30h
	mov ah,02h
	int 21h
	mov dx,offset msglowest;打印最低分
	mov ah,09h
	int 21h
	mov dl ,min
	add dl,30h
	mov ah,02h
	int 21h
	mov ah ,4ch
	int 21h
code	ends
	end start
4、
stack	segment stack
	dw 512 dup(?)
stack	ends
data	segment
msg   		 db 'Please input a string(no more than 50 byte):','$'
yesmsg 	db 0dh,0ah,'Yes','$'
nomsg  	db 0dh,0ah,'No','$'
string      db 50		;限定长度为50
			db 0	    ;实际输入的字符个数初始0 int21后自动更改	
			db 50 dup(?) ;string第三个byte才开始储存字符串即string[2]
data	ends
code	segment 
	assume cs:code,ds:data,ss:stack
start:	mov ax,data
	mov ds,ax
	mov dx,offset msg ;打印提示
	mov ah,09h
	int 21h
	mov ah,0ah		;输入一段字符串
	mov dx,offset string
	int 21h
	xor cx,cx
	mov cl ,string[1]    ;string第一个字节存的为字符串的个数,因为masm为4个字节则循环比较字符串长度减3次
	dec cx
	dec cx
	dec cx
	lea bx, string[2]
compare :            ;比较是否存在masm子串
	cmp  byte ptr [bx],'m'
	jne  next
	cmp byte ptr [bx+1],'a'
	jne  next
	cmp  byte ptr [bx+2],'s'
	jne  next
	cmp  byte ptr [bx+3],'m'
	je   yes
next:  
	inc bx
	loop compare
	mov dx,offset nomsg   ;不存在masm则打印No
	mov ah,09h
	int 21h
	jmp down 			;跳到结束
yes:  
	mov dx ,offset yesmsg  ;存在则打印Yes
	mov ah,09h 
	int 21h
down:
	mov ah,4ch
	int 21h
code	ends
	end start
	

四. 实验结果(包括必要的截图)

1、

实验二 汇编语言程序设计(顺序、多分支、循环)_第1张图片
实验二 汇编语言程序设计(顺序、多分支、循环)_第2张图片

实验二 汇编语言程序设计(顺序、多分支、循环)_第3张图片

2、

实验二 汇编语言程序设计(顺序、多分支、循环)_第4张图片

3、

实验二 汇编语言程序设计(顺序、多分支、循环)_第5张图片

4、

实验二 汇编语言程序设计(顺序、多分支、循环)_第6张图片

实验二 汇编语言程序设计(顺序、多分支、循环)_第7张图片实验二 汇编语言程序设计(顺序、多分支、循环)_第8张图片

五. 实验体会

(1)、加深了对有乘法运算imul,结果的分配问题,对于两个有符号字相乘ax存低16位,dx存高16位,(两个16位相乘为32位,当时没考虑到这一点出错了很久),且默认目的操作数为ax,8进制相乘默认操作数为al 结果传ax。除法两个 16进制字相除,其商存于低16位ax,其余数存高16位dx,且默认目的操作数为ax,只需一个源操作数r16,字节相除默认操作数为ax,商传 al,余数存ah,需一个r6.
(2)、定义字符串以$结尾,灵活运用odh,oah回车换行空格等,可使界面相对美观。
(3)、输入字符串0ah,其中第一个byte和第二个byte分别储存字符串长度和字符串输入的实际长度,第二个byte可以赋初始值,但是int21后系统自动更改为实际输入的字符串长度!第三个byte开始储存输入的字符串,即string[2]开始,提取所输入的字符串中的内容时,需要注意内存的开始是第三个,但是利用it21 输入一段字符串时,直接获取其的首地址:offset byte!

你可能感兴趣的:(汇编)