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
.globlmain
.typemain, @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:
.sizemain, .-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
.globlmain
.typemain, @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:
.sizemain, .-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
.globlmain
.typemain, @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:
.sizemain, .-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
.globlmain
.typemain, @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:
.sizemain, .-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
.globlmain
.typemain, @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:
.sizemain, .-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
.globlmain
.typemain, @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:
.sizemain, .-main
.ident"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-11)"
.section.note.GNU-stack,"",@progbits