Sign, carry and overflow (assembly)

.data val1 BYTE 10h val2 WORD 8000h val3 DWORD 0FFFFh val4 WORD 7FFFh 

If val2 is incremented by 1 using the ADD command, what will the Carry values ​​and sign flags be?

If val4 is incremented by 1 using the ADD command, what will the Over- flags of the stream and sign be?

I replied that both of them are mistaken. Why the answer, what is it?

+3
source share
3 answers

Imagine this as an analog clock. In the present watch, the times "sixty-five" (55) and "five-seven" (-5) are identical. This is simply the problem of your definition, the clock itself does not know your definition and works round and round ... The same thing with the processor. Look at the following "CPU-clock":

CPU clock (Addition)

I chose a 4-bit clock, but the principle is the same for every other group of bits (BYTE, WORD, DWORD, etc.).

The carry flag will be set after the clock has jumped from 15 to 0. This is in this case a flag for unsigned overflow.

The overflow flag will be set after the clock has jumped from 7 to -8. It blocks the signed overflow.

The sign flag denotes a value that can be interpreted as a negative number.

The behavior of the flags is slightly different if you think from below:

CPU clock (Subtraction)

+13
source

Trying to summarize what has already been discussed in most comments ...

x86, as well as all other processor architectures that I have seen so far, have no real idea of ​​signed and unsigned numbers. It is your interpretation that determines whether the 16-bit value is 8000h +32768 (unsigned) or -32768 (signed).

There are no separate signed and unsigned instructions for adding and subtracting; again, you decide if 7FFFh + 0001h valid (unsigned) addition or overflow (since 7FFFh is the maximum signed integer in a 16-bit complement).

To help you as a programmer, arithmetic operations set flags for both interpretations. Upon completion of the operation, you will check the appropriate flags. Rule of thumb:

  • for signed numbers, check the overflow flag and sign
  • for unsigned numbers, check the carry flag; it acts more or less as an "unsigned overflow"

Examples:

  • 7FFFh + 1 overflows when signing (OF is set), and not when unsigned (CF = 0); 8000h result is negative when signing (SF = 1)
  • FFFFh + 1 overflows when unsigned (CF is set), and not when signed (OF = 0); result 0000h is not negative (SF = 0)
+1
source

1.specify platform flags can be set differently on different platforms

2. Also, which bit are the commonly used instructions used?

  8000h +0001h ------ 8001h -> Carry=0,Sign16=1 7FFFh +0001h ------ 8000h -> Carry=0,Sign16=1 
  • most processors do not treat numbers as signed; instead, they use the binary 2'os binary representation
0
source

Source: https://habr.com/ru/post/1246977/


All Articles