反汇编的时候带上C代码便于观察
比较三元表达式和if else的差异
a1.c
#include <stdio.h>
int main(void)
{
int a=1;
int b=2;
int c=0;
a = (b>c)?1:0;
return 0;
}
a2.c
#include <stdio.h>
int main(void)
{
int a=1;
int b=2;
int c=0;
if(b>c)
{
a=1;
}
else
{
a=0;
}
return 0;
}
编译
#gcc -g a1.c
反汇编
#objdump -dS a.out
#include <stdio.h>
int main(void)
{
8048394: 55 push %ebp
8048395: 89 e5 mov %esp,%ebp
8048397: 83 ec 10 sub $0x10,%esp
int a=1;
804839a: c7 45 f4 01 00 00 00 movl $0x1,-0xc(%ebp)
int b=2;
80483a1: c7 45 f8 02 00 00 00 movl $0x2,-0x8(%ebp)
int c=0;
80483a8: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp)
a = (b>c)?1:0;
80483af: 8b 45 f8 mov -0x8(%ebp),%eax
80483b2: 3b 45 fc cmp -0x4(%ebp),%eax
80483b5: 0f 9f c0 setg %al
80483b8: 0f b6 c0 movzbl %al,%eax
80483bb: 89 45 f4 mov %eax,-0xc(%ebp)
return 0;
80483be: b8 00 00 00 00 mov $0x0,%eax
}
80483c3: c9 leave
80483c4: c3 ret
80483c5: 90 nop
编译
#gcc -g a2.c
反汇编
#objdump -dS a.out
#include <stdio.h>
int main(void)
{
8048394: 55 push %ebp
8048395: 89 e5 mov %esp,%ebp
8048397: 83 ec 10 sub $0x10,%esp
int a=1;
804839a: c7 45 f4 01 00 00 00 movl $0x1,-0xc(%ebp)
int b=2;
80483a1: c7 45 f8 02 00 00 00 movl $0x2,-0x8(%ebp)
int c=0;
80483a8: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp)
if(b>c)
80483af: 8b 45 f8 mov -0x8(%ebp),%eax
80483b2: 3b 45 fc cmp -0x4(%ebp),%eax
80483b5: 7e 09 jle 80483c0 <main+0x2c>
{
a=1;
80483b7: c7 45 f4 01 00 00 00 movl $0x1,-0xc(%ebp)
80483be: eb 07 jmp 80483c7 <main+0x33>
}
else
{
a=0;
80483c0: c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp)
}
return 0;
80483c7: b8 00 00 00 00 mov $0x0,%eax
}
80483cc: c9 leave
80483cd: c3 ret
80483ce: 90 nop