题目:如下程序输出结果为什么?
#include "stdio.h"
void main()
{
unsigned char a = 0x55;
unsigned char b = 0xaa;
if(a == ~b)
{
printf("equal");
}else
{
printf("not equ ");
}
}
结果是输出 not equ
反汇编得到
.file "test.c"
.section .rodata
.LC0:
.string "equal %x"
.LC1:
.string "not equ %x"
.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
subq $16, %rsp
movb $85, -2(%rbp)
movb $-86, -1(%rbp)
movzbl -2(%rbp), %eax
movzbl -1(%rbp), %edx
notl %edx
cmpl %edx, %eax
jne .L2
movzbl -1(%rbp), %eax
notl %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
jmp .L4
.L2:
movzbl -1(%rbp), %eax
notl %eax
movl %eax, %esi
movl $.LC1, %edi
movl $0, %eax
call printf
.L4:
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609"
.section .note.GNU-stack,"",@progbits
从汇编中的movzbl -2(%rbp), %eax 与 movzbl -1(%rbp), %edx 实现了将unsigned char 转化为unsigned int 数据类型。故在比较时就变为 a = 0x00000055,而c=~b=0xffffff55.