NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: danny on November 20, 2020, 11:33:45 PM

Title: What is the difference between “4 - 12” and “4 + (-12)”?
Post by: danny 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.
Title: Re: What is the difference between “4 - 12” and “4 + (-12)”?
Post by: Frank Kotler 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

.




Title: Re: What is the difference between “4 - 12” and “4 + (-12)”?
Post by: danny 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?
Title: Re: What is the difference between “4 - 12” and “4 + (-12)”?
Post by: danny 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?
Title: Re: What is the difference between “4 - 12” and “4 + (-12)”?
Post by: Frank Kotler 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