Questions tagged [signed-overflow]

15 questions
28
votes
2 answers

Overflow and Carry flags on Z80

I have gotten round to implementing the ADD A,r set of opcodes on my Z80 core. I had a bit of confusion about the carry and overflow flags which I think I've nailed, but I wanted to put it to the community to check that I'm right. Basically, from…
PhilPotter1987
  • 1,306
  • 2
  • 12
  • 20
22
votes
2 answers

Assembly - Carry flag VS overflow flag

I have the next code: mov al, -5 add al, 132 add al, 1 As I check it, the overflow flag and the carry flag will set in the first operation, and in the second, only the overflow will set. But I don't understand why: In unsigned number, the result…
Tom O
  • 237
  • 1
  • 4
  • 7
14
votes
2 answers

Is there a safe way to get the unsigned absolute value of a signed integer, without triggering overflow?

Consider a typical absolute value function (where for the sake of argument the integral type of maximum size is long): unsigned long abs(long input); A naive implementation of this might look something like: unsigned long abs(long input) { if…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
11
votes
1 answer

Why does cmp 0x84,0x30 trigger the overflow flag?

I've been playing with assembly for a while and looking at some code. in which AL is first set to 0x84 then cmp AL, 0x30 is used. This instruction then triggers the Overflow flag. From what I read CMP is supposed to subtract the second number from…
Maciek
  • 261
  • 2
  • 11
5
votes
1 answer

Understanding the difference between overflow and carry flags

I am designing a 16 bit ALU in verilog based on an existing RISC ISA. The ISA says that the carry flag is set when the operation is unsigned, and overflow is set when the operation is signed. The interesting thing is that the ISA implements ADD and…
richbai90
  • 4,994
  • 4
  • 50
  • 85
3
votes
1 answer

x86 left shift arithmetically overflow control

Do you know any way to efficient check if overflow/underflow occurs on x86 left shift arithmetically?
LooPer
  • 1,459
  • 2
  • 15
  • 24
3
votes
3 answers

Sign, Carry, and Overflow Flag (Assembly)

.data val1 BYTE 10h val2 WORD 8000h val3 DWORD 0FFFFh val4 WORD 7FFFh If val2 is incremented by 1 using the ADD instruction, what will be the values of the Carry and Sign flags? If val4 is incremented by 1 using the ADD instruction, what will be…
user3682802
  • 89
  • 1
  • 2
  • 8
2
votes
2 answers

C: Undefined behavior when multiplying uint16_t?

I've realized and read that multiplying a uint16_t with another uint16_t results in an integer (it actually seems to be a signed integer? see: ). Given that, do I have to assume that the following function f produces undefined behavior, because…
Kevin Meier
  • 2,339
  • 3
  • 25
  • 52
2
votes
1 answer

Why does std::push_heap generate a -Wstrict-overflow=3 warning even if no signed types are involved?

According to the documentation of -Wstrict-overflow, level 3: Also warn[s] about other cases where a comparison is simplified. For example: x + 1 > 1 is simplified to x > 0. The MWE shown below throws the following warning on level 3 and up, but…
bitmask
  • 32,434
  • 14
  • 99
  • 159
2
votes
4 answers

Checking for overflow and/or carry flags, getting an integer code of which happened

First, I want to point out that this isn't really x86, it's msx88 which is a sort of simplified version of x86 for learning purposes. I need to make a function that checks for arithmetic errors (carry, overflow) and I know that I can use jo and jc…
Aviar
  • 23
  • 1
  • 3
1
vote
1 answer

Why is the overflow flag not being set in this example?

mov al, -1 add al, 130 I am attempting to answer a question from the textbook for my x86-assembly class. One of the examples asks to explain why the over flag would help you determine if, in this instance, the final value of al falls within…
1
vote
2 answers

When is Overflow flag set?

Can anybody explain why the overflow flag is set here? I learned that it is set if you add to positives and get a negative or subtract to negatives and get a positive! LDR r0,=0X80000000 LDR r1,=0X40000000 SUBS r7,r10,r0
mangokitty
  • 1,759
  • 3
  • 12
  • 17
1
vote
2 answers

does NEG instruction in assembly language sets the Overflow flag

I want to know if the NEG instruction affects the overflow flag too. I know that it negates the value of a variable, but couldn't find out whether it affects the Overflow flag or not.
0
votes
0 answers

Why signed integer overflow in c++ is undefined rather than implementation-defined?

From what I find on the net, signed integer overflow is undefined in c++ because we simply don't have only one representation for signed integers. But I can't understand why that doesn't make it implementation-defined where each implementation can…
0
votes
3 answers

how to determine if overflow flag is turned on/off for mixed sign binary addition?

I'm still don't completely understand overflow flags but from what I've gathered if the two most significant bits are both positive and result in a negative and vice versa, the overflow flag would turn on. But what about in the case of mixed sign…
kriloots
  • 69
  • 9