0

Am having a problem understanding the indication of C and V Flags in the CPU Flags register.

For a C-Flag, its set when we genrate a carry or a borrow on the MSB. I think i understnad how its generated. However, am having a problem knowing what it indicates for the result of any arithmetic calculation.

My Tutor says that if C = 1, then the result of the addition or subtraction is wrong if we are dealing with an unsigned number.

I still dont undestand which is the unsigned number. Is it one of the two numbers that are being added or subtracted? what if one of them is negative and we generate C = 1, which is the number being referenced? Or is it the result?

I dont understnad why a V-Flag is generated in this case

90 - 55
  0101 1010  
- 1100 1001 (two's complement of 55) 
-------------------
  1001 0001 (-111)
N = 1, Z = 0, C = 1, V = 1
Hakim Marley
  • 330
  • 2
  • 5
  • 19
  • 2
    Unsigned numbers can't be negative by definition. Simply put, `V` is for signed overflow and `C` is for unsigned. That is, if the result of the operation is incorrect (does not fit in the range) for the given operation, the appropriate flag is set. Your example is `90 - (-55) = 90 + 55 = 145` which is outside of the 8 bit signed range of -128..+127 hence, `V` set. It is **not** `90 - 55` which would be `90 + (-55)` and would not generate overflow. – Jester Nov 17 '15 at 16:28
  • When you say signed or umsigned...thats where i get lost. Is it the result that is signed or u signed or one of the numbers in the operation? – Hakim Marley Nov 17 '15 at 16:38
  • 1
    One important thing to realize in assembly is that you can't look at a value in a register and know whether it is signed or unsigned. The CPU doesn't know. It's how the number is used that determines this. For example, in x86, the `IMUL` instruction is used for signed multiplication and `MUL` for unsigned. It's up to the programmer to decide what the bits mean. For addition, the rules are the same for signed and unsigned, so the `ADD` instruction is used for both. However, how one interprets the flags after the instruction does depends on whether the number is meant to be signed or unsigned. – pcarter Nov 17 '15 at 16:41
  • 1
    As @pcarter said, you decide whether you want signed or unsigned operation. It's up to you how you interpret the bit patterns. `1100 1001` is `-55` if you consider it signed, and `+201` if you consider it unsigned. You should decide up front which one, and apply this to both operands and the result, and check the appropriate flag. See also this [awesome illustration](http://stackoverflow.com/a/24002847/547981). – Jester Nov 17 '15 at 16:55
  • When wer talking about the choice being upto the programmer to use either signed or unsigned, are we meaning the one that writes assembly code or for example one that designes a compiler or are we meaning one that programs in a compiler lets say VStudio or Eclipse? – Hakim Marley Nov 17 '15 at 17:17
  • 2
    @HakimMarley when working with integers in the computer you (the programmer) can treat them as signed or unsigned. So if you have a 16-bit number, it's range unsigned would be 0 to 65535 (FFFFh). If it's treated as signed, then the highest bit is the *sign bit* and your range is -32768 (8000h) to +32767 (7FFFh). When you perform an operation on the numbers in the CPU, it sets C and V flags according to whether there's a carry in or out of the highest bit. If this is confusing, then you need to do some research on *two's complement representation* and then read up on CPU status registers. – lurker Nov 17 '15 at 17:27
  • 2
    As explained, when writing assembler only you know whether your numbers are signed or unsigned and you use the appropriate branch instruction accordingly. For example in 8086, you use `JA` (jump if above) to test the result of an unsigned operation, which checks the `C` and `Z` flags. But you use `JG` (jump if greater) for a signed result, which instead checks the `O` and `Z` flags. When writing in C the compiler will use the appropriate tests depending on whether you have defined your variable as `signed` or `unsigned`. – Weather Vane Nov 17 '15 at 17:33

1 Answers1

0

Well, it will always be upto the programmer meaning the one the programs in assembly code that decides when a correct answer will be provided based on the flags that are emitted from any binary operation.

Hakim Marley
  • 330
  • 2
  • 5
  • 19