Turing Complete

Turing Complete

View Stats:
Dante Nov 24, 2021 @ 8:56pm
Signed comparison through subtraction
I'm trying to implement the signed less operation as in x86, through subtraction and checking the flags. I'm using the two rules:
1. Overflow happens when two numbers have the same sign and their sum has the opposite sign.
2. A < B means that after A-B the overflow flag will differ from the sign flag (the result's high bit).

I implemented these rules in the Signed Less exercise, and it passed the tests. https://i.ibb.co/HT1MyFq/signed-less-through-flags.png
Great. But now I set A = 0 and B = -128 (as on the screenshot), and this schematic tells me that A < B. Meanwhile, the blue element Signed Less says the opposite.

Did I miss something?
< >
Showing 1-6 of 6 comments
SunCat Nov 27, 2021 @ 8:11am 
wiki on overflow bit
> The overflow flag is thus set when the most significant bit (here considered the sign bit) is changed by adding two numbers with the same sign (or subtracting two numbers with opposite signs).
so, you mixed up overflow for subtraction and overflow for addition.
Dante Nov 27, 2021 @ 4:55pm 
Originally posted by SunCat:
you mixed up overflow for subtraction and overflow for addition.
I treat A-B as A+(-B), and regardless of the second operand, the conditions for the overflow should remain the same.

Conditional jumps work perfectly for all cases except B = -128.
Last edited by Dante; Nov 27, 2021 @ 5:58pm
SunCat Nov 28, 2021 @ 3:11am 
Originally posted by Dante:
I treat A-B as A+(-B), and regardless of the second operand, the conditions for the overflow should remain the same.

Conditional jumps work perfectly for all cases except B = -128.

It doesn't remain the same because behavior with -128 is not the same: -(-128) equals -128 in binary (you can even see that in your own screenshot, compare number before and after NEG)
0 + (-128) = -128 (no overflow)
0 - (-128) = 128 in math, -128 in 8bit math (overflow)
chargrill Nov 29, 2021 @ 4:41pm 
late to the party so I'm not sure if you ever got your answer, but I couldn't for the life of me remember how to do overflow and your design really helped so...

You're using 1s compliment instead of 2s compliment. whack an "on" gate (or constant 1) on the carry in of the adder in your design and it stops saying 0 is less than -128
Dante Nov 29, 2021 @ 8:05pm 
Originally posted by chargrill88:
I couldn't for the life of me remember how to do overflow and your design really helped so...
Glad to help!

Originally posted by chargrill88:
You're using 1s compliment instead of 2s compliment.
I'm using NEG (2's complement) rather than NOT (1's complement).

I'll try SunCat's suggestion later.
chargrill Dec 3, 2021 @ 9:12am 
my mistake, thought it was a NOT, however I stand by your solution with a Byte NOT (instead of NEG) and carry in set high worked fine comparing 0 and -128.
< >
Showing 1-6 of 6 comments
Per page: 1530 50

Date Posted: Nov 24, 2021 @ 8:56pm
Posts: 6