实验二 简单分支程序设计
一、实验目的
- 掌握算术运算指令。
- 掌握CMP、 TEST、条件转移与无条件转移指令。
- 掌握分支程序设计结构。
二、实验内容
1.输入一个数,判断奇、偶数。
include vcIO.inc
include io32.inc
.data
msg byte '请输入一个数',10,0
fmto byte '%d是奇数',10,0
fmte byte '%d是偶数',10,0
fmtd byte '%d',0
fmts byte '%s',0
x dword ?
.code
main proc
invoke printf,offset fmts,offset msg
invoke scanf,offset fmtd,offset x
mov eax,x
shr eax,1
jc lab
invoke printf,offset fmte,x
jmp over
lab:
invoke printf,offset fmto,x
over:
ret
main endp
end main
结果略(截图太麻烦了)
2.将下面C语言程序的代码片段转换为功能等价的汇编语言代码;编写完整的汇编语言程序验证转换的正确性,其中sign与sinteger均为双字变量。
if (sinteger == 0)
sign = 0;
else if ( sinteger> 0)
sign = 1;
else
sign = -1;
include vcIO.inc
include io32.inc
.data
msg byte '请输入一个数',10,0
fmtd byte '%d',0
fmts byte '%s',0
sign dword ?
sintegter dword ?
.code
main proc
invoke printf,offset fmts,offset msg
invoke scanf,offset fmtd,offset sintegter
cmp sintegter,0
jne outjudge
mov sign,0
jmp done
outjudge:
cmp sintegter,0
jl injudge
mov sign,1
jmp done
injudge:
mov sign,-1
done:
invoke printf,offset fmtd,sign
main endp
end main
3.编写程序,计算下面函数的值并输出。
include vcIO.inc
include io32.inc
.data
msg byte '请输入一个数',10,0
fmtd byte '%d',0
fmts byte '%s',0
x dword ?
.code
main proc
invoke printf,offset fmts,offset msg
invoke scanf,offset fmtd,offset x
mov eax,x
cmp eax,0
jge xge0
imul eax,2
jmp display
xge0:
cmp eax,10
jg xg10
imul eax,3
jmp display
xg10:
imul eax,4
display:
invoke printf,offset fmtd,eax
main endp
end main
4.输入一个年份(readuid或使用scanf,读入年份),判断是否是闰年.提示:
采用伪代码描述如下:
read year
if (year mod 4=0 and year mod 100 <>0) or (year mod 400=0) then
print year ,'is leap year. ’
else
print year ,'is not leap year. ’
include vcIO.inc
include io32.inc
.data
msg byte '请输入一个数',10,0
msgnot byte '不是闰年',10,0
msgis byte '是闰年',10,0
fmtd byte '%d',0
fmts byte '%s',0
year dword ?
.code
main proc
invoke printf,offset fmts,offset msg
call readuid
mov year,eax
call dispuid
xor edx,edx
mov ebx,400
div ebx
cmp edx,0
jz leap
mov eax,year
xor edx,edx
mov ebx,4
div ebx
cmp edx,0
jnz noleap
mov eax,year
xor edx,edx
mov ebx,100
div ebx
cmp edx,0
jnz leap
noleap:mov eax,offset msgnot
jmp display
leap:
mov eax,offset msgis
display:call dispmsg
main endp
end main
5.输入三个无符号整数(readuid或使用scanf),判断并输出这三个数是否能构成一个三角形的三条边。
(选做)思考题:若这三个数能构成一个三角形的三条边,输出三角形的形状:等腰三角形、等边三角形。
6.采用无条件和条件转移指令构造while和do while循环结构,完成下面的求和任务并输出sum(sum 为双字)。 sum=1+2+3+….+100
思考题:假设sum 为双字无符号整数,sum=1+2+3+….+n,在和不溢出的情况下求出n的最大值;输出sum和n的值。
1.while:
include vcIO.inc
include io32.inc
.data
msg byte 'sum=',0
sum dword 0
.code
main proc
mov eax,0
mov ecx,101
again:
dec ecx
jz shuchu
add eax,ecx
jmp again
shuchu:
mov sum,eax
mov eax,offset msg
call dispmsg
mov eax,sum
call dispsid
main endp
end main
2.Do-while:
include vcIO.inc
include io32.inc
.data
msg byte 'sum=',0
sum dword 0
.code
main proc
mov eax,0
mov ecx,100
again:
add eax,ecx
dec ecx
jnz again
mov sum,eax
mov eax,offset msg
call dispmsg
mov eax,sum
call dispsid
main endp
end main
思考:
include vcIO.inc
include io32.inc
.data
msg byte 'sum=',0
sum dword 0
.code
main proc
mov eax,0
mov ecx,1
again:
mov eax,sum
add sum,ecx
jc next
inc ecx
jmp again
next:
call dispuid
call dispcrlf
dec ecx
mov eax,ecx
call dispuid
main endp
end main
7.编程实现习题4.11。(选做)编写一个程序,先提示输入数字“Input Number:0~9”,然后在下一行显示输入的数字,结束;如果不是键入了0~9数字,就提示错误“Error!”,继续等待输入数字。
include vcIO.inc
include io32.inc
.data
inmsg byte 'input number(0~9): ',0
ermsg byte 0dh,0ah,'Error! Input again:',0
.code
main proc
mov eax,offset inmsg
call dispmsg
again:
call readc
cmp al,'0'
jb erdisp
cmp al,'9'
ja erdisp
call dispcrlf
call dispc
jmp done
erdisp:
mov eax,offset ermsg
call dispmsg
jmp again
done:
main endp
end main
三、回答问题:
1. 在多分支结构中,为什么必须要有无条件转移指令?
对多分支结构而言,不同的分支体,最终将会到同一出口因而需要进行无条件跳转,否则将进入其他的一个分支体,产生错误。