if and switch -- assemble and C
這篇是if跟switch用在處理流程的時候assemble會是怎樣。
if跟switch的例子行為都是一樣的。就只是當“i”等於多少時“t”就應該等於多少
1. If
== Assemble code ===================
2. Switch
== Assemble code ===================
if跟switch的例子行為都是一樣的。就只是當“i”等於多少時“t”就應該等於多少
1. If
1 #include <stdio.h>
2 int main(int argc, const char *argv[])
3 {
4 int i = 2, t = 0;
5 if(i == 0){
6 t = 0x1;
7 }else if(i == 1){
8 t = 0x2;
9 }else{
10 t = 0x3;
11 }
12
13 return 0;
14 }
2 int main(int argc, const char *argv[])
3 {
4 int i = 2, t = 0;
5 if(i == 0){
6 t = 0x1;
7 }else if(i == 1){
8 t = 0x2;
9 }else{
10 t = 0x3;
11 }
12
13 return 0;
14 }
== Assemble code ===================
00000000004004ec <main> :
4004ec: push %rbp
4004ed: mov %rsp,%rbp
4004f0: mov %edi,-0x14(%rbp)
4004f3: mov %rsi,-0x20(%rbp)
4004f7: movl $0x2,-0x8(%rbp) => i = 2
4004fe: movl $0x0,-0x4(%rbp) => t = 0
400505: cmpl $0x0,-0x8(%rbp) => if(i==0)
400509: jne 400514 <main+0x28> => not equal just jump (main+0x28: 400514) position
40050b: movl $0x1,-0x4(%rbp) => t = 1
400512: jmp 40052a <main+0x3e> => if(i==0) end
400514: cmpl $0x1,-0x8(%rbp) => if(i==1)
400518: jne 400523 <main+0x37> => not equal just jump (main+0x37: 400523) position
40051a: movl $0x2,-0x4(%rbp) => t = 2
400521: jmp 40052a <main+0x3e> => if(i==1) end
400523: movl $0x3,-0x4(%rbp) => t = 3
40052a: mov $0x0,%eax
40052f: pop %rbp
400530: retq
400531: nopw %cs:0x0(%rax,%rax,1)
40053b: nopl 0x0(%rax,%rax,1)
4004ec: push %rbp
4004ed: mov %rsp,%rbp
4004f0: mov %edi,-0x14(%rbp)
4004f3: mov %rsi,-0x20(%rbp)
4004f7: movl $0x2,-0x8(%rbp) => i = 2
4004fe: movl $0x0,-0x4(%rbp) => t = 0
400505: cmpl $0x0,-0x8(%rbp) => if(i==0)
400509: jne 400514 <main+0x28> => not equal just jump (main+0x28: 400514) position
40050b: movl $0x1,-0x4(%rbp) => t = 1
400512: jmp 40052a <main+0x3e> => if(i==0) end
400514: cmpl $0x1,-0x8(%rbp) => if(i==1)
400518: jne 400523 <main+0x37> => not equal just jump (main+0x37: 400523) position
40051a: movl $0x2,-0x4(%rbp) => t = 2
400521: jmp 40052a <main+0x3e> => if(i==1) end
400523: movl $0x3,-0x4(%rbp) => t = 3
40052a: mov $0x0,%eax
40052f: pop %rbp
400530: retq
400531: nopw %cs:0x0(%rax,%rax,1)
40053b: nopl 0x0(%rax,%rax,1)
2. Switch
1 #include <stdio.h>
2 int main(int argc, const char *argv[])
3 {
4 int i = 2, t = 0;
5
6 switch(i){
7 case 0:
8 t = 0x1;
9 break;
10 case 1:
11 t = 0x2;
12 break;
13 default:
14 t = 0x3;
15 break;
16 }
17
18 return 0;
19 }
2 int main(int argc, const char *argv[])
3 {
4 int i = 2, t = 0;
5
6 switch(i){
7 case 0:
8 t = 0x1;
9 break;
10 case 1:
11 t = 0x2;
12 break;
13 default:
14 t = 0x3;
15 break;
16 }
17
18 return 0;
19 }
== Assemble code ===================
00000000004004ec <main>:
4004ec: push %rbp
4004ed: mov %rsp,%rbp
4004f0: mov %edi,-0x14(%rbp)
4004f3: mov %rsi,-0x20(%rbp)
4004f7: movl $0x2,-0x8(%rbp) => i = 2
4004fe: movl $0x0,-0x4(%rbp) => t = 0
400505: mov -0x8(%rbp),%eax
400508: test %eax,%eax => set ZF to 1 if eax == 0
40050a: je 400513 <main+0x27> => jump if ZF == 1
40050c: cmp $0x1,%eax => compare eax and 0x1
40050f: je 40051c <main+0x30> => if eax equal 0x1 then jump to 40051c(main+0x30)
400511: jmp 400525 <main+0x39> => jump to 400525(main+0x39)
400513: movl $0x1,-0x4(%rbp) => t = 0x1
40051a: jmp 40052d <main+0x41> => jump to 40052d(main+0x41)
40051c: movl $0x2,-0x4(%rbp) => t = 0x2
400523: jmp 40052d <main+0x41> => jump to 40052d(main+0x41)
400525: movl $0x3,-0x4(%rbp) => t = 0x3
40052c: nop
40052d: mov $0x0,%eax
400532: pop %rbp
400533: retq
400534: nopw %cs:0x0(%rax,%rax,1)
40053e: xchg %ax,%ax
4004ec: push %rbp
4004ed: mov %rsp,%rbp
4004f0: mov %edi,-0x14(%rbp)
4004f3: mov %rsi,-0x20(%rbp)
4004f7: movl $0x2,-0x8(%rbp) => i = 2
4004fe: movl $0x0,-0x4(%rbp) => t = 0
400505: mov -0x8(%rbp),%eax
400508: test %eax,%eax => set ZF to 1 if eax == 0
40050a: je 400513 <main+0x27> => jump if ZF == 1
40050c: cmp $0x1,%eax => compare eax and 0x1
40050f: je 40051c <main+0x30> => if eax equal 0x1 then jump to 40051c(main+0x30)
400511: jmp 400525 <main+0x39> => jump to 400525(main+0x39)
400513: movl $0x1,-0x4(%rbp) => t = 0x1
40051a: jmp 40052d <main+0x41> => jump to 40052d(main+0x41)
40051c: movl $0x2,-0x4(%rbp) => t = 0x2
400523: jmp 40052d <main+0x41> => jump to 40052d(main+0x41)
400525: movl $0x3,-0x4(%rbp) => t = 0x3
40052c: nop
40052d: mov $0x0,%eax
400532: pop %rbp
400533: retq
400534: nopw %cs:0x0(%rax,%rax,1)
40053e: xchg %ax,%ax
留言