实验三 循环程序设计

一、实验目的

1. 掌握 loop 指令。

2. 掌握while和do while循环结构。

3. 掌握伪指令.while和 .repeat。

4. 掌握一维数组处理的常用算法。

二、实验内容

1.有一个首地址为array的10个有符号的双字数组,编程分别求出偶数和与奇数和并输出。

偶数和:

include vcIO.inc
include io32.inc
.data  array dword 12,34,123,78,43,234,79,86,98,20esum  dword ?
.code
start:mov ecx,lengthof arrayxor eax,eaxmov esi,eax
again: mov ebx,array[esi*4]test ebx,1jnz nextadd eax,ebx
next: inc esiloop againmov esum,eaxcall dispuid
exit 0
end start

奇数和:

include vcIO.inc
include io32.inc
.data  array dword 12,34,123,78,43,234,79,86,98,20esum  dword ?
.code
start:mov ecx,lengthof arrayxor eax,eaxmov esi,eax
again: mov ebx,array[esi*4]test ebx,1jz nextadd eax,ebx
next: inc esiloop againmov esum,eaxcall dispuid
exit 0
end start

思考题:将求和改为求(1)最大值与最小值 (2)正数和与负数和 (3)素数和

最大值与最小值

include vcIO.inc
include io32.inc
.data  msg1 byte '输出最大值',10,0msg2 byte '输出最小值',10,0fmts byte '%s',0fmtd byte '%d',10,0array dword 12,34,123,78,43,234,79,86,98,20max dword ?min dword ?
.code
start:mov ecx,lengthof array -1mov eax,array[0]mov ebx,eaxmov esi,1.while(ecx!=0)cmp eax,array[esi*4]jge smallmov eax,array[esi*4]small:cmp ebx,array[esi*4]jle nextmov ebx,array[esi*4]next:inc esidec ecx.endwmov max,eaxinvoke printf,offset fmts,offset msg1invoke printf,offset fmtd,maxmov min,ebxmov eax,ebxinvoke printf,offset fmts,offset msg2invoke printf,offset fmtd,min
exit 0
end start

正数和与负数和

include io32.inc
.data
array sdword -4,-2,-1,-6,-5,0,1,2,1,4,5
sum_gz sdword 0
sum_lz sdword 0
msg_gz byte '正数和=',0
msg_lz byte '负数和=',0
.code
main:mov ecx,lengthof array-1xor eax,eax  ;sum_gzmov ebx,eax  ;sum_lzmov esi,eax;在使用伪代码时,如果存在负数,则有必要加上 sdword ptr.while sdword ptr ecx>=0mov edx,array[esi*4].if sdword ptr edx>0add eax,edx.endif.if sdword ptr edx<0add ebx,edx.endifinc esidec ecx.endwmov sum_gz,eaxmov eax,offset msg_gzcall dispmsgmov eax,sum_gzcall dispsidcall dispcrlfmov sum_lz,ebxmov eax,offset msg_lzcall dispmsgmov eax,ebxcall dispsid
exit 0
end main

素数和

include io32.inc
include vcio.inc
.dataarray dword 1,4,-2,3,-1,5,7,-3,0,2primeSum dword 0 ;素数和msg byte '素数和为',0
.code
start:mov ecx,lengthof arraymov esi,0again:mov eax,array[esi*4]cmp eax,0jle next ;判断是否是素数cmp eax,1je sumjmp isPrime ;非无符号数,下一个isPrime:mov ebx,2.while ebx<eaxxor edx,edxdiv ebxcmp edx,0jz nextinc ebx.endwsum:mov eax,array[esi*4]add primeSum,eaxnext:inc esiloop againmov eax,offset msgcall dispmsgmov eax,primeSumcall dispsid
exit 0
end start

2.有一个首地址为array的10个有符号的双字数组,编程逆置数组并输出。

include io32.inc
.data
array dword 10,11,9,8,7,6,3,1
count equ lengthof array
.code
main:mov esi,0mov edi,count-1.while(esi<edi)mov eax,array[esi*4]xchg eax,array[edi*4]mov array[esi*4],eaxinc esidec edi.endwmov ecx,countmov esi,0again:mov eax,array[esi*4]call dispsidcall dispcrlfinc esidec ecxjnz again
exit 0
end main

3. Fibonacci numbers的定义: f1=1,f2=1, fn= fn-1 + fn-2 n>=3 编程输出Fibonacci numbers的前30项。

include vcIO.inc
include io32.inc
.data  fmtc byte 10,'counter=%6u',0fmt byte '%10u',0
.code
main procinvoke  printf,offset fmt,1invoke printf,offset fmt,1mov ecx,2mov esi,1mov edi,esiagain:add esi,edipushadinvoke printf,offset fmt,esipopadcmp ecx,31je overinc ecxxchg esi,edijmp againover:dec ecxinvoke printf,offset fmtc,ecxret
main endp
end main

