Author Topic: What is the difference between “4 - 12” and “4 + (-12)”?  (Read 12234 times)

Offline danny

  • Jr. Member
  • *
  • Posts: 3
What is the difference between “4 - 12” and “4 + (-12)”?
« on: November 20, 2020, 11:33:45 PM »
Hello. I try to compare the next expressions:

1)
    mov al, 4
    mov bl, 12
    sub al, bl ; CF == 1

    0000 0100 (4)
   +
    1111 0100 (-12)
   =
    1111 1000 (-8 == 248)

2)
    mov al, 4
    mov bl, -12
    add al, bl ; CF == 0

    0000 0100 (4)
   +
    1111 0100 (-12)
   =
    1111 1000 (-8 == 248)

The results are identical, but carry flags are not. Why? The subtraction realized by addition to twos complement value.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: What is the difference between “4 - 12” and “4 + (-12)”?
« Reply #1 on: November 21, 2020, 12:05:18 AM »
Hi Danny,

Welcome to the forum.

Hello. I try to compare the next expressions:

1)
    mov al, 4
    mov bl, 12
    sub al, bl ; CF == 1

    0000 0100 (4)
   +
    1111 0100 (-12)

But that isn't what you're doing.

0000 1100 (12)

Subtracting that sets the carry flag. Adding -12 does not.

Best,
Frank

.





Offline danny

  • Jr. Member
  • *
  • Posts: 3
Re: What is the difference between “4 - 12” and “4 + (-12)”?
« Reply #2 on: November 21, 2020, 01:28:10 PM »
Sorry, but I do not understand. Can you explain me why it is so. The subtraction realized by addition to twos complement value. Isn't it?

Offline danny

  • Jr. Member
  • *
  • Posts: 3
Re: What is the difference between “4 - 12” and “4 + (-12)”?
« Reply #3 on: November 21, 2020, 02:05:54 PM »
I think, when we sub something that is equal to add, but by addition to twos complement value. And when we get twos complement value CPU inverted bits (neg, when we use neg command CF = 1) and then as the result we have CF = 1. Is it so?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: What is the difference between “4 - 12” and “4 + (-12)”?
« Reply #4 on: November 21, 2020, 08:25:03 PM »
Hi Danny,

You are correct that "neg" sets CF But "sub". is a separate instruction... as you show...

Best,
Frank