循环控制语句for while do-while if switch

1:For 语句循环控制

L3执行完后直接执行L2的代码
int main()
{
 int a=0;
 for(int i=0;i<100;i=i+2)
{ a=a+10;
}
}

/************************/
	.file	"b01.cpp"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$0, -4(%rbp) //a address
	movl	$0, -8(%rbp) //i address
	jmp	.L2
.L3:
	addl	$10, -4(%rbp) //a=a+10
	addl	$2, -8(%rbp)  //i=i+2
.L2:
	cmpl	$99, -8(%rbp) //i-99
	jle	.L3
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)"
	.section	.note.GNU-stack,"",@progbits

2:while 语句

 
int main()
{
 int a=0;
 while(a<2)
{
  a++;
}

}


//**************************

	.file	"b01.cpp"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$0, -4(%rbp)  //a=0
	jmp	.L2
.L3:
	addl	$1, -4(%rbp) //a++
.L2:
	cmpl	$1, -4(%rbp) //a-1
	jle	.L3
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)"
	.section	.note.GNU-stack,"",@progbits


3: Do-while

 先执行后判断
int main()
{
 int a=20;
 do
{
  a++;
}while(a>100);

}



//**********************

	.file	"b01.cpp"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$20, -4(%rbp)   //a=20
.L2:
	addl	$1, -4(%rbp) //a++
	cmpl	$100, -4(%rbp)   // a-100
	jg	.L2          大于 jg跳转
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)"
	.section	.note.GNU-stack,"",@progbits

4:IF语句

  
int main()
{
 int a;
a=20;
if(a<30)
{a=66;
}
else if(a==30)
{a=99;
}else
{
 a=89;
}

}

//**********************

	.file	"b01.cpp"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$20, -4(%rbp)  //a=20
	cmpl	$29, -4(%rbp)  //a-29
	jg	.L2            //a-29>0  跳L2
	movl	$66, -4(%rbp)  //a<=29 a=66
	jmp	.L3
.L2:
	cmpl	$30, -4(%rbp)  //a-30 
	jne	.L4            ZF标志位为0时跳转 zf=1时a==30
	movl	$99, -4(%rbp)  
	jmp	.L3
.L4:
	movl	$89, -4(%rbp)  //a=89;
.L3:
	movl	$0, %eax  //return 0
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)"
	.section	.note.GNU-stack,"",@progbits

5:swith

int main()
{
 int a;
a=20;
int b;
switch(a)
{
case 100:
 b=100;
 break;
case 20:
 b=20;
break;
default : b=0;
}
}
//*********************
	.file	"b01.cpp"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$20, -4(%rbp)    //a=20
	movl	-4(%rbp), %eax   //eax=20
	cmpl	$20, %eax        //20-20
	je	.L3             //相等jmp .L3
	cmpl	$100, %eax      //20-100
	jne	.L7             // 不等 jmp .L7
	movl	$100, -8(%rbp)  //相等 b=100
	jmp	.L5
.L3:
	movl	$20, -8(%rbp)  //b=20
	jmp	.L5            //跳出
.L7:
	movl	$0, -8(%rbp)  //b=0
.L5:
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)"
	.section	.note.GNU-stack,"",@progbits

6:continue break

continue 结束当下,继续下一个循环
break 结束循环
int main()
{
 int a;
a=20;
int b;
for(int i=0;i<100;i++)
{
if(a==50)
{
b=50;
continue;
}
if(a==60)
{
b=60;
break;
}
a++;
}
}
//**************************
	.file	"b01.cpp"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$20, -4(%rbp) //a=20
	movl	$0, -8(%rbp)  //i=0;
	jmp	.L2
.L7:
	cmpl	$50, -4(%rbp) //a-50
	jne	.L3           //a!=50 jmp .L3
	movl	$50, -12(%rbp) //if a==50 b=50
	jmp	.L4            //continue
.L3:
	cmpl	$60, -4(%rbp)
	jne	.L5
	movl	$60, -12(%rbp)
	jmp	.L6             //break
.L5:
	addl	$1, -4(%rbp)    //a++
.L4:
	addl	$1, -8(%rbp) //i++
.L2:
	cmpl	$99, -8(%rbp) //i-99
	jle	.L7    //i<=99 jmp .L7
.L6:
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)"
	.section	.note.GNU-stack,"",@progbits


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值