思考题:在不产生溢出的情况下n的最大值是多少?

include vcIO.inc
include io32.inc
.data  fmtc byte 10,'counter=%7u',0fmt byte '%10u',0
.code
main procinvoke  printf,offset fmt,1invoke printf,offset fmt,1mov ecx,2mov esi,1mov edi,esiagain:add esi,edijc overinc ecxpushad invoke printf,offset fmt,esipopadxchg esi,edijmp againover:invoke printf,offset fmtc,ecxret
main endp
end main

4.编程写一个完整的程序,求出2012年~2099年中的所有闰年年份,存入Lyear数组中,并输出所有闰年。(可考虑控制每行输出5个年份)提示:采用伪代码描述如下:

esi=0 ;闰年个数计数器;
ecx=2012 ;年份计数器;
while (ecx<2100)
{
if (year mod 4=0 and year mod 100 <>0) or (year mod 400=0) then
{ Lyear[esi]=ecx; //Lyear[esi] 为存放闰年的数组
esi++;}
ecx++;
}
Lcounter=esi ;Lcounter 闰年个数

include vcIO.inc
include io32.inc
.data  year dword ?lyear dword 100 dup(?)
.code
main procmov esi,0mov year,2012.while year<=2099mov eax,yearxor edx,edxmov ebx,400div ebxcmp edx,0jz leapmov eax,yearxor edx,edxmov ebx,4div ebxcmp edx,0jnz noleapmov eax,yearxor edx,edxmov ebx,100div ebxcmp edx,0jz noleap
leap:mov eax,yearcall dispuidcall dispcrlfmov lyear[esi*4],eaxinc esi
noleap:inc year
.endw
ret
main endp
end main

5.有一个首地址为string的字符串 ,分别统计string中空格、大写英文字母的个数并输出。

include vcIO.inc
include io32.inc
.data  msg byte 'ab efg AB 12 !@#',10,0space dword ?lowercase dword ?fmtstr byte '空格=%d 小写字母=%d',10,0
.code
main procmov ecx,lengthof msg-1xor eax,eaxmov ebx,eaxmov esi,offset msg
again:mov dl,[esi]cmp dl,' 'jnz letterinc eax
letter:cmp dl,'a'jb nextcmp dl,'z'ja nextinc ebx
next:inc esiloop againmov space,eaxmov lowercase,ebxinvoke printf ,offset fmtstr,eax,ebxret
main endp
end main

思考题:还可继续统计空格、小写英文字母、数字和其它字符的个数并输出。

6. palindrome(回文)是指正读和反读都一样的数或文本。例如:11、121、12321等,编写程序,求10到10000之间所有回文数并输出。要求每行输出10个数。( 提示:采用div指令把整数分解为单个的数字,并将它们组合成一个新的整数。)

include vcIO.inc
include io32.inc
.data  msg byte 13,10,0fmts byte '%s',0fmtd byte '%7d',0
.code
main procmov edi,0mov ecx,10mov ebx,ecx.while ecx<=10000xor esi,esimov eax,ecx.while(eax!=0)xor edx,edxdiv ebximul esi,10add esi,edx.endwcmp esi,ecxjne nextmov eax,ecxpushadinvoke printf,offset fmtd,eaxpopadinc edi.if edi==10pushadinvoke printf,offset fmts,offset msgpopadmov edi,0.endifnext:inc ecx.endwret
main endp
end main

思考题:测试一字符串是否为“回文串”。

7. 有一个首地址为string 的字符串,剔除string 中所有的空格字符。请从字符串最后一个字符开始逐个向前判断、并进行处理。

include vcIO.inc
include io32.inc
.datastring byte ' i love huibian ,and you?',0fmtd byte '%s'
.code
start:mov esi,lengthof string-1mov edi,lengthof string-1.while sdword ptr esi>=0cmp string[esi],20hjnz nextinc esimov ebx,esi.while ebx<=edimov al,string[ebx]mov string[ebx-1],alinc ebx.endwdec edinext:dec  esi.endwmov byte ptr string[edi+1],0mov eax,offset stringcall dispmsgexit 0
end start

三、回答问题:

1. 在循环结构中,如何避免无限循环情况的发生?

使用确定循环条件真能结束的条件,如不能保证假定的循环结束条件是否真能发生,则应设置循环次数,在汇编语言的loop指令中中,一般设置eax>0。

2. 下面的程序段中,循环体将被执行多少次?如果的初值为-100,循环体将被执行多少次?

     mov ecx,0
loopTest:   mov eax,ecxcall dispuidloop loopTest

当前循环体,ecx为零,循环体循环;当ecx为-100时,循环体被执行的次数为

