Assembly - Carry Flag when negative minus negative

1

If the CPU treats the subtraction as add like this question.
Then (-1) - (-2) should be FF...FF + 00...02, and the CARRY FLAG should be set.
I wrote c code to test (-1) - (-2) on linux, but found the CARRY FLAG was not set.
How CPU deal with this situation.

Disassembly Code

00000000004004ed <main>:
  4004ed:   55                      push   %rbp
  4004ee:   48 89 e5                mov    %rsp,%rbp
  4004f1:   c7 45 fc ff ff ff ff    movl   $0xffffffff,-0x4(%rbp)
  4004f8:   c7 45 f8 fe ff ff ff    movl   $0xfffffffe,-0x8(%rbp)
  4004ff:   8b 45 f8                mov    -0x8(%rbp),%eax
  400502:   8b 55 fc                mov    -0x4(%rbp),%edx
  400505:   29 c2                   sub    %eax,%edx
  400507:   89 d0                   mov    %edx,%eax
  400509:   89 45 f4                mov    %eax,-0xc(%rbp)
  40050c:   5d                      pop    %rbp
  40050d:   c3                      retq   
  40050e:   66 90                   xchg   %ax,%ax

GDB test

(gdb) ni
0x0000000000400505 in main ()
(gdb)
0x0000000000400507 in main ()
(gdb) p $eflags
$1 = [ IF ]
assembly
x86
cpu
alu
eflags
asked on Stack Overflow Dec 1, 2019 by mingw xu • edited Dec 2, 2019 by phuclv

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0