Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Inspect Assembly Code

C Code

#include <stdio.h>

int is_equal(int x, int y) { return !(x & ~y); }

int main(void) {
  int x1 = 0x45;
  int x2 = 0x55;
  int y1 = 0x45;
  int y2 = 0x66;

  /* test with == first */
  if (x1 == y1) {
    printf("TRUE: %#010x\n", x1 == y1);
  }
  printf("is_equal: %#010x\n", is_equal(x1, y1));
  printf("is_equal: %#010x\n", is_equal(x2, y2));
  return 0;
}

Assembly

	.file	"isequal.c"
	.text
	.globl	is_equal
	.type	is_equal, @function
is_equal:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -4(%rbp)
	movl	%esi, -8(%rbp)
	movl	-8(%rbp), %eax
	notl	%eax
	andl	-4(%rbp), %eax
	testl	%eax, %eax
	sete	%al
	movzbl	%al, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	is_equal, .-is_equal
	.section	.rodata
.LC0:
	.string	"TRUE: %#010x\n"
.LC1:
	.string	"is_equal: %#010x\n"
	.text
	.globl	main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	$69, -4(%rbp)
	movl	$85, -8(%rbp)
	movl	$69, -12(%rbp)
	movl	$102, -16(%rbp)
	movl	-4(%rbp), %eax
	cmpl	-12(%rbp), %eax
	jne	.L4
	movl	-4(%rbp), %eax
	cmpl	-12(%rbp), %eax
	sete	%al
	movzbl	%al, %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
.L4:
	movl	-12(%rbp), %edx
	movl	-4(%rbp), %eax
	movl	%edx, %esi
	movl	%eax, %edi
	call	is_equal
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	-16(%rbp), %edx
	movl	-8(%rbp), %eax
	movl	%edx, %esi
	movl	%eax, %edi
	call	is_equal
	movl	%eax, %esi
	leaq	.LC1(%rip), %rax
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf@PLT
	movl	$0, %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Debian 14.2.0-19) 14.2.0"
	.section	.note.GNU-stack,"",@progbits