实验三 循环程序设计相关推荐

  1. c语言程序设计实验三程序,c语言程序设计实验三循环.doc

    c语言程序设计实验三循环 高级语言程序设计 实验三 循环控制 一.实验目的和要求 1. 熟练掌握用while语句.do while语句和for语句实现循环的方法.掌握在程序设计中用循的方法实现一些常用 ...

  2. 周信东c语言实验二实验报告,实验三 循环结构程序设计参考答案

    C语言实操课循环结构答案 实验三 循环结构程序设计(参考答案) 1.设计程序sy3-1.c ,要求打印小学九九乘法表. 算法分析: 根据九九乘法表可知,该表共有9行,第i行有i个表达式,而且表达式中的 ...

  3. c语言已知祖父年龄70,实验三循环结构程序设计.doc

    实验三循环结构程序设计 实验三 循环结构程序设计 一.实验目的 (1) 掌握while.do~while.for语句的使用. (2) 掌握循环体内使用break.continue语句的作用. (3) ...

  4. 实验三matlab实现,实验三matlab程序设计.docx

    实验三matlab程序设计.docx 共 24 页,第 27页 27 2014秋2012级<MATLAB程序设计>实验报告 班级:软件C121 姓名:冯杨腾 学号:125692 实验三 M ...

  5. linux实验三shell程序设计,实验三 LINUX SHELL编程

    精选文库 -实验三LINUX SHELL 编程 四.实验内容 本实验包含两个具体的SHELL程序编写任务,较为全面地涉及了SHELL 程序的设计方法和技术.内容如下: 1.创建以下菜单程序: USER ...

  6. java实验Java面向对象编程_java 实验三 面向对象程序设计(无脑实验系列)

    实验7 运算符重载 (1)定义日期类,重载 "++"和"--"运算符,使之能处理两个日期类对象自增和自减运算,并且自增和自减又分为前缀和后缀运算.(可继续完善. ...

  7. 编译原理实验三 语义分析程序设计与实现

    一.实验目的 在实现词法.语法分析程序的基础上,编写相应的语义子程序,进行语义处理,加深对语法制导翻译原理的理解,进一步掌握将语法分析所识别的语法范畴变换为某种中间代码(四元式)的语义分析方法,并完成 ...

  8. 实验三 循环结构设计

    一.计算s=22+42+62.....+1002 #include<stdio.h> int main() { long s=0;int i;int a=22;while(a<=10 ...

  9. c语言实训模块化程序设计,C语言 实验三 模块化程序设计.doc

    实验掌握一维数组和二维数组的定义.赋值和输入输出的方法 2. 掌握字符数组和字符串函数的使用 3. 掌握与数组有关的算法.掌握函数定义的方法掌握函数实参与形参的传递方式 6. 掌握函数的嵌套调用和递归 ...

  10. 【实验三/四 面向对象程序设计/语言基础与面向对象】

    目录 一.实验目的和要求 二.实验内容 1.求最大公约数 2.复数 5.编写一个含有5个类的程序 三.参考 四.其他实验内容 [实验三 面向对象程序设计] 3.编写一个含圆类的程序 4.含圆类.圆柱类 ...

最新文章

  1. windows cmd/dos操作防火墙
  2. python循环报数游戏_python经典面试题之一:猴子报数
  3. 解题报告 poj 3207
  4. 文件目录遍历的并发算法
  5. 10种CSS3实现的Loading效果
  6. 防火墙的基础知识入门
  7. global.asax不执行原因
  8. paip.提升用户体验----gcc c++ JIT-debugging 技术
  9. Python输出 计算器 !
  10. Excel时间做差,统计加班时长
  11. 白领十大职业病及对策
  12. 邮件黑名单系列文章十 : IP被UCEProtect列入黑名单
  13. 什么是对象,如何面向对象,为什么要面向对象
  14. 基于FPGA的DDS 信号发生器(一)
  15. Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录
  16. Python实现炸金花游戏的示例代码
  17. MOOC和网上学习平台整理
  18. 阿秀朋友先后折戟腾讯、字节、快手、网易、滴滴、深信服后,终于成功上岸了...
  19. VisualFreeBasic:VisualBasic6望尘莫及之变量
  20. VC 怎么获取windows7系统管理员权限

热门文章

  1. 如何使用ArcGIS在Power BI中创建地理地图
  2. (C#)基于百度api实现经纬度查询地址
  3. 怎么把pdf文件转换成word格式文档
  4. 泰坦尼克号生还率预测分析
  5. .net / .net core excel转pdf
  6. ST、SC、FC、LC光纤接头区别?
  7. N76E003 驱动 UC1705并口屏(8080)
  8. IIS部署ASP网站项目详细教程(内部含有子目录)
  9. 【重磅】Crust主网进入节点接入阶段
  10. 上海数据交易中心交易系统开放